Headlines
Loading...
C program of Stack Operations using arrays

C program of Stack Operations using arrays

/* Stack Operations - arrays */

#include <stdio .h="">
#include <conio .h="">
#define SIZE 10

int stack[SIZE], top = 0;
void push(void);
void pop(void);
void display(void);

void main()
{
 int choice = 1;
 while (choice == 1 || choice == 2 || choice == 3)
 {
  clrscr();
  printf("Stack Implementation Using Arrays :-\n");
  printf("----- -------------- ----- ------\n\n");
  printf("1] Push an element onto the stack\n");
  printf("2] Pop an element from the stack\n");
  printf("3] Display the stack elements\n");
  printf("4] Exit\n\n");
  printf("Enter your choice :- ");
  scanf("%d",&amp;choice);
  switch (choice)
  {
  case 1 :
   push();
   break;
  case 2 :
   pop();
   break;
  case 3 :
   display();
   break;
  case 4 :
   break;
  default :
   printf("\nOut of range\n\n");
   choice = 1;
   getch();
  }
 }
 clrscr();
 printf("\nProgram finished...bye...bye...");
 getch();
}

/* Function to push an element onto the stack */
void push(void)
{
 int value;
 if (top == SIZE)
 {
  printf("\nStack Full...can not push...");
  getch();
 }
 else
 {
  printf("\nEnter the element value : ");
  scanf("%d", &amp;value);
  stack[top] = value;
  top++;
 }
 return;
}

/* Function to pop an element from the stack */
void pop(void)
{
 int value;
 if (top == 0)
 {
  printf("\nStack Empty...can not pop...");
  getch();
 }
 else
 {
  value = stack[top-1];
  printf("\n%d is popped from the stack...", value);
  getch();
  top--;
 }
 return;
}

/* Function to display all the elements of the stack */
void display(void)
{
 int i;
 if (top == 0)
 {
  printf("\nStack empty...No elements to display...");
  getch();
 }
 else
 {
  printf("\nStack elements are :-\n\n");
  for (i=0; i&lt;=(top-1); i++)
  {
   printf("%d  ----&gt;  %d\n", (i+1), stack[i]);
  }
  getch();
 }
 return;
}
*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***