Stack Data Structure
Stack Data Structure
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.
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.
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.
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) );
newNode->data= num;
newNode->next = top;
top=newNode;
10 NULL 1
}
TOP NULL
Push()
{
newNode = (struct node *) malloc ( sizeof (struct
node) );
newNode->data= num; 20 2
newNode->next = top;
top=newNode;
TOP 10 NULL 1
}
NULL
Push()
{
newNode = (struct node *) malloc ( sizeof (struct
node) );
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; 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