CPT212-05-Stack Queue List
CPT212-05-Stack Queue List
List
TAN TIEN PING
Stack
A collection of item which is inserted and removed according to LIFO.
Required methods in a stack: push(e), and pop().
Convenient methods in a stack: top(), size(), isEmpty().
Stack in Java library: java.util.Stack.
We can implement stack using array or linked list.
Stack Implemented Using Array
Complexity of Stack methods implemented using array:
Stack Implemented Using Array
Stack Implemented Using Linked
List
Ref: https://en.wikipedia.org/wiki/Linked_list
Queue
Queue is a collection of items inserted and removed according to FIFO.
Required methods in a queue: enqueue(e) / append(e) and dequeue() / remove().
Convenient methods in a queue: first(), size(), and isEmpty().
Queue can be implemented using array or linked list.
Array implementation:
Queue Implemented Using Array
Challenges in implementing queue using array in handling of dequeue().
◦ Remove first element and subsequently updated all other elements in the array by moving them
forward. Complexity: O(n).
◦ Alternatively, use a variable to keep track of the first element of the queue. When, the queue reach the
end, continue to add items to the head of the queue by using mod operator (%).
Queue Implemented Using Array
Complexity:
Stack/Queue Implemented Using
Linked List
Stack/Queue implemented using linked list has the same worst case complexity compared to
stack/queue implemented using array.
Advantage of Linked List implementation: No need to fix a particular size of the stack/queue.
Disadvantage of Linked List implementation:
◦ Use more space for storing references.
◦ Performed more primitive operations compared to array implementation.
Priority Queue
The queue that we described so far uses FIFO policy.
In some situation FIFO is less suitable.
◦ Airlines. Priority given to business class, first class …
◦ Post office. Old people and pregnant woman are served first.
◦ Server. User with higher right is process first.
Since different problem have different way of defining priority and different type of object (e.g.
compare airline and post office), thus this is left for the user to define. More on this later.
Priority Queue
Common method in priority queue:
◦ insert(key, value): insert a new value/object, with a particular key/priority
◦ min(): return the value/object with the top priority. Does not remove the object from queue.
◦ removeMin(): return the value/object with the top priority. Remove the object from queue.
◦ size(): return the size of queue
◦ isEmpty(): return true/false whether the queue is empty.
Priority Queue: Example
In the following example, the key is Integer type, while the value is of type Character.
Priority Queue
There are 2 ways to define comparison between object in Java
◦ Comparable interface: use to define the natural ordering of the object. E.g. for String object it is based
on the alphabetical order, such as a > b > c …
◦ Comparator interface: use to define custom ordering. E.g. for our String, we may want to define the
ordering/priority based length of the string. This is the class which we will normally use. (Note: Strategy
design pattern)
Priority Queue
There are few ways to define priority queue:
◦ Using unsorted list
◦ Using sorted list
◦ Using ordered Tree (not discuss here. It will be discussed in Tree chapter)