Download presentation
Presentation is loading. Please wait.
2
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms Lecture 5: Deque Comparison of sequence containers Deque Applications considerations Comparison of sequence containers -- By Rossella Lau
3
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Deque Doubly-ended-queue allows objects to be added to the front or back efficiently compromises the advantages and disadvantages of list and vector An example of deque: Deque.h a linked list of blocks which are arrays (or vectors) ……
4
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 The example structure of DequeBlock DequeBlock similar to Node use of a static array is to avoid unwanted array re-size in STL’s vector template class DequeBlock { T elm[BLOCK_SIZE]; friend class Deque ; };
5
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 The example structure of Deque use STL’s list which is a doubly linked list length stores the number of elements in Deque indexInHead and IndexInTail store the first position of the first block and the last index of the last block template class Deque { list > blockList; size_t length; size_t indexInHead; size_t indexInTail; };
6
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Constraints of Deque Middle blocks are full The first block usually stores data at its last part while the last block usually stores data at its beginning part ……
7
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Adding elements into deque push_back() …… push_back() …… push_front()
8
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Identifying an element in deque …… [0][1][1+ BLOCK_SIZE -1] posInBlock() for deque[i] = i – [sizeInHead() + (previous blocks – 1) * BLOCK_SIZE ] dequeBlock[pos] is posInBlock() and its corresponding deque [i] = sizeInHead() + (n-2) * BLOCK_SIZE + posInBlock() - 1
9
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Exercises on using Deque Assume that a deque has the structures as in Deque.h and the BLOCK_SIZE is 3 (instead of 10 as in the program). Depict the diagrams, or write down the answer, after each set of the following operations is performed: push_back("a"), push_back("b"), push_back("c"), push_back("d"), push_front("e"), push_front("f"), push_front("g"), push_front("h"), push_back("i") identify the value of deque[5], the block number of the value located, and the array index of the block pop_back(), pop_front(), pop_front() identify the value of deque[5], the block number of the value located, and the array index of the block. pop_back(), pop_back(), pop_front(), pop_front(), pop_front()
10
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Another implementation of deque Collin’s 5:82 : An array (map) of blocks with pointers
11
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 The iterator class Collin’s 5:85-86: first, last, current, node
12
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Sequence containers Vector, list, and deque are sequence containers deque is a compromise of vector and list Creation on demand for a block to save the overhead of creation for each node Shift operations involve only a block or at most two blocks for Collin’s structure Allow random access and thus allow for efficient search It also has the disadvantages of vector and list but they are limited to a lower degree with some space and computation overhead
13
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Container for order re-visit Will “order” use deque as its container better? Use vector A small vector causes resizing for “long” orders A large vector results in wasting space for “short” orders Using list solves the above problem but creation for each node is not efficient Use deque The one in Deque.h avoids resizing the array by simply creating another block and each block can be defined as a “short” block The one in Collin’s is similar with more flexibility in insertion/removal but more space overhead
14
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Application considerations A typical order system in a restaurant usually includes 2 containers: Menu: All the dishes provided in the restaurant. Typical contents of each item are the dish name and its price. Table Order: All the dishes ordered by a table/customer. Typical contents of a Table Order includes the table id or a customer name and all the orders of the table. Each order includes at least the dish name and the quantity. Before analysis, assumptions should be made for Types of operations and their frequency
15
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Analysis of using which containers Define (Make the assumptions) the attributes of the containers in the following areas: Elements adding frequency Elements removing frequency Search frequency Update frequency
16
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Comparisons – Storage DequeVector (or array)List For Collin’s structure, some space in the front/back blocks is wasted Unused slots are wastedEfficient because of create on demand Overhead is required for links or the class of Iterator No overhead for each element A pointer is the overhead of each element
17
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Comparisons – Adding/Removing elements DequeVector (or array)List Efficient for both adding/removing at the end or at the front Efficient when appending while there is still room Adding to the front causes lots of operations fast when adding an element in any position if the position is set Insert involves fewer “shift” operations than vector that depend on the size of a block for Collin’s structure Insert may involve a lot of shift operations which depend on number of data Insert/remove causes only a fixed number of operations
18
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Comparisons – Access DequeVector (or array)List Random accessRandom assessNo random access Can apply Binary Search Can apply Binary searchCan only apply linear search Traversal is in between Vector and List Traversal is simple and efficient since data are stored contiguous Traversal is not complicated but a bit slower than vector
19
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Comparisons – Memory allocation DequeVector (or array)List Actions are required when a block is full Actions are only required when the array needs to be re-sized Actions are required for each add/delete operation on a list Size of a new block is fixed and usually much smaller than the one in vector Size of a new block depends on number of data Size of a new node is fixed and usually the size is small No additional action is required for creation of a new block Copying of the original elements is required No additional action is required for creation of a new node
20
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 No appropriate container? It seems that it is difficult to find a suitable container for “order” Possible application re-design? Consider an "Order" in a local super market, it only allows for an item to be added to an order and there is no modification. Although an order may consist of more than one identical item, the only “append” operation simplifies the requirements in choosing an efficient container for the application
21
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Summary Two typical deque structures are studied: a list of vector and a map of blocks Deque compromises the advantages and disadvantages of linked list and array and uses a node to store an array to reduce the times of using new/delete There is not a sequence container that can always be the best for the frequent operations: add, remove, update, and search
22
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 Reference Ford: 6, 9.1-4, Collin: 5.4 STL online references http://www.sgi.com/tech/stl http://www.sgi.com/tech/stl http://www.cppreference.com/ http://www.cppreference.com/ Example programs: Deque.h (v.8) -- END --
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.