Stack using Linked List C program

Another approach to create the same program
https://marxtudor.com/c-program-implementing-stack-using-a-linked-list/

Implementing stack using a linked list

#include
#include
struct node
{
int info;
struct node *link;
}*top=NULL;
   
void push(int num);
int pop();
void display();

main()
{
    int choice, num;
    while(1)
    {
        printf("n1 Pushn");
        printf("2 Popn");
        printf("3 Displayn");
        printf("4 Exitnn");
        
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("Enter a number to be insertedn");
                scanf("%d",&num);
                push(num);
                break;
            case 2:
                num = pop();
                break;
            case 3:
                 display();
                 break;
           case 4:
                 exit(1);
          default:
                 printf("Invalid Input");
        }
    }
}
void push(int num)
{
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
     if (temp==NULL)
 {
               printf("stack overflown");
               return;
}
 temp->info = num;
 temp->link = top;
 top = temp;   
}

int pop()
{
    struct node *temp;
    int num;
    if (top==NULL)
    {
         printf("underflown");
         return;
    }
    temp=top;
    num = top->info;
    top = top->link;
    free(temp);
    return num;
}

void display()
{
    struct node *p;
        if (top==NULL)
    {
         printf("underflown");
         return;
    }
    printf("n Stack is: n");
    p=top;
    while (p!=NULL)
         {
                   printf("%dn",p->info);
                   p=p->link;
     }
    
}
Previous Post
Stack using linked list
Next Post
C program implementing Stack using a Linked List

Related Posts

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

alert('dsf'); console.log("dsdsdsd");