0% found this document useful (0 votes)
23 views23 pages

Stack

Uploaded by

Aamir Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views23 pages

Stack

Uploaded by

Aamir Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

STACK

Terminology: Push ,Pop, display ,Top


content
• What is Stack?
• Stack working principle
What is a Stack?

• Stack is a linear data structure in which the


insertion and deletion operations are
performed at only one end.
• In a stack, adding and removing of elements
are performed at a single position which is
known as "top".
• That means, a new element is added at top of
the stack and an element is removed from the
top of the stack.
Principle

LIFO(Last In First Out)


Stack Examples

top 4 50

3 40

2 30

1 20

0 10
CD-BOX

IDLY PLATES
Operations on a Stack

• Push (To insert an element on to the stack)

• Pop (To delete an element from the stack)

• Display (To display elements of the stack)


Push (To insert an element on to the stack)

Initial Top=-1
SIZE=5
4

TOP -1
Stack
Push (To insert an element on to the stack)
Algorithm
• Step 1 - Check whether stack is FULL.
(top == SIZE-1)
• Step 2 - If it is FULL, then display
"Stack is FULL!!! Insertion is not
possible!!!“ and terminate the function.
Push()
Push(60)
TOP=4 ,i.e. Stack is FULL
top 4 50

3 40

2 30

1 20

0 10

stack
Push()
• Step 3 - If it is NOT FULL, then
increment top value by one (top++)
and set stack[top] to value 4

(stack[top] = value). 3

1
TOP=-1 ,i.e. Stack is empty
0

TOP -1
Push (To insert an element on to the stack)
If (top == SIZE-1)
-1==4 1==4 2==4 3==4
0==4
4 4
4 4 4 top 50

40
3 3
3 3 3 40
top
2 30 2 30
2 2
top 2
30
20 20 20
1 1
top1 20 1 1

0 10 10 10 0 10
top 0 0 0
10

Push(40) Push(50)
Push(10) Push(20) Push(30)
Push()
Push(60) If (top == SIZE-1)
TOP=4 ,i.e. Stack is FULL Insertion not possible
top 4 50

3 40

2 30

1 20

0 10
Pop (To delete an element from the stack) Algorithm

• Step 1 - Check whether stack is EMPTY.


(top == -1)
• Step 2 - If it is EMPTY, then display "Stack is
EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Pop()
TOP=-1 ,i.e. Stack is empty

TOP -1
Pop()
TOP=4 ,i.e. Stack is not empty
Step 3 - If it is NOT EMPTY, then
delete stack[top] and
50
decrement top value by one (top--). top 4

40
3
30
2

20
1
10
0
Pop (To delete an element from the stack)
If (top == -1)
4==-1 3==-1 2==-1 1==-1 0==-1

4 50 4 4
top 4
4
40 40
top 3
3 3 3
3
30 30 2
2 2 2 30
top 2
20 20 20
20 top 1
1 1 1
1
10 10 10 0 10
0 0 0 10
0 top

Pop(30) Pop(20)
Pop(50) Pop(40)
Pop (To delete an element from the stack)

4
4

3
3 Stack is empty
2
2

1
1
0
0

Top=-1 TOP -1
Pop(10)
Display (To display elements of the stack)

• Step 1 - Check whether stack is EMPTY. (top == -


1)
• Step 2 - If it is EMPTY, then display "Stack is
EMPTY!!!" and terminate the function.
• Step 3 - If it is NOT EMPTY, then define a variable 'i'
and initialize with top. Display stack[i] value and
decrement i value by one (i--).
• Step 4 - Repeat above step until i value becomes '0'.
Display()

top 4 50

3 40

2 30

1 20

0 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy