Skip to content
Snippets Groups Projects
Commit df1eaae6 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Sommersemester 2023

parent fe167126
Branches
No related tags found
No related merge requests found
Showing
with 0 additions and 1232 deletions
File deleted
#include <stdio.h>
#include <stdlib.h>
typedef struct SingleLinkedNode {
int value;
struct SingleLinkedNode *next;
} SingleLinkedNode;
typedef struct FullLinkedNode {
int value;
struct FullLinkedNode *prev;
struct FullLinkedNode *next;
} FullLinkedNode;
typedef struct FullLinkedList {
FullLinkedNode *first;
FullLinkedNode *last;
} FullLinkedList;
SingleLinkedNode *reverseLinkedList(SingleLinkedNode *head) {
if (head) {
// ... -> cur -> n1 -> n2 -> ...
SingleLinkedNode *cur = head;
SingleLinkedNode *n1 = head->next;
SingleLinkedNode *n2;
cur->next = NULL;
while (n1 != NULL) {
n2 = n1->next;
n1->next = cur;
cur = n1;
n1 = n2;
}
return cur;
} else {
return NULL;
}
}
void printLinkedList(SingleLinkedNode *head) {
while (head != NULL) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
}
FullLinkedList *singleLinkedListToFullLinkedList(SingleLinkedNode *head) {
FullLinkedList *fullLinkedList = malloc(sizeof(FullLinkedList));
if (head) {
FullLinkedNode *cur = malloc(sizeof(FullLinkedNode));
SingleLinkedNode *curSingle = head;
cur->value = curSingle->value;
cur->prev = NULL;
if (curSingle->next) {
cur->next = malloc(sizeof(FullLinkedNode));
} else {
cur->next = NULL;
}
fullLinkedList->first = cur;
while (curSingle->next) {
cur->next->prev = cur;
cur->next->value = curSingle->next->value;
curSingle = curSingle->next;
cur = cur -> next;
if (curSingle->next) {
cur->next = malloc(sizeof(FullLinkedNode));
} else {
cur->next = NULL;
}
}
fullLinkedList->last = cur;
} else {
fullLinkedList->first = NULL;
fullLinkedList->last = NULL;
}
return fullLinkedList;
}
void printFullLinkedList(FullLinkedList *fullLinkedList) {
printf("Forward\n");
for (FullLinkedNode *cur = fullLinkedList->first; cur; cur = cur->next) {
printf("%d ", cur->value);
}
printf("\n");
printf("Backward\n");
for (FullLinkedNode *cur = fullLinkedList->last; cur; cur = cur->prev) {
printf("%d ", cur->value);
}
printf("\n");
}
int main(void) {
// Task: Reverse linked list
SingleLinkedNode n5 = {5, NULL};
SingleLinkedNode n4 = {4, &n5};
SingleLinkedNode n3 = {3, &n4};
SingleLinkedNode n2 = {2, &n3};
SingleLinkedNode n1 = {1, &n2};
printf("Initial\n");
printLinkedList(&n1);
// Works with normal linked list
SingleLinkedNode *reversedHead = reverseLinkedList(&n1);
printf("Reversed\n");
printLinkedList(reversedHead);
// Works with NULL as input
reverseLinkedList(NULL);
// Works with linked list that has only one node
SingleLinkedNode foobar = {42, NULL};
reverseLinkedList(&foobar);
// Task: Convert single linked list to full linked list
SingleLinkedNode n10 = {10, NULL};
SingleLinkedNode n9 = {9, &n10};
SingleLinkedNode n8 = {8, &n9};
SingleLinkedNode n7 = {7, &n8};
SingleLinkedNode n6 = {6, &n7};
FullLinkedList *fullLinkedList = singleLinkedListToFullLinkedList(&n6);
printf("\nBefore conversion\n");
printLinkedList(&n6);
printf("After conversion\n");
printFullLinkedList(fullLinkedList);
return 0;
}
#include <stdlib.h>
int a = 0;
int b = 1;
int c = 2;
int main (void)
{
int d = 3;
int e = 4;
int *f = malloc (sizeof (int));
*f = 5;
int *g = malloc (sizeof (int));
*g = 6;
return 0;
}
.file "memory-1.c"
.text
.globl main
.type main, @function
main:
.LFB11:
.cfi_startproc
movl $0, %eax
ret
.cfi_endproc # #include <stdlib.h>
.LFE11: #
.size main, .-main # int a = 0;
.globl c # int b = 1;
.data # int c = 2;
.align 4 #
.type c, @object # int main (void)
.size c, 4 # {
c: # int d = 3;
.long 2 # int e = 4;
.globl b # int *f = malloc (sizeof (int));
.align 4 # *f = 5;
.type b, @object # int *g = malloc (sizeof (int));
.size b, 4 # *g = 6;
b: # return 0;
.long 1 # }
.globl a
.bss
.align 4
.type a, @object
.size a, 4
a:
.zero 4
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
.file "memory-1.c"
.text
.globl a
.bss
.align 4
.type a, @object
.size a, 4
a:
.zero 4
.globl b
.data
.align 4
.type b, @object
.size b, 4
b:
.long 1
.globl c
.align 4
.type c, @object # #include <stdlib.h>
.size c, 4 #
c: # int a = 0;
.long 2 # int b = 1;
.text # int c = 2;
.globl main #
.type main, @function # int main (void)
main: # {
.LFB6: # int d = 3;
.cfi_startproc # int e = 4;
pushq %rbp # int *f = malloc (sizeof (int));
.cfi_def_cfa_offset 16 # *f = 5;
.cfi_offset 6, -16 # int *g = malloc (sizeof (int));
movq %rsp, %rbp # *g = 6;
.cfi_def_cfa_register 6 # return 0;
subq $32, %rsp # }
movl $3, -4(%rbp)
movl $4, -8(%rbp)
movl $4, %edi
call malloc@PLT
movq %rax, -16(%rbp)
movq -16(%rbp), %rax
movl $5, (%rax)
movl $4, %edi
call malloc@PLT
movq %rax, -24(%rbp)
movq -24(%rbp), %rax
movl $6, (%rax)
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE6:
.size main, .-main
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
../common/Zeichen_123.pdf
\ No newline at end of file
This diff is collapsed.
#include <stdio.h>
int main (void)
{
char *a[] = { "Dies", "ist", "ein", "Test" };
char **p = a;
return 0;
}
.file "arrays-1.c"
.text
.globl main
.type main, @function
main:
.LFB11:
.cfi_startproc
movl $0, %eax
ret
.cfi_endproc
.LFE11:
.size main, .-main
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != &a + 4)
printf ("%s\n", *p++);
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != &a[0] + 4)
printf ("%s\n", *p++);
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != a[0] + 4)
printf ("%s\n", *p++);
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != a[0] + 4)
printf ("%s\n", p++);
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
for (int i = 0; i < 4; i++)
printf ("%s\n", a[i]);
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
int i = 0;
while (p != a[0] + 4)
printf ("%s\n", p[i++]);
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != a[0] + 4)
{
printf ("%s\n", p);
p += 5;
}
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != a[][0] + 4)
{
printf ("%s\n", p);
p += 5;
}
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != a[0] + 5)
{
printf ("%s\n", p);
p += 5;
}
return 0;
}
#include <stdio.h>
int main (void)
{
static char a[][5] = { "Dies", "ist", "ein", "Test" };
static char *p = a[0];
while (p != a[4] + 5)
{
printf ("%s\n", p);
p += 5;
}
return 0;
}
#include <stdio.h>
int main (void)
{
char *a[] = { "Dies", "ist", "ein", "Test" };
char **p = a;
for (int i = 0; i < 4; i++)
printf ("%s\n", p[i]);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment