DS-Chapter - 4
DS-Chapter - 4
Data Structures
What is a queue?
• It is an ordered group of homogeneous items of
elements.
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
• The element added first is also removed first
(FIFO: First In, First Out).
Queue Specification
• Definitions: (provided by the user)
– MAX_ITEMS: Max number of items that might be on
the queue
– ItemType: Data type of the items on the queue
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType& item)
Enqueue (ItemType newItem)
• Function: Adds newItem to the rear of the
queue.
• Preconditions: Queue has been initialized
and is not full.
• Postconditions: newItem is at rear of queue.
Dequeue (ItemType& item)
• Function: Removes front item from queue
and returns it in item.
• Preconditions: Queue has been initialized
and is not empty.
• Postconditions: Front element has been
removed from queue and item is a copy of
removed element.
Priority Queue
• Sometimes it is not enough just do FIFO ordering
– may want to give some items a higher priority than other
items
• these should be serviced before lower priority even if they arrived
later
• Two major ways to implement a priority queue
– insert items in a sorted order
• always remove the head
– insert in unordered order and search list on remove
• always add to the tail
– either way, time is O(n)
• either adding data takes time and removing is quick, or
• adding data is quick and removing takes time
Inserting in Order
• Use the very first linked list class shown
– only need to use the add() and removeHead()
methods
• add() method puts things in the list in order
– the compareTo() method (implemented by your
data class) should return a value based on priority
• usually consider lower number a higher priority
– Performance
• O(n) to add
• O(1) to remove
Implementation issues
• Implement the queue as a circular
structure.
• How do we know if a queue is full or
empty?
• Initialization of front and rear.
• Testing for a full or empty queue.
Make front point to the element preceding the front
element in the queue (one memory location will be
wasted).
Initialize front and rear
Queue is empty
now!!
rear == front
Queue Implementation of Linked List
class Queue {
public:
Queue(int size = 10); // constructor
~Queue() { delete [] values; } // destructor
bool IsEmpty(void);
bool IsFull(void);
bool Enqueue(double x);
bool Dequeue(double & x);
void DisplayQueue(void);
private:
int front; // front index
int rear; // rear index
int counter; // number of elements
int maxSize; // size of array queue
double* values; // element array
};
Queue Class
• Attributes of Queue
– front/rear: front/rear index
– counter: number of elements in the queue
– maxSize: capacity of the queue
– values: point to an array which stores elements of the queue
• Operations of Queue
– IsEmpty: return true if queue is empty, return false otherwise
– IsFull: return true if queue is full, return false otherwise
– Enqueue: add an element to the rear of queue
– Dequeue: delete the element at the front of queue
– DisplayQueue: print all the data
Create Queue
•Queue(int size = 10)
– Allocate a queue array of size. By default, size = 10.
– front is set to 0, pointing to the first element of the array
– rear is set to -1. The queue is empty initially.
void Queue::DisplayQueue() {
cout << "front -->";
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << values[(front + i) % maxSize];
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
}
}
Using Queue
int main(void) {
Queue queue(5);
cout << "Enqueue 5 items." << endl;
for (int x = 0; x < 5; x++)
queue.Enqueue(x);
cout << "Now attempting to enqueue again..." << endl;
queue.Enqueue(5);
queue.DisplayQueue();
double value;
queue.Dequeue(value);
cout << "Retrieved element = " << value << endl;
queue.DisplayQueue();
queue.Enqueue(7);
queue.DisplayQueue();
return 0;
}
Various Queues
• Normal queue (FIFO)
• Circular Queue (Normal Queue)
• Double-ended Queue (Deque)
• Priority Queue
Deque
• It is a double-ended queue.
• Items can be inserted and deleted from either ends.
• More versatile data structure than stack or queue.
• E.g. policy-based application (e.g. low priority go
to the end, high go to the front)
• In a case where you want to sort the queue once in
a while, What sorting algorithm will you use?
Priority Queues
• More specialized data structure.
• Similar to Queue, having front and rear.
• Items are removed from the front.
• Items are ordered by key value so that the item
with the lowest key (or highest) is always at the
front.
• Items are inserted in proper position to maintain
the order.
• Let’s discuss complexity
Priority Queue Example
PrioityQ
-maxSize : int
-queueArray [] : long
PriorityQApp
-nItems : int
Interface1 +Queue()
+insert() : void
+remove() : long
+peekMin() : long
+isEmpty() : bool
+isFull() : bool
Priority Queues
• Used in multitasking operating system.
• They are generally represented using
“heap” data structure.
• Insertion runs in O(n) time, deletion in O(1)
time.
Queue overflow
if(!q.IsFull())
q.Enqueue(item);
Queue underflow
if(!q.IsEmpty())
q.Dequeue(item);
Queue Applications
• As with stacks, queues are very common
– networking
• routers queue packets before sending them out
– operating systems
• disk scheduling, pipes, sockets, etc.
– system modeling and simulation
• queueing theory
• Any of these queues can be done as a priority queue
– consider a disk scheduler
• higher priority is given to a job closer to the current position of the
disk head
• next request done is that closest to the current position
Disk Scheduler
• Requests for disk sectors arrive randomly
• Disk requests are completed at a much slower
rate than disk requests arrive
– need to place waiting jobs in a queue
• All requests should be placed in a priority
queue
– jobs closest to the current position get placed
closer to the front of the queue
• When the current job finishes, the next job is
removed from the head of the queue