Code Scrappers


Stack Using Linked List C Program


                    #include<stdio.h>
                    #include<stdlib.h>

                    struct node{

                        int data;

                        struct node* link;
                    };

                    struct node* top=NULL;

                    void push();

                    void pop();

                    void display();

                    void main()
                    {
                        int choice;

                        while(1)
                        {
                              printf(" Stack using linked list \n");

                              printf("1.Push\n");

                              printf("2.Pop\n");

                              printf("3.Display\n");

                              printf("4.Exit\n");

                              printf("Enter Your choice\n");

                              scanf("%d",&choice);

                              switch(choice)
                              {
                                  case 1: push();
                                          break;

                                  case 2: pop();
                                          break;

                                  case 3: display();
                                          break;

                                  case 4: exit(0);
                                          break;

                                  default: printf(" Wrong Choice\n")  ;
                              }

                         }
                    }

                    void push()
                    {
                        struct node* temp;

                        temp = (struct node*)malloc(sizeof(struct node));

                        printf(" \nEnter the data to be pushed: ");

                        scanf("%d",&temp->data);

                        temp->link = top;

                        top = temp;

                    }

                    void pop()
                    {
                        struct node* temp;

                        if(top == NULL)
                        {
                            printf(" Stack is empty \n");
                        }

                        else
                        {
                            temp = top;

                            printf("The popped element is %d\n",temp->data);

                            top = top->link;

                            temp->link = NULL;

                            free(temp);
                        }

                    }

                    void display()
                    {
                        struct node* temp;

                        if(top==NULL)
                        {
                            printf("\nstack is empty\n");
                        }

                        else
                        {
                            temp = top;

                            printf("\nelements of the stack are:\n");

                            while(temp != NULL)
                            {

                                printf("%d\n", temp->data);

                                temp = temp->link;
                            }
                        }
                    }


                



Output

      Stack using linked list

      1.Push
      2.Pop
      3.Display
      4.Exit

      Enter Your choice
      1

      Enter the data to be pushed: 10

      Stack using linked list

      1.Push
      2.Pop
      3.Display
      4.Exit

      Enter Your choice
      1

      Enter the data to be pushed: 20

      Stack using linked list
      1.Push
      2.Pop
      3.Display
      4.Exit

      Enter Your choice
      1

      Enter the data to be pushed: 30

      Stack using linked list
      1.Push
      2.Pop
      3.Display
      4.Exit

      Enter Your choice
      3

      elements of the stack are:
      30
      20
      10

      Stack using linked list

      1.Push
      2.Pop
      3.Display
      4.Exit

      Enter Your choice
      2

      The popped element is 30

      Stack using linked list
      1.Push
      2.Pop
      3.Display
      4.Exit

      Enter Your choice
      3

      elements of the stack are:
      20
      10

      Stack using linked list

      1.Push
      2.Pop
      3.Display
      4.Exit
      Enter Your choice
      4