0% found this document useful (0 votes)
13 views

2.1 Stack Using Array & Linked List

Stacks and array for kids
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

2.1 Stack Using Array & Linked List

Stacks and array for kids
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

STAC

K Data
Structure
Whatis a STACK
? A stack is a container of elements that are
inserted and removed according to the last-in
first-out (LIFO) principle.

A stack is a ordered list of elements of same data


type

A stack is a Linear list


Whatis a STACK
?
In a stack all
operation like
insertion and deletion
0 1 2 3 4
are performed at
only one end called
Top
Whatis a STACK
? Insertio Deletio
n n

In a stack all 4 To
p
operation like
insertion and deletion 3
are performed at 2
only one end called
1
Top
0
STACK ADT
□ TOP pointer It will always point to the last element inserted in the stack.
For empty stack, top will be pointing to -1 (TOP = -1)
□ PUSH: It is the process of inserting a new element at the Top of the stack
□ POP: It is the process of deleting the Top element of the stack
□ PEEK: Viewing the Nth element from the TOP of the stack
□ TRAVERSE: Visiting elements from position from TOP to 0
□ Stack Overflow: An Attempt to insert an element X when the stack is Full,
is said to be stack overflow
□ Stack Underflow: An Attempt to delete an element when the stack is
empty, is said to be stack underflow
Stack Operations using
array
Algorithm for Push
Operation
Step 1 − IF TOP=MAX-1
PRINT
“OVERFLOW

” GOTO

STEP 4

[END OF IF]
Step 2 − SET TOP=TOP+1
Step 3 − SET
STACK[TOP]=VALUE
Algorithm for
POP
Operati
It is the process of deleting the Top element of the stack
on
It takes only one parameter Pop(X). The element X to be deleted
of the Stack. Before deleting the Top element of the stack,
from the Top
check for Empty Stack

If the Stack is Empty, deletion is not possible.


Otherwise, delete the Top element from the Stack & then
decrement the Top pointer by 1
Algorithm for
POP Operation
Step 1: IF TOP=NULL
PRINT “UNDERFLOW”
GOTO STEP 4
[END OF IF]
Step 2: SET
VAL=STACK[TOP]
Step 3: SET TOP=TOP-1
Step 4: END
Routine for different
operations on
Creatio STACK using array
n

Insertion

Deletion
Operations
on STACK ?
#define 4
Creatio
SIZE
n 5 int 3
Insertio
n stack[SIZE]; 2
Deletio
n 1
Displayin
g 0
stac
k
Operations on
STACK ? Insertion operation is called
as “push”
void 4
Creatio
push(element)
n { 3
Insertio if(Top=MAX-1)
n {
2
Deletio printf(“FULL!!!”);
n 1
}
Displayin else
0
g {
Top++; stack[Top] = stac
element; k
Operations on
STACK ? Deletion operation is called
“p o p ”
as i n t p op( ){ 4
Creatio if(Top==-1)
n { 3
Insertio printf(“EMPTY!!!
n ”); return Top; 2
Deletio }
n else 1
Displayin {
deleted = 0
g
stack[Top]; Top--;
stac
return deleted; k
Operations
on STACK ? void display( 4
Creatio ){
if(Top==- 1)
n 3
Insertio {
printf(“EMPTY!!!”); 2
n }
Deletio else
n {
1
Displayin for(i=Top; i>-1; i--)
0
g printf(“%d\n”,stack[i
]); stac
} k
Algorithm for
Stack
Eopme
Initially Stack is
n
pratytio
With Empty stack TOP pointer points to –1
Empty

It is necessary to check for Empty Stack before deleting (pop) an element


from the stack

Routine to check whether stack is


empty
int IsEmpty ( Stack S ) {
if( Top = = - 1 )

return(1);
}
Aoplgeorraitiom for

F
Stack u l l
As we kee p ins e rting the elements, the Stack
the eleme
gets filled nwith
nts With Full stack TOP pointer points to N–1

where N is the size of the stack

• Hence it is necessary to check whether the stack is full


or not before inserting a
• new element into the stack

• Routine to check whether a stack is full


int IsFull ( Stack S ) { if( Top = = Arraysize – 1 )

return(1);
More
o n
Operations
PEEK (Stack, i) – PEE K re tur ns S at the i location from the
the value th

TOP of the Stack. The element can only be viewed. It cannot be deleted
ta ck
TRAVERSE (Stack) – TRAVERSE is the process of visiting each and every data
inside the data structure only once. In case of Stack from TOP (index of recently
added data) to 0th index RETURNTOP (Stack) – Returns the Topmost element of
Stack
Algorithm for Peek Operation

Step 1: IF TOP=NULL

PRINT “STACK IS
EMPTY” GOTO STEP 3
Step 2: RETURN STACK[TOP]
Stack using
array-Animation
Stack using Linked
List
• A linked list supports all the 3 operations: Push,
Pop & Peek
• PUSH OPERATION:
• The push operation is used to insert an element into
the stack.
• The new element is added at the top most position
of the stack.
(Contd)

•ALGORIT
• HM:
Step 1: Allocate memory for the new node &
name it as NEW_NODE.
• Step 2: SET NEW_NODE->DATA=VAL
• Step 3: IF TOP=NULL
• SET NEW_NODE->NEXT=NULL
• SET TOP=NEW_NODE
• ELSE
• SET NEW_NODE->NEXT=TOP
• SET TOP=NEW_NODE
• [END OF IF]
• Step 4: END
(Contd)

…Explanation:
• In step 1, memory is allocated for the new node.

• In step 2, the DATA part of the new node is initialized with the value to be stored in
the node.
• In step 3, we check if the new node is the first node of the linked list . This is done by
checking if TOP=NULL.

• In case, the IF statement evaluates to true, the NULL is stored in the NEXT part of the
node and the new node is called TOP.

• However, if the new node is not the 1st node in the list, then it is added before the 1st
of the list(ie;the top node) & termed as TOP.
Algorithm for POP operation
using
• The pop operation is used to delete theLinked Listfrom a stack.
top most element
• However, before deleting the value, we must 1st check if TOP=NULL, because
if this is the case, then it means that the stack is empty, and no more
deletions can be done.
• If an attempt is made to delete a value from a stack that is already empty,
an “UNDERFLOW” message is printed.
(Contd)

• ALGORITHM:

• Step 1: IF TOP=NULL
• PRINT
“UNDERFLOW”
• Goto Step 5
• [END OF IF]
• Step 2: SET PTR=TOP
• Step 3: SET TOP=TOP-
>NEXT
• Step 4: FREE PTR
• Step 5: END
(Contd)

• Explanation:

• In step1, we 1st check for the “UNDERFLOW” condition.

• In step 2, we use a pointer PTR that points to TOP.

• In step 3, TOP is made to point to the next node in


sequence.

• In step 4, the memory is occupied by PTR is given back


to the free pool.
Application
s of Stack
The following are some of the applications
of stack:
1. Conversion of Infix to Postfix
Expression
2. Evaluating the Postfix Expression
3. Balancing the Symbols
4. Function Call
5. Tower of Hanoi
6. 8 Queen Problem
7. Recursion

You might also like