Operation on Stack [Array Version] - C/C++

A stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop.
The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data structure, the last element added to the structure must be the first one to be removed. This is equivalent to the requirement that, considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack. Often a peek or top operation is also implemented, returning the value of the top element without removing it.


Various operations on Stack :

1. Creation
2. Push
3. Pop
4. Display etc.


#include<stdio.h>
#include<conio.h>
 
int stack[20], top = -1;
int choice;
 
void menu();
void create();
void push();
void pop();
void display();
 
 
void main()
{
 int choice;
 char ch, ch2 = 'y';
 do
 {
  if (stack[0] == NULL)
  {
   do
   {
    clrscr();
    printf("\n\nError : Stack is Empty. Do you want to Create (Y/N).. ");
    fflush(stdin);
    scanf("%c", &ch);
    if (ch == 'n' || ch == 'N')
    {
     exit();
    }
    else if (ch == 'y' || ch == 'Y')
    {
     create();
     break;
    }
    else
    {
     printf("\n\nError : Invalid Choice");
    }
   } while (ch == 'y' || ch == 'Y');
  }
  else
  {
   clrscr();
 
   printf("\n1. Create");
   printf("\n2. Push");
   printf("\n3. Pop");
   printf("\n4. Display");
   printf("\n\nPress 0 to Exit");
 
   printf("\n\nEnter your Choice : ");
   scanf("%d", &choice);
 
   switch (choice)
   {
   case 0:
    exit();
    break;
   case 1:
    create();
    display();
    break;
   case 2:
    push();
    break;
   case 3:
    pop();
    break;
   case 4:
    display();
    break;
   default:
    printf("\n\nError : Invalid Choice. \n");
   }
   printf("\n\nWant to Continue (Y/N)... ");
   fflush(stdin);
   scanf("%c", &ch2);
  }
 
 } while (ch2 == 'y' || ch2 == 'Y');
}
 
 
void create()
{
 int i = 0, data;
 char c;
 top = -1;
 do
 {
  top++;
  if (top < 20)
  {
   printf("\nEnter Data :: ");
   scanf("%d", &data);
   stack[i] = data;
   i++;
   printf("\nDo you want to insert more ? (Y/N)");
   fflush(stdin);
   scanf("%c", &c);
  }
  else
  {
   printf("\nError : Stack is full.");
   fflush(stdin);
   c = 'n';
   top--;
  }
 } while (c == 'y' || c == 'Y' && i < 20);
}
void push()
{
 char c;
 int data;
 do
 {
  top++;
  if (top < 20)
  {
   printf("\nInsert Data :: ");
   scanf("%d", &data);
   stack[top] = data;
   fflush(stdin);
   printf("\nDo you want to add more ? (Y/N)");
   scanf("%c", &c);
  }
  else
  {
   top--;
   printf("\nError : Stack is full.");
   fflush(stdin);
   c = 'n';
  }
 } while (c == 'y' || c == 'Y');
 
}
void pop()
{
 char c;
 int data;
 do
 {
 
  if (top > -1)
  {
   data = stack[top];
   top--;
   printf("\nData :: %d", data);
   fflush(stdin);
   printf("\n\nDo you want to pop more ? (Y/N)");
   scanf("%c", &c);
  }
  else
  {
   printf("\nError : Stack is Empty.");
   fflush(stdin);
   c = 'n';
  }
 } while (c == 'y' || c == 'Y');
}
 
void display()
{
 int i = 0;
 for (i = 0; i < 20; i++)
 {
  printf("\n%d", stack[i]);
 }
}

Post a Comment