C program to create stack using linked list with push, pop and display elements on stack functions.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *link;
}*first=NULL;
void push();
void pop();
void disp();
int main()
{
    int choice;
    while(1)
    {
        printf("\nEnter your choice\n");
        printf("1 to push element into stack\n");
        printf("2 to pop element from stack \n");
        printf("3 to display stack elements\n");
        printf("0 to exit\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:push();
                    break;
            case 2:pop();
                    break;
            case 3:disp();
                    break;
            case 0: return 0;
                    break;
            default:printf("Invalid Entry\n");
        }
    }
}
void push()
{
    struct node *new_node;
    new_node=(struct node *)malloc(sizeof(struct node) );
    printf("Enter an element\n");
    scanf("%d",&new_node->data);
    new_node->link=NULL;
    if(first==NULL)
    {
        first=new_node;
    }
    else
    {
        new_node->link=first;
        first=new_node;
    }
}

void pop()
{
    struct node *x;
    if(first==NULL)
        printf("Pop not possible, stack empty!\n");
    else if(first->link==NULL)
    {
        printf("Element poped is %d\n",first->data);
        free(first);
        first=NULL;
    }
    else
    {
        printf("Element deleted is %d\n",first->data);
        x=first;
        first=first->link;
        free(x);
    }
}

void disp()
{
    struct node *temp;
    if(first==NULL)
    {
        printf("Display not possible, stack empty!\n");
    }
    else if(first->link==NULL)
    {
        printf("\nDisplaying elements in stack\n");
        printf("%d\n",first->data);
    }
    else
    {
        temp=first;
        printf("\nDisplaying elements in stack\n");
        while(temp!=NULL)
        {
            printf("%d\n",temp->data);
            temp=temp->link;
        }
        printf("\n");
    }
}

OUTPUT:

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
4
Invalid Entry

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
3
Display not possible, stack empty!

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Pop not possible, stack empty!

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
1
Enter an element
10

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
3

Displaying elements in stack
10

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
1
Enter an element
20

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
1
Enter an element
30

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
1
Enter an element
40

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
1
Enter an element
50

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
3

Displaying elements in stack
50
40
30
20
10


Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Element deleted is 50

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
3

Displaying elements in stack
40
30
20
10


Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Element deleted is 40

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Element deleted is 30

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Element deleted is 20

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
3

Displaying elements in stack
10

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Element poped is 10

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
3
Display not possible, stack empty!

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
2
Pop not possible, stack empty!

Enter your choice
1 to push element into stack
2 to pop element from stack
3 to display stack elements
0 to exit
 0

Popular Posts