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
Embed
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
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
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
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
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
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
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
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
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
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
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
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
Alice E. FischerData Structures L6, Restricted Linear Data Structures:Stacks and Queues. . . 15/28
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
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
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
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
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
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
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
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
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
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
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
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
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