Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 6 Queues and Deques.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
CPSC 335 Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
Chapter 11 Indexing and Hashing (2) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
Data Structures Using C++ 2E
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Hash Table indexing and Secondary Storage Hashing.
Memory Management Memory Areas and their use Memory Manager Tasks:
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Rossella Lau Lecture 11, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 11: Queue & Priority Queue  Basic operations.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Generic Programming Using the C++ Standard Template Library.
C++ STL CSCI 3110.
Data structures Abstract data types Java classes for Data structures and ADTs.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
Lecture 7 : Intro. to STL (Standard Template Library)
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
C++ Review STL CONTAINERS.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS4432: Database Systems II
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Standard Template Library
Linked List ADT used to store information in a list
Data Structures Using C++ 2E
Data Structure and Algorithms
Data Structures Using C++ 2E
External Methods Chapter 15 (continued)
Chapter 6 – Queues and Deques
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Recursive Linked List Operations
Chapter 17: Linked Lists.
Lecture 8 : Intro. to STL (Standard Template Library)
STL List.
Linked List Intro CSCE 121.
STL List.
Presentation transcript:

Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque  Applications considerations  Comparison of sequence containers -- By Rossella Lau

Rossella Lau Lecture 5, DCO20105, Semester A, 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) ……

Rossella Lau Lecture 5, DCO20105, Semester A, 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 ; };

Rossella Lau Lecture 5, DCO20105, Semester A, 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; };

Rossella Lau Lecture 5, DCO20105, Semester A, 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 ……

Rossella Lau Lecture 5, DCO20105, Semester A, Adding elements into deque push_back() …… push_back() …… push_front()

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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()

Rossella Lau Lecture 5, DCO20105, Semester A, Another implementation of deque  Collin’s 5:82 : An array (map) of blocks with pointers

Rossella Lau Lecture 5, DCO20105, Semester A, The iterator class  Collin’s 5:85-86: first, last, current, node

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, 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

Rossella Lau Lecture 5, DCO20105, Semester A, Reference  Ford: 6, 9.1-4, Collin: 5.4  STL online references    Example programs: Deque.h (v.8) -- END --