1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure...

49
1 Stacks & Queues CSC212

Transcript of 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure...

Page 1: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

1

Stacks & Queues

CSC212

Page 2: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

2

Stacks & Queues

• Stack: Last In First Out (LIFO).– Used in procedure calls, to compute arithmetic

expressions etc.

• Queue: First In First Out (FIFO).– Used in operating systems, simulations etc.

• Priority Queues: Highest priority item is served first.– Used in operating systems, printer servers etc.

Page 3: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

3

Stack (Linked Implementation)

TOP

Page 4: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

4

ADT Stack: Specification

Elements: The elements are of a variable type <Type>. In a linked implementation an element is placed in a node.

public class Node<T> extends Object {public T data;public Node<T> next;public Node () { data = null; next = null; }public Node (T val) { data = val; next = null; }

}

Page 5: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

5

ADT Stack: Specification

Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element called top.

Domain: the number of elements in the stack is bounded therefore the domain is finite. Type of elements: Stack

Page 6: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

6

ADT Stack: Specification

Operations: 1. Procedure Push (Stack S, Type e)

requires: Stack S is not full. input: Stack S, Type e.results: Element e is added to the stack as its most recently added elements. output: none.

2. Procedure Pop (Stack S, Type e)requires: Stack S is not empty. input: Stack S.results: the most recently arrived element in S is removed and its value assigned to e. output: Type e.

3. Procedure Empty (Stack S, boolean flag)input: Stack S. results: If Stack S is empty then flag is true, otherwise false. output: flag.

Page 7: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

7

ADT Stack: Specification

Operations:

4. Procedure Full (Stack S, Boolean flag).

requires: input: Stack S.

results: If S is full then Full is true, otherwise Full is false. output: flag.

Page 8: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

8

ADT Stack (Linked Implementation)

TOP

DataElement

Pointer

A LinkedImplementation of the Stack.

Page 9: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

9

ADT Stack (Linked Implementation)

public class LinkStack<T> { private Node<T> top; /* Creates a new instance of LinkStack */ public LinkStack() { top = null; } public boolean empty(){ return top == null; }

Page 10: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

10

ADT Stack (Linked Implementation)

public boolean full(){ return false; }public void push(T e){ Node<T> tmp = new Node(e); tmp.next = top; top = tmp; }

Page 11: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

11

ADT Stack (Linked Implementation)

public T pop(){ Node<T> tmp = top; T e = top.data; top = top.next; return e; }}

Page 12: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

12

Stack: Array Implementation

public class ArrayStack<T> { private int maxsize; private int top; private T[] nodes; /** Creates a new instance of ArrayStack */ public ArrayStack(int n) { maxsize = n; top = 0; nodes = (T[]) new Object[n]; }

Page 13: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

13

Stack: Array Implementation

public boolean empty(){ return top == 0; } public boolean full(){ return top == maxsize; }

Page 14: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

14

Stack: Array Implementation

public void push(T e){ nodes[top++] = e; } public T pop(){ return nodes[--top]; }}

Page 15: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

15

Applications of Stacks

• Some applications of stacks are:– Balancing symbols.– Computing or evaluating postfix expressions.– Converting expressions from infix to postfix.

Page 16: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

16

1. Balancing Symbols

• Expressions: mathematical (a + ((b-c)*d)) or programs have delimiters.

begin {S1 S1S2 {begin S2

S3 S3begin }…. S4end }

endend

Page 17: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

17

1. Balancing Symbols

• Delimiters must be balanced. [()] is legal but [(]) illegal.

• A stack can be used to check if the delimiters are balanced.– Read characters from the start of the expression

to the end.– If the token is a starting delimiter, push on to

the stack, if closing delimiter pop the corresponding start delimiter from the stack.

Page 18: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

18

1. Balancing Symbols

– If the stack is empty or if the popped symbol does not correspond to the closing symbol: report error.

– If stack is not empty at the end of file report an error.

Page 19: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

19

2. Postfix Expressions

• Evaluating Postfix Expressions:– Infix expression: 4.99*1.06+5.99+6.99*1.06– Value 18.69 correct parenthesis used.– Value 19.37 incorrect no parenthesis used.– In postfix form, above expression becomes:

4.99 1.06 * 5.99 + 6.99 1.06*+

Advantage: no brackets are needed and a stack can be used to compute the expression.

Page 20: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

20

2. Postfix Expressions

• Example: – infix: 6*(5+((2+3)*8)+3)– postfix: 6 5 2 3 + 8 * + 3 + *.

• Algorithm to compute postfix expression: – Read the postfix expression left to right. When

a number is read push it on the stack; when a operator is read, pop two numbers from the stack and carry out the operation on them, push the result back on the stack.

Page 21: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

21

3. Infix to Postfix Conversion

• A stack can also be used to convert an infix expression to postfix expression. (See handout)

• Example: infix expression

a + b * c + (d * e + f) * g

to postfix expression

a b c * + d e * f + g * +

Page 22: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

22

Queues

Front

Tail

Page 23: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

23

ADT Queue: Specification

Elements: The elements are of a variable type <Type>. In a linked implementation elements are placed in nodes.

public class Node<T> extends Object {public T data;public Node<T> next;

public Node () { data = null; next = null; }

public Node (T val) { data = val; next = null; }

}

Page 24: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

24

ADT Queue: Specification

Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called the tail and least recently arrived element the front or head.

Domain: the number of elements in the queue is bounded therefore the domain is finite. Type of elements: Queue

Page 25: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

25

ADT Queue: Specification

Operations:

1. Procedure Enqueue (Queue Q, Type e)

requires: Queue Q is not full. input: Queue Q, Type e.

