cs 106x, lecture 16 more linked lists - stanford...

18
This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, Marty Stepp, Ashley Taylor and others. CS 106X, Lecture 16 More Linked Lists reading: Programming Abstractions in C++, Chapters 11-12, 14.1-14.2

Upload: others

Post on 02-Jun-2020

14 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, Marty Stepp, Ashley Taylor and others.

CS 106X, Lecture 16More Linked Lists

reading:Programming Abstractions in C++, Chapters 11-12, 14.1-14.2

Page 2: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

2

Plan For Today• Implementing a Linked List

– Pointers– Dynamic memory– Classes– Testing

• Announcements• Template Classes• Doubly-Linked Lists

Page 3: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

3

Learning Goals• Understand the implementation of the LinkedList ADT• Understand how to make generic classes using templates• Understand the benefits and drawbacks of doubly-linked lists

Page 4: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

4

Plan For Today• Implementing a Linked List

– Pointers– Dynamic memory– Classes– Testing

• Announcements• Template Classes• Doubly-Linked Lists

Page 5: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

5

LinkedListClass• Let's write a collection class named LinkedListClass.

– Has the similar public members to Vector•add, clear, get, insert, remove, size, toString

– The list is internally implemented as a chain of linked nodes• The LinkedListClass keeps a pointer to its front node as a field•nullptr is the end of the list; a null front signifies an empty list

front

add(value)insert(index, value)remove(index)size()toString()...

LinkedListClassListNode ListNode ListNode

data next42

data next-3

data next17

element 0 element 1 element 2

Page 6: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

6

Destructor (12.3)// ClassName.h // ClassName.cpp~ClassName(); ClassName::~ClassName() { ...

• destructor: Called when the object is deleted by the program.(when the object goes out of {} scope; opposite of a constructor)

– Useful if your object needs to do anything important as it dies:• saving any temporary resources inside the object• freeing any dynamically allocated memory used by the object's members• ...

Page 7: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

7

Plan For Today• Implementing a Linked List

– Pointers– Dynamic memory– Classes– Testing

• Announcements• Template Classes• Doubly-Linked Lists

Page 8: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

8

Announcements• Midterm Thursday 11/1 7-9PM in 420-040• Midterm Review session tomorrow Tues. 10/30 5-6:30PM in

Hewlett 102• Assignment 5, MiniBrowser, out later today, due Wed. 11/7 @

11AM

Page 9: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

9

Plan For Today• Implementing a Linked List

– Pointers– Dynamic memory– Classes– Testing

• Announcements• Template Classes• Doubly-Linked Lists

Page 10: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

10

Template function (14.1-2)template<typename T>returntype name(parameters) {

statements;}

• Template: A function or class that accepts a type parameter(s).– Allows you to avoid redundancy by writing a function that can accept

many types of data.– Templates can appear on a single function, or on an entire class

Page 11: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

11

Template func exampletemplate<typename T>T max(T a, T b) {

if (a < b) { return b; }else { return a; }

}

– The template is instantiated each time you use it with a new type.• The compiler actually generates a new version of the code each time.• The type you use must have an operator < to work in the above code.

int i = max(17, 4); // T = intdouble d = max(3.1, 4.6); // T = doublestring s = max(string("hi"), // T = string

string("bye"));

Page 12: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

12

Template class (14.1-2)• Template class: A class that accepts a type parameter(s).

– In the header and cpp files, mark each class/function as templated.– Replace occurrences of the previous type int with T in the code.

// ClassName.htemplate<typename T>class ClassName {

...};

// ClassName.cpptemplate<typename T>type ClassName::name(parameters) {

...}

Page 13: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

13

Template .h and .cpp• Because of an odd quirk with C++ templates, the separation

between .h header and .cpp implementation must be reduced.– Either write all the bodies in the .h file (suggested),– Or #include the .cpp at the end of .h file to join them together.

// ClassName.h#ifndef _classname_h#define _classname_h

template<typename T>class ClassName {

...};

#include "ClassName.cpp"#endif // _classname_h

Page 14: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

14

Exercise• Convert the LinkedListClass to use templates.

– A client should be able to create a LinkedListClass of any type.

LinkedListClass<int> s1;s1.add(42);s1.add(17);

LinkedListClass<string> s2;s2.add("hello");s2.add("there");...

Page 15: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

15

Plan For Today• Implementing a Linked List

– Pointers– Dynamic memory– Classes– Testing

• Announcements• Template Classes• Doubly-Linked Lists

Page 16: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

16

Doubly linked list• doubly linked list: Each node has a pointer to next and prev node.

– Allows walking forward and backward in list efficiently.– Overall list often maintains a back pointer to end of list.

frontprev data next

42element 0

prev data next17

element 1

prev data next88

element 2

back

Page 17: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

17

D.L. list growth• State of a doubly linked list of 0, 1, 2, N nodes:

frontprev data next

42prev data next

17 back

0: front back

1: front backprev data next

42

2:(add at back)

(add at front)

frontprev data next

88prev data next

42 back3:prev data next

17 back

Page 18: CS 106X, Lecture 16 More Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106x/cs106x.1192/lectures/Lecture... · Doubly linked list •doubly linked list: Each

18

D.L. list remove• When removing a node, must change two pointers.

– Might also need to change front and/or back.– Example: Try removing each of the three nodes below.

frontprev data next

88prev data next

42 backprev data next

17 back