This article explains to you how to implement Stack using Linked List.
1. What is a Stack?
ANS.: An Ordered collection of elements using LIFO(Last in First Out) mechanism.
2. Operations performed on the stack?
ANS.: Push (insertion), pop (deletion), peek (Topmost element) & display.
3. Applications of a stack?
ANS.: Infix, Postfix, and Prefix notations, Recursive functions, Function cell, Expression evaluation, etc.
Graphical Representation.
Also, you can use the C compiler to compile a program.
Program for Stack using LL.
Source code.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * addr;
};
struct node * start;
void push(int);
void pop();
void disp();
int main()
{
push(10);
push(20);
push(30);
push(40);
pop();
pop();
pop();
pop();
}
void push(int val)
{
struct node * temp,*i=start,*top ;
temp=(struct node *) malloc (sizeof (struct node));
temp -> data=val;
temp-> addr=NULL;
if(start==NULL)
{
start=temp;
}
else
top->addr=temp;
top=temp;
disp();
}
void disp()
{
struct node * i=start;
printf("\nstackd: ");
while(i!=NULL)
{
printf("%d ",i->data);
i=i->addr;
}
}
void pop()
{
struct node *i=start,*top;
if(start!=NULL)
{
while(i->addr!=top)
{
i=i->addr;
}
i->addr=NULL;
top=i;
if(start==NULL)
{
top=NULL;
}
disp();
}
}
Output.
stackd: 10
stackd: 10 20
stackd: 10 20 30
stackd: 10 20 30 40
stackd: 10 20 30
stackd: 10 20
stackd: 10
Also view – Queue using a linked list.
Thanks for posting this. Keep up the great effort.|