Program to Implement Stack using Linked List Notes | EduRev

: Program to Implement Stack using Linked List Notes | EduRev

 Page 1


www.eazynotes.com Gursharan Singh Tatla Page No. 1 
 
STACK USING LINKED LIST 
 
/******    Program to Implement Stack using Linked List    ******/ 
 
#include <stdio.h> 
 
void push(); 
void pop(); 
void display(); 
 
struct node 
{ 
       int info; 
       struct node *link; 
} *top = NULL; 
 
int item; 
 
main() 
{ 
      int ch; 
       
      do 
      { 
       printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n"); 
       printf("\nEnter your choice: "); 
       scanf("%d", &ch); 
 
       switch(ch) 
       { 
                case 1: 
                     push(); 
                     break; 
                      
                case 2: 
                     pop(); 
                     break; 
                      
                case 3: 
                     display(); 
                     break; 
                      
                case 4: 
                     exit(0); 
                      
 
Page 2


www.eazynotes.com Gursharan Singh Tatla Page No. 1 
 
STACK USING LINKED LIST 
 
/******    Program to Implement Stack using Linked List    ******/ 
 
#include <stdio.h> 
 
void push(); 
void pop(); 
void display(); 
 
struct node 
{ 
       int info; 
       struct node *link; 
} *top = NULL; 
 
int item; 
 
main() 
{ 
      int ch; 
       
      do 
      { 
       printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n"); 
       printf("\nEnter your choice: "); 
       scanf("%d", &ch); 
 
       switch(ch) 
       { 
                case 1: 
                     push(); 
                     break; 
                      
                case 2: 
                     pop(); 
                     break; 
                      
                case 3: 
                     display(); 
                     break; 
                      
                case 4: 
                     exit(0); 
                      
 
www.eazynotes.com Gursharan Singh Tatla Page No. 2 
 
                default: 
                     printf("Invalid choice. Please try again.\n"); 
       } 
      } while(1); 
      getch(); 
} 
 
void push() 
{ 
     struct node *ptr; 
 
     printf("\n\nEnter ITEM: "); 
     scanf("%d", &item); 
 
     if (top == NULL) 
     { 
             top = (struct node *)malloc(sizeof(struct node)); 
             top->info = item; 
             top->link = NULL; 
     } 
     else 
     { 
             ptr = top; 
             top = (struct node *)malloc(sizeof(struct node)); 
             top->info = item; 
             top->link = ptr; 
     } 
 
     printf("\nItem inserted: %d\n", item); 
} 
 
void pop() 
{ 
     struct node *ptr; 
 
     if (top == NULL) 
             printf("\n\nStack is empty\n"); 
     else  
     { 
             ptr = top; 
             item = top->info; 
             top = top->link; 
             free(ptr); 
              
             printf("\n\nItem deleted: %d", item); 
     } 
} 
Page 3


www.eazynotes.com Gursharan Singh Tatla Page No. 1 
 
STACK USING LINKED LIST 
 
/******    Program to Implement Stack using Linked List    ******/ 
 
#include <stdio.h> 
 
void push(); 
void pop(); 
void display(); 
 
struct node 
{ 
       int info; 
       struct node *link; 
} *top = NULL; 
 
int item; 
 
main() 
{ 
      int ch; 
       
      do 
      { 
       printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n"); 
       printf("\nEnter your choice: "); 
       scanf("%d", &ch); 
 
       switch(ch) 
       { 
                case 1: 
                     push(); 
                     break; 
                      
                case 2: 
                     pop(); 
                     break; 
                      
                case 3: 
                     display(); 
                     break; 
                      
                case 4: 
                     exit(0); 
                      
 
www.eazynotes.com Gursharan Singh Tatla Page No. 2 
 
                default: 
                     printf("Invalid choice. Please try again.\n"); 
       } 
      } while(1); 
      getch(); 
} 
 
void push() 
{ 
     struct node *ptr; 
 
     printf("\n\nEnter ITEM: "); 
     scanf("%d", &item); 
 
     if (top == NULL) 
     { 
             top = (struct node *)malloc(sizeof(struct node)); 
             top->info = item; 
             top->link = NULL; 
     } 
     else 
     { 
             ptr = top; 
             top = (struct node *)malloc(sizeof(struct node)); 
             top->info = item; 
             top->link = ptr; 
     } 
 
     printf("\nItem inserted: %d\n", item); 
} 
 
void pop() 
{ 
     struct node *ptr; 
 
     if (top == NULL) 
             printf("\n\nStack is empty\n"); 
     else  
     { 
             ptr = top; 
             item = top->info; 
             top = top->link; 
             free(ptr); 
              
             printf("\n\nItem deleted: %d", item); 
     } 
} 
www.eazynotes.com Gursharan Singh Tatla Page No. 3 
 
void display() 
{ 
     struct node *ptr; 
 
     if (top == NULL) 
        printf("\n\nStack is empty\n"); 
     else 
     { 
         ptr = top; 
          
         while(ptr != NULL) 
         { 
                   printf("\n\n%d", ptr->info); 
                   ptr = ptr->link; 
         } 
     } 
} 
Read More
Offer running on EduRev: Apply code STAYHOME200 to get INR 200 off on our premium plan EduRev Infinity!