Implementation of Stack using linked list in c code

what is stack? Stack is a linear data structure which implements data on last in first
out criteria. Here is a c program to implement stack using linked list.

Implementation of Stack using linked list in c code

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

struct Node
{
    int Data;
    struct Node *next;
}*top;

void popStack()
{
    struct Node *temp, *var=top;
    if(var==top)
    {
        top = top->next;
        free(var);
    }
    else
    printf("\nStack Empty");
}

void push(int value)
{
    struct Node *temp;
    temp=(struct Node *)malloc(sizeof(struct Node));
    temp->Data=value;
    if (top == NULL)
    {
         top=temp;
         top->next=NULL;
    }
    else
    {
        temp->next=top;
        top=temp;
    }
}

void display()
{
     struct Node *var=top;
     if(var!=NULL)
     { 
          printf("\nElements are as:\n");
          while(var!=NULL)
          {
               printf("\t%d\n",var->Data);
               var=var->next;
          } 
     printf("\n");
     }
     else
     printf("\nStack is Empty");
}

int main(int argc, char *argv[])
{
     int i=0;
     top=NULL;
     printf(" \n1. Push to stack");
     printf(" \n2. Pop from Stack");
     printf(" \n3. Display data of Stack");
     printf(" \n4. Exit\n");
     while(1)
     {
          printf(" \nChoose Option: ");
          scanf("%d",&i);
          switch(i)
          {
               case 1:
               {
               int value;
               printf("\nEnter a valueber to push into Stack: ");
               scanf("%d",&value);
               push(value);
               display();
               break;
               }
               case 2:
               {
               popStack();
               display();
               break;
               }
               case 3:
               {
               display();
               break;
               }
               case 4:
               {
               struct Node *temp;
               while(top!=NULL)
               {
                    temp = top->next;
                    free(top);
                    top=temp;
               }
               exit(0);
               } 
               default:
               {
               printf("\nwrong choice for operation");
               }
         }
    }
}

Output of Stack using linked list in c code:



6 thoughts on “Implementation of Stack using linked list in c code”

  1. Sir,
    What do we mean by linked list of stack and linked list of queue?
    I wan to know about it briefly and immediately.

      1. Try this:

        #include
        #include
        struct node
        {
        int key;
        struct node *next;
        };
        void init(struct node **);
        void push(struct node **,int);
        void display(struct node **);
        int pop(struct node **);

        int main()
        {
        struct node *top;
        init(&top);
        int ch=0,x;
        while(1)
        {
        printf(“\n1.Push\n2.Pop\n3.Display\n4.exit\n”);
        printf(“Enter your choice:”);
        scanf(“%d”,&ch);
        switch(ch)
        {
        case 1:printf(“Enter element to push:”);
        scanf(“%d”,&x);
        push(&top,x);
        break;
        case 2:x=pop(&top);
        if(x==(int)NULL)
        printf(“stack is empty\n”);
        else
        printf(“poped element is %d\n”,x);
        break;
        case 3:display(&top);
        break;
        case 4: return 0;
        }
        }
        return 0;
        }
        void init(struct node **n)
        {
        *n=NULL;
        return;
        }
        void display(struct node **n)
        {
        struct node *temp=*n;
        if(*n==NULL)
        {
        printf(“Stack is empty\n”);
        return ;
        }
        printf(“Stack elements are\n”);
        while(temp!=NULL)
        {
        printf(“\t%d\n”,temp->key);
        temp=temp->next;
        }
        printf(“\n”);
        return ;
        }
        void push(struct node **n,int x)
        {
        struct node *temp=(struct node *)malloc(sizeof(struct node));
        temp->key=x;
        temp->next=*n;
        *n=temp;
        return ;
        }
        int pop(struct node **n)
        {
        if(*n==NULL)
        return (int)NULL;
        int x=(*n)->key;
        (*n)=(*n)->next;
        return x;
        }

  2. Great program. Stacks are generally implemented in two ways. The first is using arrays and the second is using linked list.

    The program shown by you is correct and works fine. Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>