results: Element e is added to the queue at its tail. output: none.

2. Procedure Serve (Queue Q, Type e)

requires: Queue Q is not empty. input: Queue Q.

results: the element at the head of Q is removed and its value assigned to e. output: Type e.

3. Procedure Length (Queue Q, int length)

input: Queue Q. results: The number of element in the Queue Q is returned. output: length.

Page 26: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

26

ADT Queue: Specification

Operations:

4. Procedure Full (Queue Q, Boolean flag).

requires: input: Queue Q.

results: If Q is full then flag is set to true, otherwise flag is set to false. output: flag.

Page 27: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

27

ADT Queue (Linked Implementation)

public class LinkQueue <Type> { private Node<Type> head, tail; private int size; /** Creates a new instance of LinkQueue */public LinkQueue() { head = tail = null; size = 0; }

Page 28: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

28

ADT Queue (Linked Implementation)

public boolean full() {

return false; } public int length (){ return size; }

Page 29: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

29

ADT Queue (Linked Implementation)

public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; } size++; }

Page 30: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

30

ADT Queue (Linked Implementation)

public Type serve() { Node<Type> tmp; Type x; tmp = head; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }}

Page 31: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

31

ADT Queue (Array Implementation)

• Array implementation of the queue…a fixed size array is used to store the data elements.

• As data elements are enqueued & served the queue crawls through the array from low to high index values.

• As the queue crawls forward, it also expands and contracts.

Page 32: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

32

ADT Queue (Array Implementation)

Head Tail

Head TailAfter one En-queue and one Serve

Page 33: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

33

ADT Queue (Array Implementation)

Head Tail

Where to En-queue this?

Page 34: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

34

ADT Queue (Array Implementation)

HeadTail

Wrap Round

0 MaxSize-1

Page 35: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

35

ADT Queue (Array Implementation)

0 MaxSize - 1

HeadTail

Page 36: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

36

ADT Queue (Array Implementaion)

public class ArrayQueue <T> { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }

Page 37: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

37

ADT Queue (Array Implementation)

public boolean full () { return size == maxsize ? true : false; } public int length () { return size; } public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1) % maxsize; size++; }

Page 38: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

38

ADT Queue (Array Implementation)

public T serve () { T e = nodes[head]; head = (head + 1) % maxsize; size--; return e; }}

Page 39: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

39

Priority Queue

• Each data element has a priority associated with it.Highest priority item is served first.

• Real World Priority Queues: hospital emergency rooms…most sick patients treated first, events in a computer system, etc.

• Priority Queue can be viewed as:– View 1: Priority queue as an ordered list.

– View 2: Priority queue as a set.

Page 40: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

40

ADT Priority Queue

Specification:Elements: The elements are of type PQNode. Each node has

in it a data element of variable type <Type> and priority of type Priority ( which could be int type).

public class PQNode<T> { private T data; private Priority priority; public PQNode<T> next; public PQNode() { next = null; } public PQNode(T e, Priority p) { data = e; priority = p; }

Page 41: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

41

ADT Priority Queue

Structure: the elements are linearly arranged, and may be ordered according to a priority value, highest priority element is called the tail and least priority element the front or head.

Domain: the number of nodes in the queue is bounded therefore the domain is finite. Type of elements: PriorityQueue

Page 42: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

42

ADT Priority Queue

Operations: 1. Procedure Enqueue (PriorityQueue PQ, Type e, Priority p)

requires: PQ is not full. input: PQ, e.results: Element e is added to the queue according to its priority. output: none.

2. Procedure Serve (PriorityQueue PQ, Type e, Priority p)requires: PQ is not empty. input: PQ.results: the element at the head of PQ is removed and returned. output: e, p.

3. Procedure Length (PriorityQueue PQ, int length)input: PQ. results: The number of element in the PQ is returned. output: length.

Page 43: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

43

ADT Priority Queue

Operations:

4. Procedure Full (PrioriytQueue PQ, Flag flag).

requires: input: PQ.

results: If PQ is full then flag is set to true, otherwise flag is set to false. output: flag.

Page 44: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

44

ADT Priority Queue

el10

el el el el el10 8 7 7 5

Head Tail

Array Implementation

el7

Insert Where?

el10

el10

el8

el7

el5

el7

Head Tail

Linked Implementation

el el

Page 45: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

45

ADT Priority Queue (Linked)

public class LinkPQ<T> { private int size; private PQNode<T> head, tail;

/* tail is of no use here. */ public LinkPQ() { head = tail = null; size = 0; } public int length (){ return size; }

Page 46: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

46

ADT Priority Queue (Linked)

public int length (){ return size; } public boolean full () { return false; }

Page 47: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

47

ADT Priority Queue (Linked)

public void enqueue(T e, int pty) { PQNode<T> p, q, tmp; if ((size == 0) || (pty > head.Priority())) { tmp = new PQNode<T>(e, pty); tmp.next = head; head = tmp; } else { p = head; q = null; while ((p != null) && (p.Priority() > pty)) { q = p; p = p.next; } tmp = new PQNode<T>(e, pty); tmp.next = p; q.next = tmp; } }

Page 48: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

48

ADT Priority Queue (Linked)

public T serve (Priority pty){ T e = head.get_data();

pty.set_value(head.get_priority().get_value()); head = head.next; size--; return(e); }}

Page 49: 1 Stacks & Queues CSC212. 2 Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.

49

ADT Priority Queue

• Implementations– Array Implementation: Enqueue is O(n), Serve

is O(1).– Linked List: Enqueue is O(n), Serve is O(1).– Heap: Enqueue is O(log n), Serve is O(log n)

Heaps to be discussed later.