Node-Based Structures Linked Lists: Implementation



Similar documents
Linked Lists: Implementation Sequences in the C++ STL

Sequences in the C++ STL

Binary Heap Algorithms

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

Recursion vs. Iteration Eliminating Recursion

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

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

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

Lecture 12 Doubly Linked Lists (with Recursion)

Analysis of a Search Algorithm

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

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

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

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

10CS35: Data Structures Using C

Unordered Linked Lists

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

DATA STRUCTURES USING C

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

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 {

6. Storage and File Structures

APP INVENTOR. Test Review

Lecture Notes on Binary Search Trees

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

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

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Data Structures Fibonacci Heaps, Amortized Analysis

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:

Chapter 8: Bags and Sets

Universidad Carlos III de Madrid

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

LINKED DATA STRUCTURES

ADTs,, Arrays, Linked Lists

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

Sequential Data Structures

PES Institute of Technology-BSC QUESTION BANK

6. Standard Algorithms

Algorithms and Data Structures

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

CmpSci 187: Programming with Data Structures Spring 2015

Pitfalls of Object Oriented Programming

Record Storage and Primary File Organization

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Triangulation by Ear Clipping

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

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

Physical Data Organization

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.

From Last Time: Remove (Delete) Operation

Algorithms. Margaret M. Fleck. 18 October 2010

Data storage Tree indexes

Ordered Lists and Binary Trees

Data Structure [Question Bank]

Zabin Visram Room CS115 CS126 Searching. Binary Search

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

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

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

C++ Programming Language

C++FA 3.1 OPTIMIZING C++

Operating Systems CSE 410, Spring File Management. Stephen Wagner Michigan State University

Data Structures and Data Manipulation

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

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

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

Linked List as an ADT (cont d.)

Introduction to Object-Oriented Programming

Application of Stacks: Postfix Expressions Calculator (cont d.)

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

Raima Database Manager Version 14.0 In-memory Database Engine

Data Structures and Algorithms Lists

Pseudo code Tutorial and Exercises Teacher s Version

C++ INTERVIEW QUESTIONS

Loop Invariants and Binary Search

Symbol Tables. Introduction

A New Paradigm for Synchronous State Machine Design in Verilog

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

Converting a Number from Decimal to Binary

Lecture Notes on Linear Search

Linked Lists Linked Lists, Queues, and Stacks

Two Parts. Filesystem Interface. Filesystem design. Interface the user sees. Implementing the interface

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

Circular Linked List. Algorithms and Data Structures

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

How to test and debug an ASP.NET application

Parameter Passing in Pascal

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

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Introduction to Data Structures

Persistent Binary Search Trees

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

Programming with Data Structures

Data Structures and Algorithms

DATA STRUCTURE - QUEUE

CompSci-61B, Data Structures Final Exam

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

Data Structures and Algorithms

Final Exam. Route Computation: One reason why link state routing is preferable to distance vector style routing.

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

Transcription:

Linked Lists: Implementation CS 311 Data Structures and Algorithms Lecture Slides Monday, March 30, 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 Node-based structures Implementation Sequences in the C++ STL Stacks Queues 30 Mar 2009 CS 311 Spring 2009 2

Review Exception Safety When a function signals client code that an error has occurred: Are the data left in a usable state? Do we know something about that state? Are resource leaks avoided? These issues are collectively referred to as safety. Basic Guarantee Data remain in a usable state, and resources are never leaked, even in the presence of exceptions. Strong Guarantee If the operation throws an exception, then it makes no changes that are visible to the client code. No-Throw Guarantee The operation never throws an exception. Notes Each guarantee includes the previous one. The Basic Guarantee is the minimum standard for all code. The Strong Guarantee is the one we generally prefer. The No-Throw Guarantee is required in some special situations. 30 Mar 2009 CS 311 Spring 2009 3

Review Smart Arrays: Allocation & Efficiency [1/2] 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. 30 Mar 2009 CS 311 Spring 2009 4

Review Smart Arrays: Allocation & Efficiency [2/2] Fixing Class SmArray Our original design did not allow for efficient insert-at-end. Reallocate-and-copy would happen every time. The revised design had three data members: size, capacity, and the array pointer. Having a capacity member allows us to keep a record of how much memory is allocated. Then we can allocate extra so that we do not need to reallocate every time. Result: amortized-constant-time insert-at-end. 30 Mar 2009 CS 311 Spring 2009 5

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. 30 Mar 2009 CS 311 Spring 2009 6

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. 30 Mar 2009 CS 311 Spring 2009 7

General Introduction Our fundamental building block for data structures has been the array. Items are stored in contiguous memory locations. There is little memory-management overhead. Look-up operations are usually very fast. Operations that require rearrangement (insert, remove, etc.) can be slow. Now we begin looking at data structures built out of nodes. A node is generally a small block of memory that is referenced via a pointer, and which may reference other nodes via pointers. Node-based structures do not necessarily store data in contiguous memory. Memory-management overhead becomes significant. To find a node, we follow a chain of pointers. Look-up can be slow. Operations that require rearrangement can be very fast. 30 Mar 2009 CS 311 Spring 2009 8

General Pictures, Physical vs. Logical When we draw pictures of node-based data structures, the positions of nodes in the picture have nothing to do with their positions in memory. For example, if a structure is stored like this Physical Structure then we might draw it like this: Logical Structure 30 Mar 2009 CS 311 Spring 2009 9

General Storing Nodes in an Array We normally store nodes in separately allocated blocks of memory. However, we might allocate an array of nodes. Replace pointers with array indices, if desired. Both of these have the above logical structure. This is still a node-based data structure. It still has most of the pro s & con s of node-based structures. The main differences involve memory management: who does it & when it gets done. 30 Mar 2009 CS 311 Spring 2009 10

General Classes When we define a class to implement a node-based data structure, we usually need to define several classes: The main container class, representing the structure as a whole. A class representing a node. This might be a private member of the main class. Typically client code does not deal with this class. Possibly an iterator class. Iterators to node-based structures are almost never pointers, because operator++, for example, needs to go to the next logical node, not the next physical memory location. Despite the multiple classes being defined, we are implementing only a single package. Thus, multiple header & source files are generally not necessary. We can make all of these classes friends without breaking encapsulation. 30 Mar 2009 CS 311 Spring 2009 11

General Pointers & Ownership Think of nodes as resources to be owned & managed. Who owns them? Always document ownership (here: in the class invariants). Internal pointers in a node-based structure will usually be owning pointers. A node is typically owned by the node that points to it. Thus, a node s destructor should free all nodes that it points to. This can make destroying a node-based structure easy. Each node is responsible for destroying the nodes it owns. Thus, to destroy the whole structure, all we need to do is destroy the nodes that are not owned by other nodes. And there is usually just one of these. 30 Mar 2009 CS 311 Spring 2009 12

Linked Lists Refresher [1/2] We have already seen one node-based structure: a Linked List. Like an array, a Linked List is a structure for storing a sequence of items. Array 3 1 5 3 5 2 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 3 5 2 NULL pointer These pointers are the only way to find the next data item. Thus, unlike an array, we cannot quickly skip to (say) the 100th item in a Linked List. Nor can we quickly find the previous item. A Linked List is a one-way sequential-access data structure. Thus, its natural iterator is a forward iterator, which has only the ++ operator. More precisely, the above is a Singly Linked List. A related structure is a Doubly Linked List, which also has previous-node pointers. 30 Mar 2009 CS 311 Spring 2009 13

Linked Lists Refresher [2/2] Why not always use (smart) arrays? One important reason: we can often insert and remove much faster with a Linked List. Inserting Inserting an item at a given position in an array is slow-ish. Inserting an item at a given position (think iterator ) in a Linked List is very fast. Example: insert a 7 after the bold node. Removing Removing the item at a given position from an array is also slow-ish. Removing the item at a given position from a Linked List is very fast. We need an iterator to the previous item. Example: Remove the item in the bold node. 1 5 3 1 5 3 7 1 5 3 1 3 30 Mar 2009 CS 311 Spring 2009 14

Linked Lists More Advantages [1/2] 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. 30 Mar 2009 CS 311 Spring 2009 15

Linked Lists More Advantages [2/2] 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 30 Mar 2009 CS 311 Spring 2009 16

Linked Lists Comparison With Arrays [1/4] What is the order of each of the following when using (1) a smart-array implementation of a Sequence, and (2) a Linked-List implementation? Assume good design and good algorithms. Look-up an item by index. Search in a sorted Sequence. Search in an unsorted Sequence. Sort a Sequence. Insert an item at a given position. Remove an item at a given position. Splice part of one Sequence into another. Insert item at beginning of Sequence. Remove item at beginning of Sequence. Insert item at end of Sequence. Remove item at end of Sequence. Traverse a Sequence (iterate through all items, in order). Make a copy of an entire Sequence. Swap two Sequences. What other issues arise when comparing the two data structures? 30 Mar 2009 CS 311 Spring 2009 17

Linked Lists Comparison With Arrays [2/4] 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 30 Mar 2009 CS 311 Spring 2009 18

Linked Lists Comparison With Arrays [3/4] 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. 30 Mar 2009 CS 311 Spring 2009 19

Linked Lists Comparison With Arrays [4/4] The Moral of the Story Two kinds of design decisions affect the efficiency of code: Choice of algorithm. Choice of data structure. The latter often has the greater impact. Again: Use arrays when we want fast look-up/search. Use Linked Lists when we want fast insert & delete (by iterator). But what if I want all of those to be fast? Stick around for the rest of the semester! 30 Mar 2009 CS 311 Spring 2009 20

Linked List Variations Doubly Linked Lists [1/3] The kind of Linked List we have been discussing contains one pointer per node. Thus, it is called a Singly Linked List. Singly Linked List 3 1 5 4 5 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. 30 Mar 2009 CS 311 Spring 2009 21

Linked List Variations Doubly Linked Lists [2/3] Doubly Linked Lists have essentially all the advantages of (Singly) Linked Lists, plus some more. They allow efficient insert/remove at both ends of the list. We can maintain an end-of-list pointer without trouble. They allow efficient insert-before-this-node and remove-this-node. They allow efficient backwards iteration. However Doubly Linked Lists are a little slower than Singly Linked Lists. Constant-time operations remain, but the constant is a little larger. Doubly Linked Lists still cannot do both splice and size efficiently. The Bottom Line 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? The C++ STL has Doubly Linked Lists: the std::list class template. 30 Mar 2009 CS 311 Spring 2009 22

Linked List Variations Doubly Linked Lists [3/3] 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 30 Mar 2009 CS 311 Spring 2009 23

Linked List Variations Circular Linked Lists Using nodes and pointers, we can arrange data structures in just about any way we want. An interesting variation on a Linked List is a Circular Linked List. Here, we arrange our nodes in a circle. Head weak reference Ownership issues get trickier. Destroy the head and nothing else gets destroyed? One (somewhat icky) solution: use a weak reference. 30 Mar 2009 CS 311 Spring 2009 24

Linked Lists: Implementation Two Approaches 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. How would these be different? In particular, what classes might we define in each case? 30 Mar 2009 CS 311 Spring 2009 25

Linked Lists: Implementation TO BE CONTINUED Linked Lists: Implementation will be continued next time. 30 Mar 2009 CS 311 Spring 2009 26