Data Structures and Algorithms Lecture 4 2016 Stacks and... 1/28
1 2 Circular Linked 3 Queue Syntax and Functions in C++ 4 Stacks and... 2/28
FIFO and LIFO Outline 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 insertions and deletions happen at one end of the array or list. A Queue is a First-In-First-Out data structure with both a front and a back. Data is added at the back of the queue and removed at the front. A Dequeue is a more general data structure with both a front and a back. Data can be either added or removed at both the front and the back of the dequeue. Stacks and queues have played an important role in the development of computer science. Stacks and... 3/28
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 Dequeue Array simple, fast, fast, not limited size. limited size. appropriate. Growing fast, fast, used by STL Array not complex, complex to program tricky to program unlimited size unlimited size unlimited size Linked easy not di cult not List slower runtime slower runtime appropriate. unlimited size unlimited size The nature of the application you are implementing may determine which implementation you should use. Stacks and... 4/28
Traditional Stack and Queue Functions There is a long tradition behind the names of the functions used with stacks and queues. Stack empty full top push pop Queue empty full front back enqueue dequeue C++ provides template class implementations that honor some of these names, change some, add alternative names and provide additional functions. Stacks and... 5/28
Circular Linked Circular Linked STL and their functions Stacks and... 6/28
are Familiar Circular Linked The first item inserted in a queue is the first to be processed, and every 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. Stacks and... 7/28
in Computing Circular Linked are central in computer simulations. Events that happen in the simulation are modeled by objects placed in a queue. When they get to the front of the queue, the computer executes the imaginary event. are used in GUI interfaces to manage events. Within a running computer, there are multiple processes and threads that all need to share one (or a few) processing units. The operating system handles these processes by putting them in queues, waiting for a turn at executing on a CPU. Every I/O request ends one turn. When the I/O is finished, the process goes back into the queue, waiting for another turn. Stacks and... 8/28
ACircularQueue-Empty Circular Linked Aqueueimplementedinanarrayiscalledacircular queue because the 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. Queue q; max count front back 8 0 0 0 data???????? [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 9/28
ACircularQueue-1 Circular Linked Here, we push 4 items onto the queue. q.push('a'); q.push('z'); q.push('m'); q.push('p'); max 8 count 4 front back 0 4 data a z m p???? [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 10/28
ACircularQueue-2 Circular Linked Now we pop two and push two. q.pop(); q.push('b'); q.push('c'); q.pop(); max 8 count 4 front 2 back 6 data?? m p b c?? [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 11/28
ACircularQueue-3 Circular Linked Here we see the back position wrap around. q.push('d'); q.push('e'); q.push('f'); max 8 count 7 front 2 back 1 data f? m p b c d e [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 12/28
ACircularQueue-4 Circular Linked Here we see the front position wrap around. q.pop(); q.pop(); q.pop(); q.pop(); q.pop(); q.push('g'); q.push('h'); q.pop(); q.pop(); max 8 count 2 front 1 back 3 data? g h? [0] [1] [2] [3]???? [4] [5] [6] [7] Stacks and... 13/28
ALinkedQueue Empty Circular Linked 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 back pointers in an empty list. Queue q; front count back 0 ~ nullptr Stacks and... 14/28
ALinkedQueue-1 Outline Circular Linked A linked queue after four pushes. q.push('a'); q.push('z'); q.push('m'); q.push('p'); ~ front count back 4 a z m p nullptr Stacks and... 15/28
Pop One and Push One. Circular Linked q.pop(); pop() must free this cell: q.push('b'); a nullptr ~ front count back 4 z m p b nullptr Stacks and... 16/28
ALinkedQueue-3 Outline Circular Linked The queue grows and shrinks easily by relinking cells. q.push('c'); q.pop(); q.pop(); q.pop(); front count back 2 ~ b c nullptr Stacks and... 17/28
Queue Syntax and Functions in C++ STL Classes Overview of STL STL Queue Implementations Stacks and... 18/28
Stack and Queue Implementations Queue Syntax and Functions in C++ 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 are debugged, ready to use, and very adaptable. However, because this is a data structures class, I will sometimes ask you to build your own. In this way, you will better understand how data structures work. Stacks and... 19/28
Stack and Queue Functions in C++ Queue Syntax and Functions in C++ C++ template classes exist for many data structures, including Stack and Queue. These functions are defined: Stack Queue (constructor) (constructor) empty empty size size top front back push push emplace emplace pop pop swap swap Stacks and... 20/28
queue<bt> Outline Queue Syntax and Functions in C++ queue is an adapter class; it is an interface wrapped around another STL class. The underlying class can be a vector<bt>, a list<bt> or a deque<bt>. A deque (double-ended queue) is the default. (First form of the constructor, below). To make a queue with a di erent underlying container, use the second form of the constructor. queue<int> qu1; // Empty queue using deque queue<int, list<int>> qu2; // Empty queue using list Stacks and... 21/28
queue<bt> Functions Queue Syntax and Functions in C++ 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 Stacks and... 22/28
Program 4: Bucket Sort. Due March 1 A bucket sort 1 is the fastest sort for a vary large number of data items 2. It is an O(n) sort that works by using groups of bits from the sort keys rather than by comparing key values to each other. The bucket sort described here is applicable to any large data set, but the presentation is specific to the one you will be using. In this assignment, you will implement an array of queues. You will build your own queue class, not use the C++ STL queue. 1 This technique has been in use since the days of punched cards. 2 Google s map-reduce is based on a bucket sort. Stacks and... 23/28
The Data to Sort Outline 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 of time/temperature measurements, recorded in January at an apartment in Tuckahoe, New York, arranged by time. There will be three files, one from each of three sensors on di erent sides of the building. You only need to process one of 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 data item installed in a new cell. The cells must each be linked onto the end of a queue called the master queue. Stacks and... 24/28
Sorting by Temperature. Data cells will be distributed into buckets. An array of 256 buckets must be created. Each bucket is a linked queue, initially empty. When the data and buckets are ready, sorting begins. It will work in four passes (numbered 0, 1, 2, and 3), where each pass consists of a distribute operation (next slide) and a collect operation (slide 26). After distributing all the data into the buckets. the queues from the buckets must be collected and joined back into a single master queue. At the end of four passes, the data will again be in the master queue, and will be sorted. Output the sorted data to a file. Stacks and... 25/28
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 a subscript to select a bucket. Isolate that byte using shifting and 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. Stacks and... 26/28
Isolating one byte. Outline Define mask = 0xff; to isolate one byte (8 bits). Start with the temperature value from the cell you are distributing. Compute subscript = mask & (temp >> n) where n is 8 k, andk is the pass number. The computed subscript is the number of the bucket to use for this data item. Detach the cell from the master queue and push it onto the selected bucket queue. In this process, the actual sort key must not be changed. Stacks and... 27/28
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, through number 255. If the bucket is not empty, Attach the last cell of the master queue to the first non-dummy cell in the bucket. Set the back pointer of the master list equal to the back pointer of the bucket. All the cells have now been removed from the bucket, so reset it to empty. Stacks and... 28/28