Linked Lists: Implementation Sequences in the C++ STL



Similar documents
Sequences in the C++ STL

Node-Based Structures Linked Lists: Implementation

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Heap Algorithms

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Recursion vs. Iteration Eliminating Recursion

recursion, O(n), linked lists 6/14

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Why you shouldn't use set (and what you should use instead) Matt Austern

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Universidad Carlos III de Madrid

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Cours de C++ Utilisations des conteneurs

CmpSci 187: Programming with Data Structures Spring 2015

Analysis of a Search Algorithm

10CS35: Data Structures Using C

Data Structures Fibonacci Heaps, Amortized Analysis

Unordered Linked Lists

Chapter 8: Bags and Sets

Algorithms and Data Structures

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Cpt S 223. School of EECS, WSU

Data Structures Using C++ 2E. Chapter 5 Linked Lists

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

6. Standard Algorithms

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Lecture Notes on Linear Search

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

DATA STRUCTURES USING C

ADTs,, Arrays, Linked Lists

APP INVENTOR. Test Review

Loop Invariants and Binary Search

Introduction to Data Structures

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Lecture Notes on Binary Search Trees

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

C++ Programming Language

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Sequential Data Structures

Data Structure [Question Bank]

Symbol Tables. Introduction

C++FA 3.1 OPTIMIZING C++

Computer Science 210: Data Structures. Searching

PES Institute of Technology-BSC QUESTION BANK

Common Data Structures

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Data Structures and Algorithms

Lecture 12 Doubly Linked Lists (with Recursion)

Sources: On the Web: Slides will be available on:

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Linked List as an ADT (cont d.)

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Module 2 Stacks and Queues: Abstract Data Types

Introduction to Programming System Design. CSCI 455x (4 Units)

Splicing Maps and Sets

From Last Time: Remove (Delete) Operation

Algorithms. Margaret M. Fleck. 18 October 2010

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Parameter Passing in Pascal

Data Structures. Topic #12

Persistent Binary Search Trees

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Converting a Number from Decimal to Binary

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

The ADT Binary Search Tree

Binary Search Trees CMPSC 122

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Semantic Analysis: Types and Type Checking

C++ INTERVIEW QUESTIONS

Data Structures. Level 6 C Module Descriptor

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

7.1 Our Current Model

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Data Structures and Data Manipulation

Decimal Notations for Fractions Number and Operations Fractions /4.NF

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Zabin Visram Room CS115 CS126 Searching. Binary Search

Pitfalls of Object Oriented Programming

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Programming with Data Structures

DVR GUIDE. Using your DVR/Multi-Room DVR WAVE-123 wavebroadband.com

DATA STRUCTURE - STACK

Persistent Data Structures

Frequently Asked Questions for logging in to Online Banking

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Transcription:

Linked Lists: Implementation Sequences in the C++ STL continued CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 1, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

Unit Overview Sequences & Their Implementations Major Topics Introduction to Sequences Smart arrays Interface A basic implementation Exception safety Allocation & efficiency Generic containers Linked Lists (part) Node-based structures Implementation Sequences in the C++ STL Stacks Queues 1 Apr 2009 CS 311 Spring 2009 2

Review Smart Arrays: Allocation & Efficiency An operation is amortized constant time if k operations require O(k) time. Thus, over many consecutive operations, the operation averages constant time. Not the same as constant-time average case. Quintessential amortized-constant-time operation: insert-at-end for a well written (smart) array. Amortized constant time is not something we can easily compare with (say) logarithmic time. 1 Apr 2009 CS 311 Spring 2009 3

Review Generic Containers [1/2] A generic container is a container that can hold a client-specified data type. In C++ we usually implement a generic container using a class template. A function that allows exceptions thrown by a client s code to propagate unchanged, is said to be exception-neutral. When exception-neutral code calls a clientprovided function that may throw, it does one of two things: Call the function outside a try block, so that any exceptions terminate our code immediately. Or, call the function inside a try block, then catch all exceptions, do any necessary cleanup, and re-throw. Client code calls Our package calls Implementation of templateparameter type This code might throw and if it does, this code handles the exception. 1 Apr 2009 CS 311 Spring 2009 4

Review Generic Containers [2/2] We can use catch-all, clean-up, re-throw to get both exception safety and exception neutrality. arr = new MyType[10]; try { std::copy(a, a+10, arr); } catch (...) { delete [] arr; throw; } Called outside any try block. If this fails, we exit immediately, throwing an exception. Called inside a try block. If this fails, we need to deallocate the array before exiting. This helps us meet the Basic Guarantee (also the Strong Guarantee if this function does nothing else). This makes our code exception-neutral. 1 Apr 2009 CS 311 Spring 2009 5

