Stack using Arrays

Implement Stack using Arrays:

Required Knowledge:

C basics, Arrays

Program:

 1 #include<stdio.h>
 2  #define SIZE 10
 3  
 4  void push(int);
 5  void pop();
 6  void display();
 7  
 8  int stack[SIZE], top = -1;
 9  
10  void main()
11  {
12      int value, choice;
13      while(1)
14      {
15          printf("\n\n***** MENU *****\n");
16          printf("1. Push\n2. Pop\n3. Display\n4. Exit");
17          printf("\nEnter your choice: ");
18          scanf("%d",&choice);
19          switch(choice)
20          {
21          case 1:
22              printf("Enter the value to be insert: ");
23              scanf("%d",&value);
24              push(value);
25              break;
26          case 2:
27              pop();
28              break;
29          case 3:
30              display();
31              break;
32          case 4:
33              exit(0);
34          default:
35              printf("\nWrong selection Try again!!!");
36          }
37      }
38  }
39  void push(int value)
40  {
41      if(top == SIZE-1)
42          printf("\nStack is Full!!! Insertion is not possible!!!");
43      else
44      {
45          top++;
46          stack[top] = value;
47          printf("\nInsertion success!!!");
48      }
49  }
50  void pop()
51  {
52      if(top == -1)
53          printf("\nStack is Empty!!! Deletion is not possible!!!");
54      else
55      {
56          printf("\nDeleted : %d", stack[top]);
57          top--;
58      }
59  }
60  void display()
61  {
62      if(top == -1)
63          printf("\nStack is Empty!!!");
64      else
65      {
66          int i;
67          printf("\nStack elements are:\n");
68          for(i=top; i>=0; i--)
69              printf("%d\n",stack[i]);
70      }
71  }

Comments

Popular posts from this blog

C program to calculate velocity

C program to find square root of a number

C program to find maximum between two numbers