Operation on Stack [LinkedList 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 Operation on Stack implemented using LinkedList :
1. Creation
2. Push
3. Pop
4. Display



#include<stdio.h>
#include<conio.h>
struct stack
{
 int val;
 struct stack *ptr;
}*start = NULL, *top = NULL;
int choice;
void menu();
void create();
void push();
void pop();
void display();
void main()
{
 int choice;
 char ch, ch2 = 'y';
 do
 {
  if (start == 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()
{
 char c;
 struct stack *temp;
 int data;
 start = NULL;
 do
 {
  temp = (struct stack*)malloc(sizeof(struct stack));
  printf("\nEnter Data :: ");
  scanf("%d", &data);
  temp->val = data;
  if (start == NULL)
  {
   temp->ptr = NULL;
  }
  else
  {
   temp->ptr = start;
  }
  start = temp;
  printf("\nDo you want to insert more ? (Y/N)");
  fflush(stdin);
  scanf("%c", &c);
 } while (c == 'y' || c == 'Y');
}
void push()
{
 char c;
 do
 {
  struct stack *temp;
  printf("\nEnter Data :: ");
  scanf("%d", &temp->val);
  if (start == NULL)
  {
   temp->ptr = NULL;
  }
  else
  {
   temp->ptr = start;
  }
  start = temp;
  fflush(stdin);
  printf("\n\nDo you want to push more ? (Y/N)");
  scanf("%c", &c);
 } while (c == 'y' || c == 'Y');
}
void pop()
{
 char c;
 do
 {
  if (start == NULL)
  {
   printf("\nError : Stack is empty.");
   fflush(stdin);
   c = 'n';
  }
  else
  {
   printf("\n%d", start->val);
   start = start->ptr;
   fflush(stdin);
   printf("\nDo you want to pop more ? (Y/N) ");
   scanf("%c", &c);
  }
 } while (c == 'y' || c == 'Y');
}
void display()
{
 struct stack *temp;
 temp = start;
 while (temp != NULL)
 {
  printf("\n%d", temp->val);
  temp = temp->ptr;
 }
}

Post a Comment