Top Banner
Outline Overview: Linear Data Structures Queues C++ Template Class Functions Homework Data Structures and Algorithms Alice E. Fischer Lecture 4 – 2016 Alice E. Fischer Data Structures L6, Restricted Linear Data Structures: Stacks and Queues. . . 1/28
28

Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

Apr 25, 2018

Download

Documents

dinhkiet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Data Structures and Algorithms

Alice E. Fischer

Lecture 4 – 2016

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 1/28

Page 2: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

Outline

Overview:LinearDataStructures

Queu

esC++

Tem

plate

Class

Functions

Hom

ework

1Overview:LinearDataStructures

2Queues

CircularQueues

LinkedQueues

3C++

Tem

plateClass

Fun

ctions

Queue

SyntaxandFun

ctions

inC++

4Hom

ework

AliceE.Fisch

erDataStructuresL6,

RestrictedLinearDataStructures:

Stacksan

dQueu

es...

2/28

Page 3: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

FIFO and LIFO

Data structures can be specialized versions of the general array,growing array, or linked list.

A Stack is a Last-In-First-Out data structure: all insertionsand deletions happen at one end of the array or list.

A Queue is a First-In-First-Out data structure with both afront and a back. Data is added at the back of the queueand removed at the front.

A Dequeue is a more general data structure with both afront and a back. Data can be either added or removed atboth the front and the back of the dequeue.

Stacks and queues have played an important role in thedevelopment of computer science.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 3/28

Page 4: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Stack and Queue Implementations

Implementation can be an array, a growing array, or a linked list.Each implementation has its own and advantages / disadvantages.

Stack Queue DequeueArray simple, fast, fast, not

limited size. limited size. appropriate.Growing fast, fast, used by STLArray not complex, complex to program tricky to program

unlimited size unlimited size unlimited sizeLinked easy not di�cult notList slower runtime slower runtime appropriate.

unlimited size unlimited size

The nature of the application you are implementing may determinewhich implementation you should use.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 4/28

Page 5: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Traditional Stack and Queue Functions

There is a long tradition behind the names of the functions usedwith stacks and queues.

Stack

empty

full

top

push

pop

Queue

empty

full

front

back

enqueue

dequeue

C++ provides template class implementations that honor some ofthese names, change some, add alternative names and provideadditional functions.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 5/28

Page 6: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

Queues

Circular QueuesLinked Queues

STL Queues and their functions

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 6/28

Page 7: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

Queues are Familiar

The first item inserted in a queue is the first to be processed, andevery item is processed in order.

We “queue up” for service in banks, grocery stores, airports,etc.

FIFO order is consistent with our idea of fairness.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 7/28

Page 8: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

Queues in Computing

Queues are central in computer simulations. Events thathappen in the simulation are modeled by objects placed in aqueue. When they get to the front of the queue, thecomputer “executes” the imaginary event.

Queues are used in GUI interfaces to manage events.

Within a running computer, there are multiple processes andthreads that all need to share one (or a few) processing units.The operating system handles these processes by putting themin queues, waiting for a turn at executing on a CPU. EveryI/O request ends one turn. When the I/O is finished, theprocess goes back into the queue, waiting for another turn.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 8/28

Page 9: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Circular Queue - Empty

A queue implemented in an array is called a circular queue becausethe part of the array that is filled with date moves toward the end,then circles back to the beginning of the array.Here, we push 4 items onto the queue.

?

0count0front

data ? ? ?[0] [1] [2] [3] [4] [5] [6] [7]

Queue q;

????0back

8max

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 9/28

Page 10: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Circular Queue - 1

Here, we push 4 items onto the queue.

a

4count0front

data z m p[0] [1] [2] [3] [4] [5] [6] [7]

q.push('a'); q.push('z'); q.push('m'); q.push('p');

????4back

8max

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 10/28

Page 11: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Circular Queue - 2

Now we pop two and push two.

q.pop(); q.push('b'); q.push('c'); q.pop();

?

4count2front

data ? m p[0] [1] [2] [3] [4] [5] [6] [7]

??cb6back

8max

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 11/28

Page 12: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Circular Queue - 3

Here we see the back position wrap around.

q.push('d'); q.push('e'); q.push('f');

f

7count2front

data ? m p[0] [1] [2] [3] [4] [5] [6] [7]

edcb1back

8max

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 12/28

Page 13: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Circular Queue - 4

Here we see the front position wrap around.

?

2count1front

data g h ?[0] [1] [2] [3]

3back

8max

[4] [5] [6] [7]????

q.pop(); q.pop(); q.pop(); q.pop(); q.pop(); q.push('g'); q.push('h'); q.pop(); q.pop();

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 13/28

Page 14: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Linked Queue – Empty

An empty linked queue has a dummy header cell.

This eliminates special cases and makes programming easier.

The dummy cell gives a place to anchor front and backpointers in an empty list.

nullptr~0countfront

back

Queue q;

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 14/28

Page 15: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Linked Queue - 1

A linked queue after four pushes.

a4count

front

q.push('a'); q.push('z'); q.push('m'); q.push('p');

z

m

nullptrp

back

~

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 15/28

Page 16: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

Pop One and Push One.

nullptraq.pop(); pop() must free this cell:q.push('b');

z

4countfront

m

p

nullptrb

back

~

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 16/28

Page 17: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Circular QueuesLinked Queues

