0% found this document useful (0 votes)
96 views

L7b - Queue ADT

The document discusses queues as a first-in first-out (FIFO) data structure. It provides an overview of queues and their operations like enqueue, dequeue, and get front. It then covers two common implementations of queues - using arrays and linked lists. For arrays, it describes using a circular array approach to make both enqueue and dequeue efficient. For linked lists, it suggests using a circular doubly linked list with head and tail pointers to efficiently support both operations. Finally, it briefly mentions the queue implementation in the C++ STL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

L7b - Queue ADT

The document discusses queues as a first-in first-out (FIFO) data structure. It provides an overview of queues and their operations like enqueue, dequeue, and get front. It then covers two common implementations of queues - using arrays and linked lists. For arrays, it describes using a circular array approach to make both enqueue and dequeue efficient. For linked lists, it suggests using a circular doubly linked list with head and tail pointers to efficiently support both operations. Finally, it briefly mentions the queue implementation in the C++ STL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Lecture 7b

Queue ADT

A First-In-First-Out Data Structure


Lecture Overview
 Queue
 Introduction
 Specification
 Implementations
 Array Based
 Linked List Based
 Application
 Palindrome checking

[ CS1020E AY1516S2 Lecture 7b ]


2
What is a Queue
 Real life examples
 A queue for movie tickets, Airline reservation queue,
etc.

 First item added will be the first item to be


removed
 Has the First In First Out (FIFO) property

 Major Operations
 Enqueue: Items are added to the back of the queue
 Dequeue: Items are removed from the front of the
queue
 Get Front: Take a look at the first item
[ CS1020E AY1516S2 Lecture 7b ]
3
Queue: Illustration

A queue of Enqueue a new


Dequeue a person from
3 persons person to the back
the front of the queue
of the queue

[ CS1020E AY1516S2 Lecture 7b ]


4
Queue ADT: C++ Specification
template<typename T>
class Queue {
public:
Queue();

bool isEmpty() const;


int size() const;

void enqueue(const T& newItem);


void dequeue();
void getFront(T& queueTop) const;

private:
// Implementation dependant
// See subsequent implementation slides
};

[ CS1020E AY1516S2 Lecture 7b ]


5
Queue ADT: Design Considerations
 How about the common choices?
 Efficiency of array based implementation
 Removing item at the head is the worst case
 Adding item at the back is the best case

 Efficiency of singly linked list implementation


 Removing item at the head is the best case
 Adding item at the back is the worst case

 Is it possible to have both efficient


enqueue() and dequeue() operations?

[ CS1020E AY1516S2 Lecture 7b ]


6
Queue ADT using Array
Array Implementation Issues
 Removing item from the front is inefficient
 Shifting items is too expensive
 Basic Idea
 The reason for shifting is
 Front is assumed to be at index 0
 Instead of shifting items
 Shift the front index
 So, we have two indices
 Front: index of the queue front
 Back: index of the queue back

[ CS1020E AY1516S2 Lecture 7b ]


8
Incorrect Implementation
 At the beginning, with 4 items queued

 After many queue operations

 The front index will drift to the right,


 Most array locations empty and unusable
[ CS1020E AY1516S2 Lecture 7b ]
9
Circular Array
 Allow both indices to “wrap” back to index 0 when
they reached the end of array
 Effectively making the array “circular”

0 1 2 3 4 5 6 7 8 9

front A B C D E F G

back
Visualized as a
circular array 9 0 front
8 1
A
B
C 2
// Advancing front and back index
7
front = (front+1) % maxsize;
G D back = (back+1) % maxsize;
back F E 3
6
4
[ CS1020E AY1516S2 Lecture 7b ]
10
Queue ADT (Array): C++ Specification
const int MAX_QUEUE = 50; // here is the main problem of array
// our queue cannot be that large

template<typename T>
class Queue {
public:
Queue();
bool isEmpty() const;
int size() const;

void enqueue(const T& newItem);


void dequeue();
void getFront(T& queueFront) const;

private:
T _items[MAX_QUEUE];
int _front, _back, _count;
};
QueueA.h

[ CS1020E AY1516S2 Lecture 7b ]