Review Node-Based Structures Linked Lists [1/5] Our first node-based data structure is a (Singly) Linked List. A Linked List is composed of nodes. Each has a single data item and a pointer to the next node. Linked List Head Nodes 3 1 5 4 5 2 These pointers are the only way to find the next data item. NULL pointer Once we have found a position within a Linked List, we can insert and delete in constant time. 3 1 5 4 5 2 3 1 5 4 5 2 7 1 Apr 2009 CS 311 Spring 2009 6

Review Node-Based Structures Linked Lists [2/5] Also, with Linked Lists, we can do a fast splice: Before 3 1 5 4 5 2 6 2 1 1 3 3 After 3 1 5 4 5 2 6 2 1 1 3 3 Note: If we allow for efficient splicing, then we cannot efficiently keep track of a Linked List s size. 1 Apr 2009 CS 311 Spring 2009 7

Review Node-Based Structures Linked Lists [3/5] Further, with Linked Lists, iterators, pointers, and references to items will always stay valid and never change what they refer to, as long as the Linked List exists unless we remove or change the item ourselves. Iterator 3 1 5 4 5 2 Remove Iterator 3 5 4 5 2 Insert Iterator 3 5 4 5 2 7 1 Apr 2009 CS 311 Spring 2009 8

Review Node-Based Structures Linked Lists [4/5] Look-up by index Smart Array Linked List *For Singly Linked Lists, we mean inserting or removing just after the given position. Doubly Linked Lists can help. Search sorted Search unsorted Sort Insert @ given pos Remove @ given pos Splice O(log n) O(n log n) O(n log n) * * ** if reallocation occurs. Otherwise,. Amortized constant time. Pre-allocation can help. ***For, need a pointer to the end of the list. Otherwise,. This is tricky. Doubly Linked Lists can help. Insert @ beginning Remove @ beginning Insert @ end or ** amortized const or *** Remove @ end or *** Traverse Copy Swap Find faster with an array Rearrange faster with a Linked List 1 Apr 2009 CS 311 Spring 2009 9

Review Node-Based Structures Linked Lists [5/5] Other Issues Linked Lists use more memory. When order is the same, Linked Lists are almost always slower. Arrays might be 2 10 times faster. Arrays keep consecutive items in nearby memory locations. Many algorithms have the property that when they access a data item, the following accesses are likely to be to the same or nearby items. This property of an algorithm is called locality of reference. Once a memory location is accessed, a memory cache will automatically load nearby memory locations. With an array, these are likely to hold nearby data items. Thus, when a memory cache is used, an array can have a significant speed advantage over a Linked List, when used with an algorithm that has good locality of reference. With an array, iterators, pointers, and references to items can be invalidated by reallocation. Also, insert/remove can change the item they reference. 1 Apr 2009 CS 311 Spring 2009 10

Review Node-Based Structures Linked List Variations [1/2] In a Doubly Linked List, each node has a data item & two pointers: A pointer to the next node. A pointer to the previous node. Doubly Linked List 3 1 5 4 5 End-of-list pointer Doubly Linked Lists often have an end-of-list pointer. This can be efficiently maintained, resulting in constant-time insert and remove at the end. Doubly Linked Lists are generally considered to be a good basis for a general-purpose generic container type. Singly-Linked Lists are not. Remember all those asterisks? 1 Apr 2009 CS 311 Spring 2009 11

Review Node-Based Structures Linked List Variations [2/2] Smart Array Doubly Linked List With Doubly Linked Lists, we can get rid of most of our asterisks. Look-up by index Search sorted Search unsorted Sort O(log n) O(n log n) O(n log n) * if reallocation occurs. Otherwise,. Amortized constant time. Pre-allocation can help. Insert @ given pos Remove @ given pos Splice Insert @ beginning Remove @ beginning Insert @ end or * amortized const Remove @ end Traverse Copy Swap Find faster with an array Rearrange faster with a Linked List 1 Apr 2009 CS 311 Spring 2009 12

Review Linked Lists: Implementation Two approaches to implementing a Linked List: A Linked List package to be used by others. A Linked List as part of some other package, and not exposed to clients. 1 Apr 2009 CS 311 Spring 2009 13

Linked Lists: Implementation Write It continued TO DO Write an insert-at-beginning operation for a Linked List. Done. See linked_list.cpp, on the web page. 1 Apr 2009 CS 311 Spring 2009 14

