Stack using link list implementation - Coders PlayGround

Learn to code and change the future!

Sunday, 6 August 2017

Stack using link list implementation

Hello Friends,

#include<stdio.h>
#include<stdlib.h>
struct stack
{
 int data;
 struct stack *next;
};
struct stack *top = NULL;
struct stack *getNode()
{
 return (struct stack*)malloc(sizeof(struct stack));
}
void push()
{
 int val;
 struct stack *newnode;
 printf("\n Enter data :");
 scanf("%d",&val);
 newnode = getNode();
 newnode->data = val;
 if(top == NULL)
 {
  top = newnode;
  newnode->next = NULL;
 }
 else
 {
  newnode->next = top;
  top = newnode;
 }
}
void pop()
{
 int x;
 struct stack *del,*temp;
 temp = top;
 if(temp== NULL)
  printf("\n stack underflow");
 else
 {
  x=temp->data;
  top = temp->next;
  free(temp);
  printf("\n Deleted element is %d",x);
 }
}
void peek()
{
 if(top == NULL)
  printf("\n Stack Underflow");
 else
 {
  printf("Peek value : %d \n ",top->data);
 }
}
void disp()
{
 struct stack *temp=top;
 if(top == NULL)
  printf("\n Stack underflow \n");
 else
 {
  printf("\n Stack elements are : ");
  while(temp!=NULL)
  {
   printf("%d \t",temp->data);
   temp = temp->next;
  }
 }
}
void quit()
{
 printf("\n Thank you!!! \n\n");
}
void main()
{
 int ch;
 do{
  printf("\n == MENU ==");
  printf("\n 1. PUSH ");
  printf("\n 2. POP ");
  printf("\n 3. PEEK ");
  printf("\n 4. DISPLAY ");
  printf("\n 5. QUIT");
  printf("\n Enter your choice :");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1 : push();
      break;
   case 2 : pop();
      break;
   case 3 : peek();
      break;
   case 4 : disp();
      break;
   case 5 : quit();
      break;
   default : printf("\n Invalid input");
  }
 }while(ch!=5);
}
/*
OUTPUT : 



 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :50

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :20

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :30

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :40

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :10

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 10  40  30  20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 10 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 10
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 40  30  20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 40
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 30
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 20
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 50 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 50 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 50
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3

 Stack Underflow
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack underflow 

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :5

 Thank you!!! 

*/


PropellerAds