Write a C Program to perform Stack operations using linked list

By | 09.04.2017

Stack operations using linked list


Write a C Program to perform Stack operations using linked list. Here’s simple Program to perform Stack operations using linked list in C Programming Language.


What is Stack ?


Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects.


Basic Operations : :


  • push() − Pushing (storing) an element on the stack.
  • pop() − Removing (accessing) an element from the stack.
  • peek() − get the top data element of the stack, without removing it.
  • isFull() − check if stack is full.
  • isEmpty() − check if stack is empty.

Below is the source code for C Program to perform Stack operations using linked list which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to perform Stack operations using linked list */

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

struct node
{
        int info;
        struct node *link;
}*top=NULL;

void push(int item);
int pop();
int peek();
int isEmpty();
void display();

int main()
{
        int choice,item;
        while(1)
        {
                printf("\n1.Push\n");
                printf("2.Pop\n");
                printf("3.Display item at the top\n");
                printf("4.Display all items of the stack\n");
                printf("5.Quit\n");
                printf("\nEnter your choice : ") ;
                scanf("%d", &choice);

                switch(choice)
                {
                case 1:
                        printf("\nEnter the item to be pushed : ");
                        scanf("%d",&item);
                        push(item);
                        break;
                case 2:
                        item=pop();
                        printf("\nPopped item is : %d\n",item);
                        break;
                case 3:
                        printf("\nItem at the top is %d\n",peek());
                        break;
                case 4:
                        display();
                        break;
                case 5:
                        exit(1);
                default :
                        printf("\nWrong choice\n");
                }/*End of switch */
        }/*End of while */
}/*End of main() */

void push(int item)
{
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        if(tmp==NULL)
        {
                printf("\nStack Overflow\n");
                return;
        }
        tmp->info=item;
        tmp->link=top;
        top=tmp;
}/*End of push()*/

int pop()
{
        struct node *tmp;
        int item;
        if( isEmpty() )
        {
                printf("\nStack Underflow\n");
                exit(1);
        }
        tmp=top;
        item=tmp->info;
        top=top->link;
        free(tmp);
        return item;
}/*End of pop()*/

int peek()
{
        if( isEmpty() )
        {
                printf("\nStack Underflow\n");
                exit(1);
        }
        return top->info;
}/*End of peek() */

int isEmpty()
{
        if(top == NULL)
                return 1;
        else
                return 0;
}/*isEmpty()*/


void display()
{
        struct node *ptr;
        ptr=top;
        if(isEmpty())
        {
                printf("\nStack is empty\n");
                return;
        }
        printf("\nStack elements :\n\n");
        while(ptr!=NULL)
        {
                printf(" %d\n",ptr->info);
                ptr=ptr->link;
        }
        printf("\n");
}/*End of display()*/

OUTPUT : :


1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 1

Enter the item to be pushed : 1

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 1

Enter the item to be pushed : 2

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 1

Enter the item to be pushed : 3

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 1

Enter the item to be pushed : 4

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 4

Stack elements :

 4
 3
 2
 1


1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 2

Popped item is : 4

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 2

Popped item is : 3

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 2

Popped item is : 2

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 3

Item at the top is 1

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 2

Popped item is : 1

1.Push
2.Pop
3.Display item at the top
4.Display all items of the stack
5.Quit

Enter your choice : 2

Stack Underflow

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments