Commits

Nick Raptis  committed 6d4223a

Exercises for week 3 (01/04/2011)

- Stack implementation

  • Participants
  • Parent commits db51a64

Comments (0)

Files changed (1)

File 1550_Ask3_stack.c

+/*
+  Name: Raptis Nikolaos
+  AM: 1550
+  Omada: Paraskeyi 16-18 
+  Date: 01/04/11 16:44
+  Description: Ylopoiisi stoibas
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#define N 4
+
+int stack[N], head=0, check;
+void prints(void);
+void push(int);
+int pop(void);
+
+main()
+{
+    int choice, x;
+    
+    do
+    {
+        printf("0: Ektypwsi stoibas\n1: Eisagwgi sti stoiba\n");
+        printf("2: Trabigma apo stoiba\n3: Eksodos\nDwste entoli...\n");
+        scanf("%d", &choice);
+        printf("\n");
+        
+        if(choice==0) //print stack
+        {
+            prints();
+        }
+        else if(choice==1) //push
+        {
+            printf("Arithmos gia eisagwgi : ");
+            scanf("%d", &x);
+            push(x);
+            if(check==1)
+            {
+                printf("Sfalma: H stoiba einai gemati\n");
+            }
+            else
+            {
+                printf("Eisagwgi epityxis\n");
+            }
+        }
+        else if(choice==2) //pop
+        {
+            x = pop();
+            if(check==1)
+            {
+                printf("Sfalma: H stoiba einai adeia\n");
+            }
+            else
+            {
+                printf("Htan: %d\n", x);
+            }
+        }
+        else if(choice==3) //exit
+        {
+            printf("Eksodos!\n");
+        }
+        else
+        {
+            printf("Lathos entoli\n");
+        }
+        
+        printf("\n\n");
+    } while(choice!=3);
+
+    system("pause");    
+}
+
+void prints(void)
+{
+    int i;
+    printf("Stoiba:\n");
+    for(i=0; i<head; i++)
+    {
+        printf("%d ",stack[i]);
+    }
+    printf("\n");
+}
+
+void push(int x)
+{
+    if(head<N)
+    {
+        head++;
+        stack[head-1] = x;
+        check = 0;
+    }
+    else
+    {
+        check = 1;
+    }
+}
+
+int pop(void)
+{
+    int x;
+    if(head>0)
+    {
+        x = stack[head-1];
+        head--;
+        check = 0;
+    }
+    else
+    {
+        check = 1;
+    }
+}