A Linked Queue - 3

The queue grows and shrinks easily by relinking cells.

2countfront

q.pop(); q.pop(); q.pop();

back

q.push('c');

nullptrc

b

~

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 17/28

Page 18: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Queue Syntax and Functions in C++

STL Classes

Overview of STLSTL Queue Implementations

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 18/28

Page 19: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Queue Syntax and Functions in C++

Stack and Queue Implementations

When using a data structure, there are two approaches:

Build your own.

Use one of the template classes (STL) provided by C++.

The second choice is normally better. STL containers aredebugged, ready to use, and very adaptable.

However, because this is a data structures class, I will sometimesask you to build your own. In this way, you will better understandhow data structures work.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 19/28

Page 20: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Queue Syntax and Functions in C++

Stack and Queue Functions in C++

C++ template classes exist for many data structures, includingStack and Queue. These functions are defined:

Stack

(constructor)

empty

size

top

push

emplace

pop

swap

Queue

(constructor)

empty

size

front

back

push

emplace

pop

swap

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 20/28

Page 21: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Queue Syntax and Functions in C++

queue<BT>

queue is an adapter class; it is an interface wrapped aroundanother STL class.

The underlying class can be a vector<BT>, a list<BT> or adeque<BT>.

A deque (double-ended queue) is the default. (First form ofthe constructor, below).

To make a queue with a di↵erent underlying container, usethe second form of the constructor.

queue<int> qu1; // Empty queue using dequequeue<int, list<int>> qu2; // Empty queue using list

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 21/28

Page 22: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Queue Syntax and Functions in C++

queue<BT> Functions

Examples of use are given here. Prototypes are at cplusplus.com

while (!qu2.empty()) ... // Result is type bool

unsigned n = qu2.size(); // # of elements in container

BT temp = qu2.front( ); // Refers to oldest item in qu2

BT temp = qu2.back( ); // Refers to newest item in qu2

qu2.push( item ); // Insert item after back

qu2.emplace( BT( ) ); // Construct and push an item

qu2.pop(); // Remove item at front. Return void

swap( qu1, qu2); // Swap two entire queues

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 22/28

Page 23: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Program 4: Bucket Sort.

Due March 1

A bucket sort1 is the fastest sort for a vary large number ofdata items2. It is an O(n) sort that works by using groups ofbits from the sort keys rather than by comparing key values toeach other.

The bucket sort described here is applicable to any large dataset, but the presentation is specific to the one you will beusing.

In this assignment, you will implement an array of queues. Youwill build your own queue class, not use the C++ STL queue.

1This technique has been in use since the days of punched cards.

2Google’s map-reduce is based on a bucket sort.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 23/28

Page 24: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

The Data to Sort

We will be sorting objects with two data fields: (1) the time(type time t), a 4-byte unsigned integer. (2) the temperature,a 4-byte float.

The data file will consist of a very large number oftime/temperature measurements, recorded in January at anapartment in Tuckahoe, New York, arranged by time.

There will be three files, one from each of three sensors ondi↵erent sides of the building. You only need to process oneof the files, but di↵erent people should choose di↵erent files.Handle file opening and EOF properly.

First, all of the data must be read from a file, and each dataitem installed in a new cell. The cells must each be linkedonto the end of a queue called the master queue.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 24/28

Page 25: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Sorting by Temperature.

Data cells will be distributed into buckets.

An array of 256 buckets must be created. Each bucket is alinked queue, initially empty.

When the data and buckets are ready, sorting begins. It willwork in four passes (numbered 0, 1, 2, and 3), where eachpass consists of a distribute operation (next slide) and acollect operation (slide 26) .

After distributing all the data into the buckets. the queuesfrom the buckets must be collected and joined back into asingle master queue.

At the end of four passes, the data will again be in the masterqueue, and will be sorted. Output the sorted data to a file.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 25/28

Page 26: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Distributing Data into Buckets

Distribute each cell into a bucket as follows:

Remove it from the front of the master queue.

Access one byte of the temperature and use the result as asubscript to select a bucket. Isolate that byte using shiftingand masking (next slide).

In pass k , use the kth byte from the right.

Attach the cell to the end of the queue in that bucket.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 26/28

Page 27: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Isolating one byte.

Define mask = 0xff; to isolate one byte (8 bits).

Start with the temperature value from the cell you aredistributing.

Compute subscript = mask & (temp >> n)

where n is 8 ⇤ k , and k is the pass number.

The computed subscript is the number of the bucket to usefor this data item.

Detach the cell from the master queue and push it onto theselected bucket queue.

In this process, the actual sort key must not be changed.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 27/28

Page 28: Data Structures and Algorithms - University of New …eliza.newhaven.edu/datastrG/attach/L5-Queues.pdfOutline Overview: Linear Data Structures Queues C++ Template Class Functions Homework

OutlineOverview: Linear Data Structures

QueuesC++ Template Class Functions

Homework

Collecting the Data into a Master List

At the end of each distribution pass, your master queue is empty.Now collect all the cells, as follows:

Find the first non-empty bucket, (k), in the bucket array.

Start at bucket k and process buckets, in order, throughnumber 255.

If the bucket is not empty,Attach the last cell of the master queue to the firstnon-dummy cell in the bucket.Set the back pointer of the master list equal to the backpointer of the bucket.All the cells have now been removed from the bucket, so resetit to empty.

Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 28/28