Stacks
CH BALAKRISHNA
Assistant Professor
Definition
• Stack is a linear data structure.
• Stack can be defined as a collection of
homogeneous elements, where insertion and
deletion operations takes place at only one end
called TOP.
• Stack follows LIFO principle. i.e. Last In First Out
• Stack Operations:- We can perform mainly 3
operations on stack.
1.Push :- Inserting an element into the stack
2.Pop:- Deleting an element from the stack
3.Peep:- Displaying top most element of the stack
Push :-
Inserting an element into
the stack
10
Push :-
Inserting an element into
the stack
20
10
Push :-
Inserting an element into
the stack
30
20
10
Push :-
Inserting an element into
the stack
40
30
20
10
Push :-
Inserting an element into
the stack 50
40
30
20
10
Push :-
Inserting an element into
the stack 50
40
30
Stack is FULL 20
10
It is Called OVERFLOW
Pop:-
Deleting an element from
the stack 50
40
30
20
10
Pop:-
Deleting an element from
the stack
40
30
20
10
Pop:-
Deleting an element from
the stack
30
20
10
Pop:-
Deleting an element from
the stack
20
10
Pop:-
Deleting an element from
the stack
10
Pop:-
Deleting an element from
the stack
Stack is EMPTY
It is Called UNDERFLOW
Peep:-
Displaying top most
element of the stack
40
DISPLAYS 30
20
40 10
Stacks
(Implementation Using Arrays)
Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Definition
• Stack is a linear data structure.
• Stack can be defined as a collection of
homogeneous elements, where insertion and
deletion operations takes place at only one end
called TOP.
• Stack follows LIFO principle. i.e. Last In First Out
• Stack Operations:- We can perform mainly 3
operations on stack.
1.Push :- Inserting an element into the stack
2.Pop:- Deleting an element from the stack
3.Peep:- Displaying top most element of the stack
PUSH()
1. void push( )
2. {
3. if( top==SIZE-1)
4. {
5. printf("\nStack is full"); 4
6. }
7. else 3
8. { stack[5]
2
9. top++;
10. printf("\nEnter the element: "); 1
11. scanf("%d",&ele);
0
12. stack[top]=ele;
13. }
14. }
-1 TOP
PUSH()
1. void push( )
2. {
3. if( top==SIZE-1)
4. {
5. printf("\nStack is full"); 4
6. }
7. else 3
8. { stack[5]
2
9. top++;
10. printf("\nEnter the element: "); 1
11. scanf("%d",&ele);
12. stack[top]=ele;
0 10
13. }
14. }
-1
0 TOP
PUSH()
1. void push( )
2. {
3. if( top==SIZE-1)
4. {
5. printf("\nStack is full"); 4
6. }
7. else 3
8. { stack[5]
2
9. top++;
10. printf("\nEnter the element: "); 1 20
11. scanf("%d",&ele);
12. stack[top]=ele;
0 10
13. }
14. }
1
0 TOP
PUSH()
1. void push( )
2. {
3. if( top==SIZE-1)
4. {
5. printf("\nStack is full"); 4 50
6. }
7. else 3 40
8. { stack[5]
9. top++;
2 30
10. printf("\nEnter the element: "); 1 20
11. scanf("%d",&ele);
12. stack[top]=ele;
0 10
13. }
14. }
4
1 TOP
OVERFLOW Condition
1. void push( )
2. {
3. if( top==SIZE-1)
4. {
5. printf("\nStack is full"); 4 50
6. }
7. else 3 40
8. { stack[5]
9. top++;
2 30
10. printf("\nEnter the element: "); 1 20
11. scanf("%d",&ele);
12. stack[top]=ele;
0 10
13. }
14. }
4 TOP
POP()
1. void pop( )
2. {
3. if( top==-1)
4. {
5. printf("\n Stack is empty");
6. }
7. else
4 50
8. {
9. ele=stack[top];
3 40
10. printf("\nElement to be deleted is 2 30 stack[5]
%d", ele);
11.
12. }
top--; 1 20
13. } 0 10
3
4 TOP
POP()
1. void pop( )
2. {
3. if( top==-1)
4. {
5. printf("\n Stack is empty");
6. }
4
7. else
8. { 3
9. ele=stack[top];
10. printf("\nElement to be deleted is 2 stack[5]
%d", ele);
11. top--; 1
12. }
13. } 0 10
0
-1 TOP
PEEP()
1. void peep( ) 4
2. {
3. if( top == -1) 3
4. printf("\n Stack is empty");
5. else 2 30 stack[5]
6. {
7. ele=stack[top]; 1 20
8. printf("\n The top element of the stack is %d",
9.
ele);
}
0 10
10. }
2 TOP
Display()
1. void display( )
2. {
3. int i;
4. if( top==-1)
5. printf("\n Stack is empty");
6. else 4 50
7. {
8. printf("\nThe elements of the stack are:\
3 40
n");
2 30 stack[5]
9.
10. for(i= top; i>=0; i--)
11. {
1 20
12. printf("%d\n", stack[i]); 0 10
13. }
14. }
15. }
Display()
1. void display( )
2. {
3. int i;
4. if( top==-1)
5. printf("\n Stack is empty");
6. else 4 50
7. {
8. printf("\nThe elements of the stack are:\
3 40
n");
2 30 stack[5]
9.
10. for(i= top; i>=0; i--)
11. {
1 20
12. printf("%d\n", stack[i]); 0 10
13. }
14. }
15. }
Display()
1. void display( )
2. {
3. int i;
4. if( top==-1)
5. printf("\n Stack is empty");
6. else 4 50
7. {
8. printf("\nThe elements of the stack are:\
3 40
n");
2 30 stack[5]
9.
10. for(i= top; i>=0; i--)
11. {
1 20
12. printf("%d\n", stack[i]); 0 10
13. }
14. }
15. }
Display()
1. void display( )
2. {
3. int i;
4. if( top==-1)
5. printf("\n Stack is empty");
6. else 4 50
7. {
8. printf("\nThe elements of the stack are:\
3 40
n");
2 30 stack[5]
9.
10. for(i= top; i>=0; i--)
11. {
1 20
12. printf("%d\n", stack[i]); 0 10
13. }
14. }
15. }
Display()
1. void display( )
2. {
3. int i;
4. if( top==-1)
5. printf("\n Stack is empty");
6. else 4 50
7. {
8. printf("\nThe elements of the stack are:\
3 40
n");
2 30 stack[5]
9.
10. for(i= top; i>=0; i--)
11. {
1 20
12. printf("%d\n", stack[i]); 0 10
13. }
-1
14. }
15. }
Stacks
(Implementation Using Linked
Lists)
Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Definition
• Stack is a linear data structure.
• Stack can be defined as a collection of
homogeneous elements, where insertion and
deletion operations takes place at only one end
called TOP.
• Stack follows LIFO principle. i.e. Last In First Out
• Stack Operations:- We can perform mainly 3
operations on stack.
1.Push :- Inserting an element into the stack
2.Pop:- Deleting an element from the stack
3.Peep:- Displaying top most element of the stack
Linked List
(Definition & Example)
A linked list is a common data structure made of a chain of nodes in
which each node contains a value and a pointer to the next node in
the chain.
Value Pointer
Node
Head
10 20 30 40 50 NULL
Stack Example
Top
NULL 10 20 30 40 50
Value Pointer
Node
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*top;
Push()
{
newNode = (struct node *) malloc ( sizeof (struct
node) );
printf(" Input data for new node ");
scanf(" %d", &num);
newNode->data= num;
newNode->next = top;
top=newNode;
10 NULL 1
}
TOP NULL
Push()
{
newNode = (struct node *) malloc ( sizeof (struct
node) );
printf(" Input data for new node ");
scanf(" %d", &num);
newNode->data= num; 20 2
newNode->next = top;
top=newNode;
TOP 10 NULL 1
}
NULL
Push()
{
newNode = (struct node *) malloc ( sizeof (struct
node) );
printf(" Input data for new node "); 30 3
scanf(" %d", &num);
newNode->data= num; TOP 20 2
newNode->next = top;
top=newNode;
10 NULL 1
}
NULL
Push()
{ 40 4
newNode = (struct node *) malloc ( sizeof (struct
node) );
printf(" Input data for new node "); TOP 30 3
scanf(" %d", &num);
newNode->data= num; 20 2
newNode->next = top;
top=newNode;
10 NULL 1
}
NULL
TOP
display()
{
if(top == NULL) temp 40 4
{
printf(“Stack is Empty”);
}
else
{
30 3
printf("\nThe elements of the stack are: ");
temp = top; 20 2
while(temp != NULL)
{
printf("%d\n", temp->data); 10 NULL 1
temp = temp->next;
}
}
40
The elements of the stack are: NULL
}
TOP
display()
{
if(top == NULL) 40 4
{
printf(“Stack is Empty”);
}
else
{
temp 30 3
printf("\nThe elements of the stack are: ");
temp = top; 20 2
while(temp != NULL)
{
printf("%d\n", temp->data); 10 NULL 1
temp = temp->next;
}
}
40 30
The elements of the stack are: NULL
}
TOP
display()
{
if(top == NULL) 40 4
{
printf(“Stack is Empty”);
}
else
{
30 3
printf("\nThe elements of the stack are: ");
temp = top; temp 20 2
while(temp != NULL)
{
printf("%d\n", temp->data); 10 NULL 1
temp = temp->next;
}
}
40 30 20
The elements of the stack are: NULL
}
TOP
display()
{
if(top == NULL) 40 4
{
printf(“Stack is Empty”);
}
else
{
30 3
printf("\nThe elements of the stack are: ");
temp = top; 20 2
while(temp != NULL)
{
printf("%d\n", temp->data); temp 10 NULL 1
temp = temp->next;
}
}
40 30 20 10
The elements of the stack are: NULL
}
TOP
display()
{
if(top == NULL) 40 4
{
printf(“Stack is Empty”);
}
else
{
30 3
printf("\nThe elements of the stack are: ");
temp = top; 20 2
while(temp != NULL)
{
printf("%d\n", temp->data); 10 NULL 1
temp = temp->next;
}
}
temp NULL
}
temp
Void pop()
{
if(top == NULL) TOP 40 NULL 4
{
printf(“Stack is Empty”);
}
else
{
30 3
temp=top;
ele = top->data;
printf("\nThe element to be deleted is: %d", 20 2
ele);
top = top->next;
temp->next = NULL; 10 NULL 1
}
}
The element to be deleted is:40 NULL
Void peep()
{
if(top == NULL) TOP 40 4
{
printf(“Stack is Empty”);
}
else
{
30 3
printf("The TOP element is: %d", top->data);
}
} 20 2
10 NULL 1
40
The element to be deleted is: NULL