Sequences in the C++ STL Generic Sequence Types Introduction The C++ STL has four generic Sequence container types. Class template std::vector. A smart array. Much like what we wrote, but with more member functions. Class template std::basic_string. Much like std::vector, but aimed at character string operations. Mostly we use std::string, which is really std::basic_string<char>. Also std::wstring, which is really std::basic_string<std::wchar_t>. Class template std::list. A Doubly Linked List. Note: The Standard does not specify implementation. It specifies the semantics and order of operations. These were written with a Doubly Linked List in mind, and a D.L.L. is the usual implementation. Class template std::deque. Deque stands for Double-Ended QUEue. Say deck. Like std::vector, but a bit slower. Allows fast insert/remove at both beginning and end. 1 Apr 2009 CS 311 Spring 2009 15

Sequences in the C++ STL Generic Sequence Types std::deque [1/4] We are familiar with smart arrays and Linked Lists. How is std::deque implemented? There are two big ideas behind it. First Idea A vector uses an array in which data are stored at the beginning. 0 1 2 3 4 5 This gives linear-time insert/remove at beginning, constant-time remove at end, and, if we do it right, amortized-constant-time insert at end. What if we store data in the middle? When we reallocate-and-copy, we move our data to the middle of the new array. 0 1 2 3 4 5 Result: Amortized-constant-time insert, and constant-time remove, at both ends. 1 Apr 2009 CS 311 Spring 2009 16

Sequences in the C++ STL Generic Sequence Types std::deque [2/4] Second Idea Doing reallocate-and-copy for a vector requires calling either the copy constructor or copy assignment for every data item. For large, complex data items, this can be time-consuming. Instead, let our array be an array of pointers to arrays, so that reallocate-and-copy only needs to move the pointers. This still lets us keep most of the locality-of-reference advantages of an array, when the data items are small. Array of Pointers Arrays of Data Items 0 1 2 3 4 5 6 7 8 1 Apr 2009 CS 311 Spring 2009 17

Sequences in the C++ STL Generic Sequence Types std::deque [3/4] An implementation of std::deque typically uses both of these ideas. It probably uses an array of pointers to arrays. This might go deeper (array of pointers to arrays of pointers to arrays). The arrays may not be filled all the way to the beginning or the end. Reallocate-and-copy moves the data to the middle of the new array of pointers. Base Object Middle is used Array of Pointers 0 1 2 3 4 5 6 7 8 9 Arrays of Data Items Thus, std::deque is an array-ish container, optimized for: Insert/remove at either end. Possibly large, difficult-to-copy data items. The cost is complexity, and a slower [but still ] look-up by index. 1 Apr 2009 CS 311 Spring 2009 18

Sequences in the C++ STL Generic Sequence Types std::deque [4/4] Essentially, std::deque is an array. Iterators are random-access. But it has some complexity to it, so it is a slow-ish array. However, insertions at the beginning do not require items to be moved up. We speed up insert-at-beginning by allocating extra space before existing data. And reallocate-and-copy leaves the data items alone. We also speeds up insertion by trading value-type operations for pointer operations. Pointer operations can be much faster than value-type operations. A std::deque can do reallocate-and-copy using a raw memory copy, with no value-type copy ctor calls. Like vector, deque tends to keep consecutive items in nearby memory locations. So it avoids cache misses when used with algorithms having good locality of reference. The Bottom Line A std::deque is generally a good choice when you need fast insert/remove at both ends of a Sequence. Especially if you also want fast-ish look-up. Some people also recommend std::deque whenever you will be doing a lot of resizing, but do not need fast insert/remove in the middle. 1 Apr 2009 CS 311 Spring 2009 19

Sequences in the C++ STL Generic Sequence Types Efficiency [1/2] We determine efficiency by counting operations. How do we count operations for a generic container type? We count both built-in operations and value-type operations. However, we typically expect that the most time-consuming operations are those on the value type. The C++ Standard, on the other hand, counts only value-type operations. For example, constant time in the Standard means that at most a constant number of value-type operations are performed. 1 Apr 2009 CS 311 Spring 2009 20

