Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]


Write a program to implement the Stack using Linked List.

/*
 File: DSPrgm06.c
 Author: Aditya Saini
 Date: Nov 10, 2019
 Description: Program to implement the Stack using Linked List.
*/

#include <stdio.h>
#define SIZE 20

typedef struct node
{
               int data;
               struct node *next;
}node;

node *start = NULL;
int element_count = 0;

void push (void);
void pop (void);
void display (void);

//Main Function
int main (void)
{
               int choice;

               //Menu
               while (1)
               {
                              printf ("\t***Menu***\n");
                              printf ("1. Push an element\n");
                              printf ("2. Pop an element\n");
                              printf ("3. Display stack\n");
                              printf ("4. Exit\n");
                              printf ("\nChoice: ");
                              scanf ("%d", &choice);

                              switch (choice)
                              {
                                             case 1:
                                                            push ( );
                                                            break;
                                             case 2:
                                                            pop ( );
                                                            break;
                                             case 3:
                                                            display ( );
                                                            break;
                                             case 4:
                                                            return 0;
                                             default:
                                                            printf ("\nError! Wrong Choice.\n");
                              }
               }
};

//Function to push an element into the stack.
void push (void)
{
               node *temp;

               if (element_count > SIZE)
               {
                              printf ("Error! Stack Overflow\n\n");
                              return;
               }

               temp = (node *) malloc (sizeof (node));
               if (temp == NULL)
               {
                              printf ("Error! Memory Full\n\n");
                              return;
               }

               printf ("Input element to push in: ");
               scanf ("%d", &temp -> data);
               temp -> next = start;
               start = temp;
               element_count++;
               printf ("\n");
};

//Function to pop an element from the stack.
void pop (void)
{
               node *temp;

               if (element_count < 1)
               {
                              printf ("Error! Stack Underflow\n\n");
                              return;
               }

               temp = start;
               start = start ->next;
               printf ("%d popped out\n\n", temp -> data);
               element_count--;
               free (temp);
};

//Function to display the elements in the stack.
void display (void)
{
               node *temp;

               if (element_count < 1)
               {
                              printf ("Error! Empty Stack\n\n");
                              return;
               }

               printf ("Stack: ");
               temp = start;
               while (temp != NULL)
               {
                              printf ("\t%d\n", temp -> data);
                              temp = temp -> next;
               }
               printf ("\n");
};

Output
               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 1
Input element to push in: 5

               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 1
Input element to push in: 6


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 1
Input element to push in: 7


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 1
Input element to push in: 8


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 3
Stack:     8
               7
               6
               5


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 2
8 popped out


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 2
7 popped out


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 3
Stack:     6
               5


               ***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit

Choice: 4


No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib