0% found this document useful (0 votes)
12 views22 pages

CPT212-05-Stack Queue List

Stack_Queue_List

Uploaded by

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

CPT212-05-Stack Queue List

Stack_Queue_List

Uploaded by

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

Stack, Queue and

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.

Priority queue ADT can be used for this purpose.


A value/object/element is given a key which represent its priority when it is added into the
queue.
Key with a smaller value has higher priority.
Priority Queue
The key can be any object or just simply integer as long as there is a way to compare them
without ambiguity.
To make sure no ambiguity, it must define total order relation.
◦ Comparability property: k1 ≤ k2 or k2 ≤ k1.
◦ Antisymmetric property: if k1 ≤ k2 and k2 ≤ k1, then k1 = k2.
◦ Transitive property: if k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3.

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)

Comparison of the complexity of methods in priority queue implemented using unsorted/sorted


list.
Method Unsorted List Sorted List Heap
size O(1) O(1) O(1)
isEmpty O(1) O(1) O(1)
insert O(1) O(n) O(log n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(1)

You might also like