# Commits

committed 32ec348

Exercises for week 7 (27/05/2011)

- Dynamic Stack as a Linked List implementation

# Files changed (1)

+/*
+  Name: Raptis Nikolaos
+  AM: 1550
+  Date: 27/05/11 16:50
+  Description: Dynamic Stack through Linked List
+*/
+#include <stdio.h>
+#include <stdlib.h>
+
+struct node
+{
+    int num;
+    struct node *next;
+};
+
+void print_stack(void);
+void push(int x);
+int pop(void);
+
+
+main()
+{
+    int choice, x;
+
+    do
+    {
+        printf("1: Push\n2: Pop\n3: Exit\n");
+        scanf("%d", &choice);
+
+        if (choice==0)
+        {
+            print_stack();
+        }
+        else if (choice==1)
+        {
+            printf("Arithmos gia eisagwgi :\n");
+            scanf("%d", &x);
+            push(x);
+        }
+        else if (choice==2)
+        {
+            printf("%d\n", x);
+            {
+                x = pop();
+                printf("O arithos itan : %d\n", x);
+            }
+            else
+                printf("H oura einai adeia.\n");
+        }
+        else if (choice==3)
+        {
+            printf("Eksodos\n");
+        }
+        else
+        {
+            printf("Lathos entoli\n");
+        }
+        printf("\n\n");
+    } while (choice != 3);
+}
+
+
+void push(int x)
+{
+    struct node *newnode;
+    newnode = (struct node*)malloc(sizeof(struct node));
+    newnode->num = x;
+    newnode->next = head;
+    head = newnode;
+    return;
+}
+
+int pop(void)
+{
+    int x;
+    {
+        struct node *pnt;
+        pnt = head;
+        x = pnt->num;
+        head = pnt->next;
+        free(pnt);
+        return x;
+    }
+}
+
+void print_stack(void)
+{
+    struct node *pnt;
+    pnt = head;
+    while(pnt)
+    {
+        printf("%d ", pnt->num);
+        pnt = pnt->next;
+    }
+    printf("\n");
+    return;
+}