Nick Raptis avatar Nick Raptis committed 32ec348

Exercises for week 7 (27/05/2011)

- Dynamic Stack as a Linked List implementation

Comments (0)

Files changed (1)

1550_Ask6_dynstack.c

+/*
+  Name: Raptis Nikolaos
+  AM: 1550
+  Omada: Paraskeyi 16-18 
+  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);
+
+struct node *head;
+
+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);
+            if (head)
+            {
+                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;
+    if(head)
+    {
+        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;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.