l7b - queue adtstevenha/cs1020e... · microsoft powerpoint - l7b - queue adt author: steven created...

29
Lecture 7b Queue ADT A First-In-First-Out Data Structure

Upload: others

Post on 29-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Lecture 7bQueue ADT

A First-In-First-Out Data Structure

Page 2: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Lecture Overview

Queue Introduction

Specification

Implementations Array Based

[ CS1020E AY1516S2 Lecture 7b ] 2

Array Based

Linked List Based

Application Palindrome checking

Page 3: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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

[ CS1020E AY1516S2 Lecture 7b ] 3

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

Page 4: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue: Illustration

[ CS1020E AY1516S2 Lecture 7b ] 4

A queue of 3 persons

Enqueue a new person to the back

of the queue

Dequeue a person from the front of the queue

Page 5: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue ADT: C++ Specificationtemplate<typename T>class Queue {public:Queue();

bool isEmpty() const;int size() const;

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

[ CS1020E AY1516S2 Lecture 7b ] 5

void getFront(T& queueTop) const;

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

};

Page 6: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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

[ CS1020E AY1516S2 Lecture 7b ] 6

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?

Page 7: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue ADT using Array

Page 8: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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

[ CS1020E AY1516S2 Lecture 7b ] 8

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

Page 9: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Incorrect Implementation At the beginning, with 4 items queued

After many queue operations

[ CS1020E AY1516S2 Lecture 7b ] 9

After many queue operations

The front index will drift to the right, Most array locations empty and unusable

Page 10: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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 7 8 92 3 4 5 6

A B C D E F Gfront

back

[ CS1020E AY1516S2 Lecture 7b ] 10

back

// Advancing front and back indexfront = (front+1) % maxsize;back = (back+1) % maxsize;

front

back

AB

C

DEF

G

0

1

7

8

9

2

3

46

Visualized as a circular array

Page 11: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue ADT (Array): C++ Specificationconst 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;

[ CS1020E AY1516S2 Lecture 7b ] 11

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

private:T _items[MAX_QUEUE];

int _front, _back, _count;}; QueueA.h

Page 12: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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;

[ CS1020E AY1516S2 Lecture 7b ] 12

}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

Page 13: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Implement Queue ADT (Array): 2/2

void dequeue() {if (isEmpty())

throw string("Queue is empty");else {

_front = (_front+1) % MAX_QUEUE;--_count;

}}

[ CS1020E AY1516S2 Lecture 7b ] 13

void getFront(T& queueFront) const {if (isEmpty())

throw string("Queue is empty");else

queueFront = _items[_front];}

QueueA.h, expanded

Page 14: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue ADT using Modified Linked List

Conceptual Discussion Only

Page 15: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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?

[ CS1020E AY1516S2 Lecture 7b ] 15

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

Page 16: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Linked List: with “head” and “tail”

[ CS1020E AY1516S2 Lecture 7b ] 16

Queue Front

Queue Back

Page 17: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Circular Linked List

Queue Front

Queue Back

[ CS1020E AY1516S2 Lecture 7b ] 17

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

Page 18: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue ADT: C++ Specificationtemplate<typename T>class Queue {public:Queue();~Queue();

bool isEmpty() const;int size() const;

void enqueue(const T& newItem);

[ CS1020E AY1516S2 Lecture 7b ] 18

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

private:struct QueueNode {T item;QueueNode *next;

};int _size;QueueNode *_lastNode;

};

Just like a ListNodestructure, yes we can use inheritance but ListLL.h in

Lecture6 is an SLL

QueueLL.h

Page 19: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Insertion: Non-Empty Queue

[ CS1020E AY1516S2 Lecture 7b ] 19

Step 1:newNode = new QueueNode;newNode->next = lastNode->next;newNode->item = 3;

Step 2:lastNode->next = newNode;

Step 3:lastNode = newNode;

This value is just an example only

Page 20: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Insertion: Empty Queue

[ CS1020E AY1516S2 Lecture 7b ] 20

Step (a):newNode = new QueueNode;newNode->item = 3;

Step (b):newNode->next = newNode;lastNode = newNode;

Set up the “loop”

Page 21: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Deletion: Queue size larger than one

[ CS1020E AY1516S2 Lecture 7b ] 21

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

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

Page 22: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Deletion: Queue size equal to one?

[ CS1020E AY1516S2 Lecture 7b ] 22

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

Step 2:lastNode = null;delete firstNode;

firstNode firstNode

Page 23: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

STL queue

You should have guessed it by now

STL has a built-in queue ADThttp://en.cppreference.com/w/cpp/container/queue

Page 24: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

STL queue: Specificationtemplate <class T>

class queue {

public:

bool empty() const;

size_type size() const;

T& front();

T& back();

We can see both front and back

[ CS1020E AY1516S2 Lecture 7b ] 24

T& back();

void push(const T& t);

void pop();

};

enqueue() is known as push() in STL Queue

This is the dequeue() equivalence

front and back

Page 25: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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

Page 26: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Queue Application

Checking for Palindrome

Page 27: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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 solutionsBut for the sake of discussion, let’s use the two newly

[ CS1020E AY1516S2 Lecture 7b ] 27

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

Page 28: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

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]);

[ CS1020E AY1516S2 Lecture 7b ] 28

s.push(input[j]); q.push(input[j]);

}while (!s.empty()) { if (s.top() != q.front())

return false;s.pop();q.pop();

}return true;

}

Push the same character into both queue and stack

Queue has the original sequence, Stack has the

reversed. Compare to make sure they are the same

Page 29: L7b - Queue ADTstevenha/cs1020e... · Microsoft PowerPoint - L7b - Queue ADT Author: Steven Created Date: 9/28/2016 7:42:40 PM

Summary

Queue ADT

Palindrome Checking

Uses

Applications

Queue

Uses

Stack ADT

[ CS1020E AY1516S2 Lecture 7b ] 29

enqueue()dequeue()front()

CircularArray

CircularLinked List

Implements Implements

Implementations

Queue

(First In First Out)pop()push()top()