11
Implement Queue ADT (Array): 1/2
#include <string>
using namespace std;
const int MAX_QUEUE = 50;
template<typename T>
class QueueA {
public:
QueueA() {
_front = 0;
_back = MAX_QUEUE-1;
_count = 0;
}
bool isEmpty() const { return _count == 0; }
int size() const { return _count; }
void enqueue(const T& newItem) {
if (_count == MAX_QUEUE)
throw string("Queue is full");
else {
_back = (_back+1) % MAX_QUEUE;
_items[_back] = newItem;
++_count;
}
}
QueueA.h, expanded
[ CS1020E AY1516S2 Lecture 7b ]
12
Implement Queue ADT (Array): 2/2
void dequeue() {
if (isEmpty())
throw string("Queue is empty");
else {
_front = (_front+1) % MAX_QUEUE;
--_count;
}
}

void getFront(T& queueFront) const {


if (isEmpty())
throw string("Queue is empty");
else
queueFront = _items[_front];
}
QueueA.h, expanded

[ CS1020E AY1516S2 Lecture 7b ]


13
Queue ADT using
Modified Linked List

Conceptual Discussion Only


Improving the Singly Linked List
 Singly linked list performs badly for
enqueue()
 Need to traverse all the way to the last node
 Takes longer time as the queue grows
 How to avoid the traversal to the last node?
 Easy: Just need to “know” where the last node is
all the time
 Solutions
 Keep an additional pointer to the last node, OR
 Circular linked last with a tail pointer

[ CS1020E AY1516S2 Lecture 7b ]


15
Linked List: with “head” and “tail”

Queue Queue
Front Back

[ CS1020E AY1516S2 Lecture 7b ]


16
Circular Linked List

Queue Queue
Front Back

 Only keep track of lastNode (tail) pointer


 firstNode pointer can be set when needed
 firstNode = lastNode->next;
 Will use circular linked list for subsequent
discussion
[ CS1020E AY1516S2 Lecture 7b ]
17
Queue ADT: C++ Specification
template<typename T>
class Queue {
public:
Queue();
~Queue();

bool isEmpty() const;


int size() const;

void enqueue(const T& newItem);


void dequeue();
void getFront(T& queueTop) const;

private:
struct QueueNode { Just like a ListNode
T item; structure, yes we can use
QueueNode *next; inheritance but ListLL.h in
}; Lecture6 is an SLL
int _size;
QueueNode *_lastNode;
};

QueueLL.h
[ CS1020E AY1516S2 Lecture 7b ]
18
Insertion: Non-Empty Queue

Step 1:
newNode = new QueueNode;
newNode->next = lastNode->next;
newNode->item = 3;
This value is just
Step 2: an example only
lastNode->next = newNode;

Step 3:
lastNode = newNode;
[ CS1020E AY1516S2 Lecture 7b ]
19
Insertion: Empty Queue

Step (a):
newNode = new QueueNode;
newNode->item = 3;
Set up the “loop”
Step (b):
newNode->next = newNode;
lastNode = newNode;

[ CS1020E AY1516S2 Lecture 7b ]


20
Deletion: Queue size larger than one

Step 1:
QueueNode* firstNode = lastNode->next;

Step 2:
lastNode->next = firstNode->next;
delete firstNode;

[ CS1020E AY1516S2 Lecture 7b ]


21
Deletion: Queue size equal to one?

firstNode firstNode

Step 1:
QueueNode* firstNode = lastNode->next;

Step 2:
lastNode = null;
delete firstNode;

[ CS1020E AY1516S2 Lecture 7b ]


22
STL queue

You should have guessed it by now


STL has a built-in queue ADT
http://en.cppreference.com/w/cpp/container/queue
STL queue: Specification
template <class T>
class queue {
public:
bool empty() const;
size_type size() const;

We can see both


T& front(); front and back
T& back();
enqueue() is known as
push() in STL Queue
void push(const T& t);
void pop();
This is the dequeue()
}; equivalence

[ CS1020E AY1516S2 Lecture 7b ]


24
VisuAlgo
 http://visualgo.net/list?mode=Queue
 I use Tailed Linked List approach instead of
Circular Linked List but the idea is the same

[ CS1020E AY1516S2 Lecture 7b ]


25
Queue Application

Checking for Palindrome


Palindrome: Problem Description
 Palindrome is a string which reads the same
either left to right, or right to left
 Palindromes: “r a d a r” and “d e e d”

 Counter Examples (most random strings): “d a t a”

 Many solutions
 But for the sake of discussion, let’s use the two newly
learned ADTs
 Highlight the difference of LIFO and FIFO property

 Main Idea
 Use stack to reverse the input
 Use queue to preserve the input
 The two sequence should be the same for palindrome
[ CS1020E AY1516S2 Lecture 7b ]
27
Palindrome: Implementation
#include <queue>
#include <stack>
using namespace std;

bool palindrome(string input) {


stack<char> s ;
queue<char> q ;

for (int j = 0; j < input.size(); j++) {


s.push(input[j]);
Push the same character into
q.push(input[j]);
both queue and stack
}
while (!s.empty()) {
if (s.top() != q.front()) Queue has the original
return false; sequence, Stack has the
s.pop(); reversed. Compare to make
q.pop(); sure they are the same
}
return true;
}

[ CS1020E AY1516S2 Lecture 7b ]


28
Summary
Palindrome
Applications
Checking

Uses Uses

Stack ADT Queue ADT


Queue
pop() enqueue()
(First In First Out)
push() dequeue()
top() front()

Implements Implements

Circular Circular Implementations

Array Linked List


[ CS1020E AY1516S2 Lecture 7b ]
29

You might also like