0% found this document useful (0 votes)
19 views14 pages

Lecture 7

Uploaded by

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

Lecture 7

Uploaded by

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

04-Jul-23

PROGRAMMING II
Stack

Sherina Sally
D e p a r t m e n t o f I C T,
F a c u l t y o f Te c h n o l o g y,
University of Colombo

Introduction

• A linear data structure

• Insertion and deletion are allowed only at one end

• The last element inserted to the stack will be on top

• Last In First Out (LIFO) – The last element inserted will be removed first

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

1
04-Jul-23

Stack Operations

• Push – Add an element to the stack Pop Push

• Pop – Remove an element from the stack

• Peek – Get the value of the top most element

• Display - Display the elements of the stack

• isFull – Check if the stack is full

• isEmpty – Check if the stack is empty

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Stack Implementation

• Stack can be implemented using,

• array

• linked list

• The topmost element is traced using the pointer TOP

• An empty stack has a value -1 for TOP

• Check if the stack is full, before pushing an element

• Check if the stack is empty, before popping an element

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

2
04-Jul-23

Stack Underflow

• Occurs when an element is popped from an empty stack

• Always check if the stack is empty before the pop operation

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Stack Overflow

• Occurs when the TOP is a value larger than the maximum elements of the stack

• Relates to a stack implemented using arrays

• Always check if the stack is full, before pushing an element

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

3
04-Jul-23

Stack Implementation - Array

• Define a maximum size for the stack

• A stack of fixed size

• Unable to shrink and grow: for large data values

• Implementation is simple

• Elements are not accessed using indices

• A stack is FULL, if the number of elements has reached the maximum size

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Push
• The first element pushed into the stack will be added to the bottom

• If the stack is full, no element can be added Push

• Check before pushing an element

• Change the reference which points to the top most element

• TOP will be incremented by 1

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

4
04-Jul-23

Push … (2)

begin procedure push: stack, data

If stack is not full


top ← top + 1
stack[top] ← data

end procedure

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Pop
• The top most element of the stack will be removed first

• If the stack is empty, no element can be removed Pop

• Check before an element is popped out

• The TOP element is -1

• After the pop operation, change the reference of the top most element

• Decrement the TOP value by 1

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

5
04-Jul-23

Pop … (2)

begin procedure pop: stack


If stack is not empty
data ← stack[top]
top ← top – 1

return data
end procedure
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Peek

• Read the top most element of the stack


Peek
• No element will be added or removed from the stack

top

begin procedure peek: stack


return stack[top]
end procedure
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

6
04-Jul-23

Traverse

• Traverse and display the values of the stack elements, starting


from the TOP

begin procedure traverse: stack pointer


pointer = top
while pointer != -1
print stack[pointer]
pointer ← pointer - 1
end procedure
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Stack Implementation – Linked List

• The elements can be added dynamically

• Add elements at the beginning

• Ability to grow and shrink

• Implementation is complex, compared to arrays

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

7
04-Jul-23

Push
• The head element is pushed first into the stack

• Create a node with a data value and a link pointing to NULL, Push

before pushing into the stack

• After pushing, point the next link of the node to the HEAD node

• The new node is the new HEAD node of the stack

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Push … (2)
void push(int data){

Node node = new Node();


node.data = data;

if (head == NULL){
head=node;
} else{
node.next = head;
head= node;
}
}
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

8
04-Jul-23

Pop
• The top most element of the stack will be removed first : HEAD node

• If the stack is empty, no element can be removed Pop

• HEAD node is NULL HEAD

• After the pop operation, change the HEAD to the next node in the
stack

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Pop … (2)

int pop(){

if (head != NULL){
Node node = head;
head = head.next;
node.next = null;
return node.data;
}else{
return -1;
}
}
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

9
04-Jul-23

Peek

• Reads the value of the HEAD node


Peek

int peek(){
HEAD top

if (head != NULL){
return head.data;
}else{
return -1;
}
}
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Traverse

• Traverse and display the values of the stack elements, starting


from the HEAD node

void traverse(){ HEAD

Node node = head;


while (node.next != NULL){
System.out.println(node.data);
node = node.next;
}
}
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

10
04-Jul-23

Stack Operations Example

Draw the stack indicating the following operations:

 push(A)

 push(B)

 push(C)

 pop(C)

 push(D)

 peek()

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Expression Evaluation

• Arithmetic expressions can be evaluated using a stack

• Infix expression – operator is in between the operands

eg: 23 * 4, 50 / 6

• Prefix expression – operator appears before the operands

eg: + 34 2, + 48 * 6 5

• Postfix expression – operator appears after the operands

eg: 48 6 5 * +
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

11
04-Jul-23

Expression Evaluation …(2)

• Convert the expression to the postfix notation

• Consider the order of precedence of operators

• Perform stack operations to the converted expression

• Push only operands into the stack

• When a operator is identified, pop two elements from the stack

Eg: Evaluate the following expression using a stack

(2 + 3) * 9 / 5
Department of ICT, Faculty of Technology, University of Colombo | [email protected]

Expression Evaluation …(3)

• Postfix notation : 23 + 9 * 5 /

• Perform stack operations to the converted expression:

push(2)
push(3)
3
2

pop(2) (2+3) = 5
pop(3)

push(5)
5

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

12
04-Jul-23

Expression Evaluation …(4)

push(9)

9
5

pop(9) (9*5) = 45
pop(5)

push(result)

45

Expression Evaluation …(5)

push(5)

5
45

pop(5) (45 / 5) = 9
pop(45)

push(result)
9

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

13
04-Jul-23

Exercise

Exercise: Evaluate the following expressions using a stack

1) 78 – 9 * 2

2) 34 + 10 / 5 – 2

Department of ICT, Faculty of Technology, University of Colombo | [email protected]

14

You might also like