2 Stack Queue
2 Stack Queue
Outline
I. Stack
Stack
Array-based Stack
Stack Implementation
II. Queue
Queue
Priority Queue
Queue Implementation
Stack
Stack Data Structure (1/4)
Stack Data Structure:
Linear data structure
• StackEmptyException
Stack Data Structure (4/4)
Applications:
Sort of nesting (such as parenthese)
• Use a top variable keeps track of the index of the top element
…
S
0 1 2 top
Array-based stack
Array-based Stack (2/2)
Array-based Stack:
Limitations:
…
S
0 1 2 top
Array-based stack may become full
Solution:
Arraylist-based LinkedList-based
Application Implementation
Convert decimal integer number to binary number
• Waiting list
• Multiprogramming
Indirect application:
normal configuration
Q
012 f l
wrapped-around configuration
Q
012 l f
Array-based Queue
Array-based Queue (2/2)
void enqueue(Object x)
class MyQueue { if(isEmpty())
{ protected Node head,tail; head = tail = new Node(x);
else
public MyQueue() { tail.next = new Node(x);
{ head = tail = null; } tail = tail.next;
}
public boolean isEmpty() }
{ return(head==null);}
enqueue
enqueue 11
54
54 11 24 44 78 dequeue
78
24
dequeue
44
54 11 24 44 78 54 11 24 44 78
dequeue dequeue
dequeue