top of page
Wave
Cactus%20Plant_edited.jpg

STACK USING SINGLE LINK LIST

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}node;
int main()
{
    node *top=NULL;
    void display();
    node* push();
    node* pop();
    int x;
    do
    {
        printf("----------------------------------------------\n");
        printf("|          STACK USING LINK LIST             |\n");
        printf("----------------------------------------------\n");
        printf("|1.Display stack                             |\n");
        printf("|2.PUSH                                      |\n");
        printf("|3.POP                                       |\n");
        printf("|4.Exit from the program                     |\n");
        printf("----------------------------------------------\n");
        printf("Enter your choice: ");
        scanf("%d",&x);
        printf("\n");
        switch(x)
        {
            case 1:
                display(top);
                break;
            case 2:
                top=push(top);
                break;
            case 3:
                top=pop(top);
                break;
            case 4:    
                exit(0);
            default :
                printf("Wrong choice\n\n");    
            }
    }while(1);
    return 1;
}
void display(node* top)
{
    node *p;
    if(top==NULL)
    {
        printf("Stack is underflow.\n\n");
        return;    
    }
    p=top;
    while(p!=NULL)
    {
        printf("[%d]->",p->data);
        p=p->next;
    }
    printf("\n\n");
}
node* push(node* top)
{
    node *p;
    int x;
    printf("Enter the value which you wand to insert: ");
    scanf("%d",&x);
    p=(node*)malloc(sizeof (node));
    if(p==NULL)
    {
        printf("Stack is overflow.\n\n");
        return top;
    }
    p->data=x;
    p->next=NULL;
    if(top==NULL)
        top=p;
    else
    {
        p->next=top;
        top=p;
    }
    return top;
}
node* pop(node* top)
{
    node *p;
    if(top==NULL)
    {
        printf("Stack is underflow.\n\n");
        return top;
    }
    p=top;
    top=p->next;
    printf("Poped value is %d\n\n",p->data);
    free(p);    
    return top;
}    

Subscribe Form

Thanks for submitting!

  • Facebook
  • YouTube
  • Instagram
  • Twitter

©2020 by Abhisek Midya ( A18 ). Proudly created with Wix.com

bottom of page