# Commits

committed 1c52781

Exercises for week 5 and 6 (13&20/05/2011)

- pointers, sizeof, malloc and free

• Participants
• Parent commits 0e602a0

# Files changed (1)

`+/*`
`+  Name: Raptis Nikolaos`
`+  AM: 1550`
`+  Omada: Paraskeyi 16-18 `
`+  Date: 13/05/11 16:44`
`+  Date: 20/05/11 16:44`
`+  Description: Linked List`
`+*/`
`+#include <stdio.h>`
`+#include <stdlib.h>`
`+`
`+struct node`
`+{`
`+    int num;`
`+    struct node *next;`
`+};`
`+`
`+void insert(int x);`
`+void deletion(int x);`
`+void search(int x);`
`+void traverse(void);`
`+`
`+struct node *head=NULL, *last=NULL;`
`+`
`+main()`
`+{`
`+    int choice, x;`
`+    `
`+    do`
`+    {`
`+        printf("1: Eisagwgi\n2: Diagrafi\n3: Eyresi\n4: Diaperasi\n");`
`+        printf("5: Eksodos\nDwste entoli...\n");`
`+        scanf("%d", &choice);`
`+        printf("\n");`
`+        `
`+        if(choice==1) //insert`
`+        {`
`+            printf("Arithmos gia eisagwgi : ");`
`+            scanf("%d", &x);`
`+            insert(x);`
`+        }`
`+        else if(choice==2) //deletion`
`+        {`
`+            printf("Arithmos gia diagrafi : ");`
`+            scanf("%d", &x);`
`+            deletion(x);`
`+        }`
`+        else if(choice==3) //search`
`+        {`
`+            printf("Arithmos gia eyresi : ");`
`+            scanf("%d", &x);`
`+            search(x);`
`+        }`
`+        else if(choice==4) //traverse`
`+        {`
`+            traverse();`
`+        }`
`+        else if(choice==5) //exit`
`+        {`
`+            printf("Eksodos!\n");`
`+        }`
`+        else`
`+        {`
`+            printf("Lathos entoli\n");`
`+        }`
`+        `
`+        printf("\n\n");`
`+        `
`+        fflush(stdin);`
`+    } while(choice!=5);`
`+`
`+    system("pause");    `
`+}`
`+`
`+void insert(int x)`
`+{`
`+    struct node *newnode;`
`+    newnode = (struct node*)malloc(sizeof(struct node));`
`+    newnode->num = x;`
`+    if (head != NULL) `
`+    {`
`+        last->next = newnode;`
`+        last = newnode;`
`+    }`
`+    else // first node`
`+        head = last = newnode;`
`+    newnode->next = NULL;`
`+    return;`
`+}`
`+`
`+`
`+void deletion(int x)`
`+{`
`+    struct node *pnt, *lst=NULL;`
`+    pnt = head;`
`+    while (pnt != NULL)`
`+    {`
`+        if (pnt->num == x)`
`+        {`
`+            if (lst != NULL)`
`+                lst->next = pnt->next;`
`+            else             // first node`
`+                head = pnt->next;`
`+            if (last == pnt) // last node`
`+                last = lst;`
`+            printf("Diagrafike\n");`
`+            free(pnt);`
`+            return;`
`+        }`
`+        lst = pnt;`
`+        pnt = pnt->next;`
`+    }`
`+    printf("Den brethike\n");`
`+    return;`
`+}`
`+`
`+`
`+void search(int x)`
`+{`
`+    struct node *pnt;`
`+    pnt = head;`
`+    int i=1;`
`+    while (pnt != NULL)`
`+    {`
`+        if (pnt->num == x)`
`+        {`
`+            printf("Brethike stin thesi %d\n", i);`
`+            return;`
`+        }`
`+        i++;`
`+        pnt = pnt->next;`
`+    }`
`+    printf("Den brethike\n");`
`+    return;`
`+}`
`+`
`+`
`+void traverse(void)`
`+{`
`+    struct node *pnt;`
`+    pnt = head;`
`+    int i=1;`
`+    while (pnt != NULL)`
`+    {`
`+        printf("%2d: %d\n", i, pnt->num);`
`+        i++;`
`+        pnt = pnt->next;`
`+    }`
`+    return;`
`+}`
`+`