linked lists - lecture 16 sections 17.1 -...

25
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

Upload: others

Post on 29-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Linked ListsLecture 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

Page 2: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 3: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 4: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Linked Lists

In a linked list, each node is allocated dynamically when theelement is added to the list and deallocated dynamically when it isremoved 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 andless 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

Page 5: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Linked Lists

a0 a1 a2 ... an - 1

The abstract concept

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 5 / 24

Page 6: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Linked Lists

a0 a1 a2 ... an - 1

The linked implementation

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 5 / 24

Page 7: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 8: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

The LinkedListNode Class

We create a separate class, LinkedListNode, as a data typethat stores a single element of a linked list.Each LinkedListNode contains a data item and a pointer thatlinks it to the next node.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 7 / 24

Page 9: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

LinkedListNode Data Members

LinkedListNode Data MembersT m_value;

LinkedListNode* m_next;

m_value – The element ai .m_next – A pointer to the node containing the element ai+1 orNULL if ai is the last element.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 8 / 24

Page 10: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

LinkedListNode Member Functions

LinkedListNode Public Member FunctionsT 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 thenode (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

Page 11: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

LinkedListNode Member Functions

LinkedListNode Private Member FunctionsLinkedListNode(const T& val = T());

LinkedListNode(T&); – Constructs a linked list nodecontaining 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

Page 12: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 13: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

The LinkedListNode Class

The LinkedListNode ClassThe header file linkedlistnode.h.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 12 / 24

Page 14: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 15: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

The LinkedList Class

LinkedList Data Membersint 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 firstelement).

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 14 / 24

Page 16: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

The LinkedListNode Class

m_size

m_head m_value m_next

LinkedList LinkedListNode LinkedListNode LinkedListNode

m_value m_next m_value m_next

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 15 / 24

Page 17: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Validity Requirements of a Linked List

The linked list structure is valid providedm_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

Page 18: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 19: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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 ofthe list and move sequentially through positions 1,2, . . . ,n − 1before reaching position n.An array allows random, or direct, access.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 18 / 24

Page 20: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Random Access vs. Sequential Access

Sequential access is much slower than random access if the listelements are accessed randomly.However, sequential access may be faster than random access ifthe list elements are accessed sequentially.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 19 / 24

Page 21: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

The Technique of Chasing Pointers

Chasing PointersLinkedListNode* 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

Page 22: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Random Access vs. Sequential Access

Random Access vs. Sequential Accessfor (int i = 0; i < list.size(); i++)

list[i] = 0;

How would the performance of the above for loop differ betweenthe ArrayList and LinkedList implementations?

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 21 / 24

Page 23: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

The LinkedList Class

The LinkedList Classlinkedlistnode.h.linkedlist.h.ListTest.cpp.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 22 / 24

Page 24: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

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

Page 25: Linked Lists - Lecture 16 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · 2017-02-21 · Linked Lists Lecture 16 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney

Assignment

AssignmentRead Sections 17.1 - 17.3.

Robb T. Koether (Hampden-Sydney College) Linked Lists Wed, Feb 22, 2017 24 / 24