Sequences in the C++ STL Generic Sequence Types Efficiency [2/2] vector, basic_string deque list Look-up by index Constant Constant Linear Search sorted Logarithmic Logarithmic Linear Insert @ given pos Linear Linear Constant Remove @ given pos Linear Linear Constant Insert @ beginning Linear Linear/ Amortized Constant* Constant Remove @ beginning Linear Constant Constant Insert @ end Linear/ Amortized Constant** Linear/ Amortized Constant* Constant Remove @ end Constant Constant Constant *Only a constant number of value-type operations are required. The C++ standard counts only value-type operations. Thus, it says that insert at beginning or end of a std::deque is constant time. **Constant time if sufficient memory has already been allocated. All have traverse, copy, and search-unsorted, swap, and O(n log n) sort. 1 Apr 2009 CS 311 Spring 2009 21

Sequences in the C++ STL Generic Sequence Types Common Features All STL Sequence containers have: iterator, const_iterator Iterator types. The latter acts like a pointer-to-const. vector, basic_string, and deque have random-access iterators. list has bidirectional iterators. iterator begin(), iterator end() iterator insert(iterator, item) Insert before. Returns position of new item. iterator erase(iterator) Remove this item. Returns position of next item. push_back(item), pop_back() Insert & remove at the end. reference front(), reference back() Return reference to first, last item. clear() Remove all items. resize(newsize) Change the size of the container. Not the same as vector::reserve, which sets capacity. In addition, deque and list also have: push_front(item), pop_front() Insert & remove at the beginning. In addition, vector, basic_string, and deque also have: reference operator[](index) Look-up by index. In addition, vector also has: reserve(newcapacity) Sets capacity to at least the given value. And there are other members 1 Apr 2009 CS 311 Spring 2009 22

Sequences in the C++ STL Iterator Validity The Idea One of the trickier parts of using container types is making sure you do not use an iterator that has become invalid. Generally, valid iterators are those that can be dereferenced. We also call things like container.end() valid. These are past-the-end iterators. Consider the smart-array class in Assignment 5. When is one of its iterators invalidated? When reallocate-and-copy occurs. When the container is destroyed. When the container is resized so that the iterator is more than one past the end. Now consider a (reasonable) Linked-List class with iterators. When are such iterators invalidated? Only when the item referenced is erased. This includes container destruction. 1 Apr 2009 CS 311 Spring 2009 23

Sequences in the C++ STL Iterator Validity Rules We see that different container types have different iteratorvalidity rules. When using a container, it is important to know the associated rules. A related topic is reference validity. Items in a container can be referred to via iterators, but also via pointers and references. Reference-validity rules indicate when pointers and references remain usable. Often these are the same as the iterator-validity rules, but not always. 1 Apr 2009 CS 311 Spring 2009 24

Sequences in the C++ STL Iterator Validity std::vector For std::vector Reallocate-and-copy invalidates all iterators and references. When there is no reallocation, the Standards says that insertion and erasure invalidate all iterators and references except those before the insertion/erasure. Apparently, the Standard counts an iterator as invalidated if the item it points to changes. A vector can be forced to pre-allocate memory using std::vector::reserve. The amount of pre-allocated memory is the vector s capacity. We have noted that pre-allocation makes insert-at-end a constanttime operation. Now we have another reason to do pre-allocation: preserving iterator and reference validity. 1 Apr 2009 CS 311 Spring 2009 25

Sequences in the C++ STL Iterator Validity std::deque For std::deque Insertion in the middle invalidates all iterators and references. Insertion at either end invalidates all iterators, but no references. Why? Erasure in the middle invalidates all iterators and references. Erasure at the either end invalidates only iterators and references to items erased. So deques have some validity advantages over vectors. 1 Apr 2009 CS 311 Spring 2009 26

Sequences in the C++ STL Iterator Validity std::list For std::list An iterator or reference always remains valid until the item it points to goes away. When the item is erased. When the list is destroyed. In some situations, these validity rules can be a big advantage of std::list. 1 Apr 2009 CS 311 Spring 2009 27

Sequences in the C++ STL Iterator Validity Example // v is a variable of type vector<int> // Insert a 1 before each 2 in v: for (vector<int>::iterator iter = v.begin(); iter!= v.end(); ++iter) { if (*iter == 2) v.insert(iter, 1); } What is wrong with the above code? The insert call invalidates iterator iter. Even if iter stays valid, after an insertion, it points to the 1 inserted. After being incremented, it points to the 2 again. Infinite loop. How can we fix it? Some ideas (most of which were discussed in class): Replace the if body with: iter = v.insert(iter, 1); ++iter;. Use indices in the loop, instead of iterators. Use std::list, instead of std::vector. Pre-allocate using reserve (and increment iter in the if ). 1 Apr 2009 CS 311 Spring 2009 28