Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Similar documents
Implementing Linked Lists

Recursive Linked Lists

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Inheritance: The Fundamental Functions

Inheritance: The Fundamental Functions

Friends and Unary Operators

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Lecture 29 Section Wed, Apr 1, 2009

Minimal Spanning Trees

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The Critical-Path Algorithm

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Class Construct Part 1

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

List Iterator Implementation

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Scope and Parameter Passing

Dynamic Allocation of Memory

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Dynamic Allocation of Memory

Rotations and Translations

Scope and Parameter Passing

The Class Construct Part 2

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

The Decreasing-Time Algorithm

Binary Tree Applications

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Sampling Distribution Examples Sections 15.4, 15.5

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Solving Recursive Sequences by Iteration

LR Parsing - Conflicts

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Building the Abstract Syntax Trees

Recursive Descent Parsers

Displaying Distributions - Quantitative Variables

Scheduling and Digraphs

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Density Curves Sections

The Pairwise-Comparison Method

The Traveling Salesman Problem Brute Force Method

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Street-Routing Problems

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The Graphics Pipeline

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Magnification and Minification

Recognition of Tokens

Binary Tree Implementation

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Stacks and their Applications

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

The Coefficient of Determination

Introduction to Compiler Design

Programming Languages

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Programming Languages

The Plurality-with-Elimination Method

Function Definition Syntax Tree

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Nondeterministic Programming in C++

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

XQuery FLOWR Expressions Lecture 35

Binary Tree Implementation

PHP Queries and HTML Forms Lecture 23

The Graphics Pipeline

Fundamental Data Types

Basic CSS Lecture 17

The CYK Parsing Algorithm

The Normal Distribution

Ambient and Diffuse Light

Abstract Syntax Trees Synthetic and Inherited Attributes

The Traveling Salesman Problem Cheapest-Link Algorithm

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

DTDs and XML Attributes

Introduction to Databases

The Projection Matrix

The x86 Instruction Set

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Insertions, Deletions, and Updates

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

CSI33 Data Structures

Transcription:

Linked Lists Lecture 16 Sections 17.1-17.3 Robb T. Koether Hampden-Sydney College Wed, Feb 22, 2017 Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 1 / 24

1 Linked Lists 2 The LinkedListNode Class 3 The LinkedList Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 2 / 24

Outline 1 Linked Lists 2 The LinkedListNode Class 3 The LinkedList Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 3 / 24

Linked Lists In a linked list, each node is allocated dynamically when the element is added to the list and deallocated dynamically when it is removed from the list. A linked list always uses exactly the amount of memory it needs. A linked list is more efficient than an array list in some ways and less efficient in others. The name of the linked list class is LinkedList. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 4 / 24

Linked Lists a 0 a 1 a 2... a n - 1 The abstract concept Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 5 / 24

Linked Lists a 0 a 1 a 2... a n - 1 The linked implementation Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 5 / 24

Outline 1 Linked Lists 2 The LinkedListNode Class 3 The LinkedList Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 6 / 24

The LinkedListNode Class We create a separate class, LinkedListNode, as a data type that stores a single element of a linked list. Each LinkedListNode contains a data item and a pointer that links it to the next node. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 7 / 24

LinkedListNode Data Members LinkedListNode Data Members T m_value; LinkedListNode* m_next; m_value The element a i. m_next A pointer to the node containing the element a i+1 or NULL if a i is the last element. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 8 / 24

LinkedListNode Member Functions LinkedListNode Public Member Functions T value() const; T& value(); LinkedListNode* next(); T value() Returns a copy of the data member of the node (r-value). T& value(); Returns a reference to the data member of the node (l-value). next(); Returns a copy of the pointer to the next node. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 9 / 24

LinkedListNode Member Functions LinkedListNode Private Member Functions LinkedListNode(const T& val = T()); LinkedListNode(T&); Constructs a linked list node containing the specified value. It can be invoked only by friend classes. The only friend class (for now) will be the LinkedList class. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 10 / 24

LinkedListNode Destructor LinkedListNode Destructor ~LinkedListNode(); What should the LinkedListNode destructor do? Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 11 / 24

The LinkedListNode Class The LinkedListNode Class The header file linkedlistnode.h. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 12 / 24

Outline 1 Linked Lists 2 The LinkedListNode Class 3 The LinkedList Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 13 / 24

The LinkedList Class LinkedList Data Members int m_size; LinkedListNode* m_head; m_size The number of elements in the list. m_head A pointer to the first node (which contains the first element). Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 14 / 24

The LinkedListNode Class m_size m_head m_value m_next m_value m_next m_value m_next LinkedList LinkedListNode LinkedListNode LinkedListNode Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 15 / 24

Validity Requirements of a Linked List The linked list structure is valid provided m_size >= 0. If m_size == 0, then m_head == NULL. If m_size > 0, then m_head!= NULL. For every i from 0 to m_size - 2, in node i, m_next!= NULL. In node m_size - 1, m_next == NULL. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 16 / 24

Outline 1 Linked Lists 2 The LinkedListNode Class 3 The LinkedList Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 17 / 24

Random Access vs. Sequential Access A linked list allows sequential access, but not random access. To find the list element in position n, we must begin at the head of the list and move sequentially through positions 1, 2,..., n 1 before reaching position n. An array allows random, or direct, access. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 18 / 24

Random Access vs. Sequential Access Sequential access is much slower than random access if the list elements are accessed randomly. However, sequential access may be faster than random access if the list elements are accessed sequentially. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 19 / 24

The Technique of Chasing Pointers Chasing Pointers LinkedListNode* node = m_head; for (int i = 0; i < pos; i++) node = node->m_next; Technique of chasing pointers to locate position pos. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 20 / 24

Random Access vs. Sequential Access Random Access vs. Sequential Access for (int i = 0; i < list.size(); i++) list[i] = 0; How would the performance of the above for loop differ between the ArrayList and LinkedList implementations? Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 21 / 24

The LinkedList Class The LinkedList Class linkedlistnode.h. linkedlist.h. ListTest.cpp. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 22 / 24

Outline 1 Linked Lists 2 The LinkedListNode Class 3 The LinkedList Class 4 Random Access vs. Sequential Access 5 Assignment Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 23 / 24

Assignment Assignment Read Sections 17.1-17.3. Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 24 / 24