Download presentation
Presentation is loading. Please wait.
1
Stacks and Queues 1
2
Stack ADT A stack is a list in which insertions and deletions take place at the same end. This end is called the top of the stack. A stack is know as a LIFO (Last in First Out) list. delete insert Primary operations: push(e) – Add an element e to the top of the stack. pop() – Remove an element from the top of the stack. top() – returns the top element. isEmpty – Returns true if the stack is empty. isFull – Returns true if the stack is full. 2
3
Array Implementation Stack ADT
public class Stack { private int topIndx = -1, capacity = default; private Object S[]; public Stack(int size) { capacity = size; S = new Object[capacity]; } public boolean isEmpty() { return topIndx<0; } public boolean isFull() { return topIndx==capacity-1; } public void push(Object Element) { S[++topIndx] = Element; } public Object pop() { Object Element; Element = S[topIndx]; S[topIndx--] = null; return Element; } public Object top() { return S[topIndx]; } 5 4 topIndx 3 2 1 S
4
Linked Implementation Stack ADT
public class Stack { private Node Top = null; private int size = 0; public boolean isEmpty() { return Top==null; } public boolean isFull() { ??? } public void push(Object Element) { Node Tmp = new Node(Element); Tmp.Next = Top; Top = Tmp; size++; } public Object pop() { Node Tmp = Top; Top = Top.Next; size--; return Tmp.Data; Top Data Next null Element
5
Queue ADT A queue is a list in which insertions take place at one end called the rear of the queue and deletions take place at the other end called the front of the queue. A queue is know as a FIFO (First in First Out) list. delete insert front rear Primary operations: enqueue(e) – Add element e to the rear of the queue. dequeue() – Remove the front element from the queue. first() – returns the front element. isEmpty – Returns true if the queue is empty. isFull – Returns true if the queue is full. 5
6
Array Implementation Queue ADT
public class Queue { private int front = 0, rear = -1, capacity = default; private Object S[]; public Queue(int size) { capacity = size; S = new Object[capacity]; } public boolean isEmpty() { return front==(rear+1)%capacity; } public boolean isFull() { ??? } public void enqueue(Object Element) { rear = (rear+1)%capacity; S[rear] = Element; } public Object dequeue() { Object Element = S[front]; S[front] = null; front = (front+1) % capacity; return Element; front rear 1 2 3 4 5 6 S rear front 1 2 3 4 5 6 Array S is treated as circular (joined at the ends) S
7
Linked Implementation Queue ADT
public class Queue { private Node Rear = null, Front = null; private int size = 0; public boolean isEmpty() { return Front==null; } public void enqueue(Object Element) { Node Tmp = new Node(Element); if (Rear==null) Rear = Front = Tmp; else { Rear.Next = Tmp; Rear = Tmp;} size++; } public Object dequeue() { Node Tmp = Front; Front = Front.Next; size--; if (Front==null) Rear = null; return Tmp.Data; Rear Front Data Next null Element
8
Stacks v.s. Queues a b c c b a a b c a b c
9
Applications of Stacks
Managing run-time memory. Performing “infix” to “postfix” conversions. Performing “postfix” calculations.
10
Algebraic Expressions
Infix notation/Algebraic notation Binary operators appear between operands. a + b Most readable for human. Prefix notation (seldom-used) Binary operators appear before operands. + a b Postfix notation/Reverse Polish Notation Binary operators appear after operands. a b + Easier to process – no need for parentheses nor rules of precedence and associativity.
11
Infix to Postfix Conversion
The algorithm: Initialize the stack with the dummy operator #. Repeat until no more tokens. Get the next token. If the next token is an operand, enqueue the operand to the postfix. If the next token is a (, push ( into the stack. If the next token is a ), pop and enqueue operators to the postfix until a ( is on the top of the stack. Pop ( out of the stack. If the next token is an operator, pop and enqueue operators to the postfix until the top of the stack has a lower precedence. Push the current operator into the stack. If no more tokens, pop and enqueue operators until the dummy opr. # is on the top of the stack. Pop # out of the stack. Note that # and ( has a precedence lower than other operators.
12
Example + - + * * * Infixt: 12 + 23 * 34 + (45 * 56 - 67) * 78
Prece dence *,/ 4 +,- 3 ( 2 # 1 Postfix: + - + 12 23 34 * 45 56 * 67 78 * Stack: ( + # * ( + # - ( + # + # * + # # + # * + # + #
13
Human evaluation of postfix
Going from left to right, if you see an operator, apply it to the previous two operands. Example: * / 40 6 8 10 4
14
Computer evaluation of postfix
Repeat until no more tokens If the next token is a number, push it into the stack. If the next token is an operator pop two operands from the stack, do the operation, and push the result back to the stack. The result is the only thing on the stack when done. (If there’s more than one thing in the stack, there is an error in the expression.) * / 2 10 2 40 2 8 2 10 9 10 6 10 4
15
Homework Use the algorithm on slide 11 to convert the following infix expression to a postfix expression. (( * 34 / 45) * ) / 89 Use the computer evaluation algorithm to evaluate the following postfix expression. * 5 / Describe an algorithm that uses a stack and a queue to test if a character string is a palindrome.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.