Download presentation
Presentation is loading. Please wait.
Published byAmelia Kennedy Modified over 9 years ago
1
This document is copyright (C) Stanford Computer Science and Marty Stepp, 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, and others. CS 106B Lecture 14 Feb 8, 2016 Chris Piech Linked Lists
2
2 恭禧发财
3
3 Midterm
4
4 Open Computer?
5
5 Conclusion: No open computers.
6
6 Midterm Questions?
7
7 Today’s Goals 1.Practice with dynamic allocation 2.Introduction to linked lists
8
8 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
9
9 How is the Stack Implemented?
10
10 Lets Write Vector
11
11 VectorInt class VectorInt { // in VectorInt.h public: VectorInt(); // constructor void add(int value); // append a value to the end int get(int index); // return the value at index private: type name; // member variables type name; // (data inside each object) };
12
12 Buggy VectorInt the First class VectorInt { // in VectorInt.h public: VectorInt(); // constructor void add(int value); // append a value to the end int get(int index); // return the value at index private: int value0; // member variables int value1; // (data inside each object) };
13
13 Problems with Stack Variables Variables have to be known at compile time. Not runtime.
14
14 Problems with Stack Variables Variables have to be known at compile time. Not runtime. Persistence is out of our control. Its hard to share a single large object between classes.
15
15 Dynamic Allocation!
16
16 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage.
17
17 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage.
18
18 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage. 124134
19
19 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage. 124134
20
20 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage. 124134
21
21 124134 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage. 124134 image 124134
22
22 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage. 124134 image 124134
23
23 Pointers GImage * image = new GImage(“cat.png”); // dynamically request memory for a new GImage. // calls the constructor. // store the address of the new GImage. 124134 image
24
24 Pointers Example
25
25
26
26 Pointers int * intList = new int[n]; // dynamically request memory for n integers. // store the address of the provided ints. intList
27
27 Variables have to be known at compile time. Not runtime. Persistence is out of our control. Its hard to share a single large object between classes. How Does this Help?
28
28 Dig deeper
29
29 All Memory Has an Address 00000 99999 00001 00002 00003 99998 99997 99996 RAM not disk Each program gets it’s own
30
30 URL Metaphore A pointer is like a URL. Not the actual page, the address of the page
31
31 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ?
32
32 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ?
33
33 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ? x y 12634 megan 12634
34
34 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ? x y 12634 megan 12634 10
35
35 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ? x y 12634 megan 12634 10 student 12634
36
36 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ? x y 12634 megan 12634 10 student 12634 7
37
37 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ? x y 12634 megan 12634 10 student 12634 7
38
38 Socrative Point * megan = new Point(); megan->setX(10); Point * student = megan; student->setY(7); cout getX(); cout << “ “; cout getY(); a) 10, 7 b) crashes c) ?, 7 d) ? ? x y 12634 megan 10 student 7
39
39
40
40 VectorInt class VectorInt { // in VectorInt.h public: VectorInt(); // constructor void add(int value); // append a value to the end int get(int index); // return the value at index private: type name; // member variables type name; // (data inside each object) };
41
41 Actual Vector class VectorInt { // in VectorInt.h public: VectorInt(); // constructor void add(int value); // append a value to the end int get(int index); // return the value at index private: int * data; };
42
42 Actual Vector class VectorInt { // in VectorInt.h public: VectorInt(); // constructor void add(int value); // append a value to the end int get(int index); // return the value at index private: int * data; int size; int allocatedSize; };
43
43 2 0 allocated length logical length element array Actual Vector
44
44 2 1 allocated length logical length element array 137 Actual Vector
45
45 2 2 allocated length logical length element array 13742 Actual Vector
46
46 2 2 allocated length logical length element array 13742 Actual Vector
47
47 2 2 allocated length logical length element array 13742 137 Actual Vector
48
48 2 2 allocated length logical length element array 13742 13742 Actual Vector
49
49 2 2 allocated length logical length element array 13742 13742 Actual Vector
50
50 2 2 allocated length logical length element array 13742 Actual Vector
51
51 2 2 allocated length logical length element array 13742 Actual Vector
52
52 4 2 allocated length logical length element array 13742 Actual Vector
53
53 4 3 allocated length logical length element array 13742271 Actual Vector
54
54 4 4 allocated length logical length element array 13742271828 Actual Vector
55
55 4 4 allocated length logical length element array 13742271828 Actual Vector
56
56 4 4 allocated length logical length element array 13742271828 137 Actual Vector
57
57 4 4 allocated length logical length element array 13742271828 13742 Actual Vector
58
58 4 4 allocated length logical length element array 13742271828 13742271 Actual Vector
59
59 4 4 allocated length logical length element array 13742271828 13742271828 Actual Vector
60
60 4 4 allocated length logical length element array 13742271828 13742271828 Actual Vector
61
61 4 4 allocated length logical length element array 13742271828 Actual Vector
62
62 4 4 allocated length logical length element array 13742271828 Actual Vector
63
63 8 4 allocated length logical length element array 13742271828 Actual Vector
64
64 8 5 allocated length logical length element array 13742271828182 Actual Vector
65
65 8 6 allocated length logical length element array 13742271828182845 Actual Vector
66
66 8 7 allocated length logical length element array 13742271828182845904 Actual Vector
67
67 8 8 allocated length logical length element array 137422718281828459045 Actual Vector
68
68 Dynamic allocation was necessary
69
69 Benefits of Dynamic Allocation Can decide on the number of variables at run time. Control over variable lifespan Its easy to share a single large object.
70
70 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
71
71 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
72
72 Vector Big O?
73
73 Big O of Get? int VecInt::get(int index){ return data[index]; }
74
74 Big O of Get?
75
75 Big O of Get?
76
76 Add?
77
77 Big O of Add? void VecInt::add(int v){ data[usedSize] = v; usedSize++; if(usedSize == allocatedSize) { doubleAllocation(); } void VecInt::doubleAllocation() { allocatedSize *= 2; int * newData = new int[allocatedSize]; for(int i = 0; i < usedSize; i++) { newData[i] = data[i]; } delete[] data; data = newData; }
78
78 Big O of Add? void VecInt::add(int v){ data[usedSize] = v; usedSize++; if(usedSize == allocatedSize) { doubleAllocation(); } void VecInt::doubleAllocation() { allocatedSize *= 2; int * newData = new int[allocatedSize]; for(int i = 0; i < usedSize; i++) { newData[i] = data[i]; } delete[] data; data = newData; } Worst Case
79
79 Big O of Add? void VecInt::add(int v){ data[usedSize] = v; usedSize++; if(usedSize == allocatedSize) { doubleAllocation(); } void VecInt::doubleAllocation() { allocatedSize *= 2; int * newData = new int[allocatedSize]; for(int i = 0; i < usedSize; i++) { newData[i] = data[i]; } delete[] data; data = newData; } Worst Case
80
80 void VecInt::add(int v){ data[usedSize] = v; usedSize++; if(usedSize == allocatedSize) { doubleAllocation(); } void VecInt::doubleAllocation() { allocatedSize *= 2; int * newData = new int[allocatedSize]; for(int i = 0; i < usedSize; i++) { newData[i] = data[i]; } delete[] data; data = newData; } Big O of Add? Worst Case
81
81 void VecInt::add(int v){ data[usedSize] = v; usedSize++; if(usedSize == allocatedSize) { doubleAllocation(); } void VecInt::doubleAllocation() { allocatedSize *= 2; int * newData = new int[allocatedSize]; for(int i = 0; i < usedSize; i++) { newData[i] = data[i]; } delete[] data; data = newData; } Big O of Add? Worst Case
82
82 void VecInt::add(int v){ data[usedSize] = v; usedSize++; if(usedSize == allocatedSize) { doubleAllocation(); } void VecInt::doubleAllocation() { allocatedSize *= 2; int * newData = new int[allocatedSize]; for(int i = 0; i < usedSize; i++) { newData[i] = data[i]; } delete[] data; data = newData; } Big O of Add? Worst Case
83
83 Big O of Add?
84
84 Remove?
85
85 Big O of Remove? void VecInt::remove(int index){ for(int i = index; i < usedSize - 1; i++) { data[i] = data[i + 1]; } usedSize--; }
86
86 Big O of Remove?
87
87 Summary?
88
88 Vector Big O Get Add Remove
89
89 That’s fine.
90
90 How is the Stack Implemented?
91
91 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
92
92 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
93
93 VectorInt class StackInt { // in VectorInt.h public: StackInt (); // constructor void push(int value); // append a value to the end int pop(); // return the value at index private: VectorInt data; // member variables };
94
94 12341234567 Excuse Me, Coming Through
95
95 12341234567 137 Excuse Me, Coming Through
96
96 123412345677 137 Excuse Me, Coming Through
97
97 123412345667 137 Excuse Me, Coming Through
98
98 123412345567 137 Excuse Me, Coming Through
99
99 123412344567 137 Excuse Me, Coming Through
100
100 123412334567 137 Excuse Me, Coming Through
101
101 123412234567 137 Excuse Me, Coming Through
102
102 123411234567 137 Excuse Me, Coming Through
103
103 12341371234567 Excuse Me, Coming Through
104
104 Stack as Vector Big O Push Pop
105
105 There’s always a better way
106
106 What About This? int * data 9 8 push(7);
107
107 What About This? int * data 9 8 7 push(7);
108
108 What About This? int * data 9 8 7 push(7);
109
109 What About This? int * data 9 8 7 push(7);
110
110 Oh Cool
111
111 What About This? int * data 9 8 7 push(6);
112
112 What About This? int * data 9 8 7 push(6); 6
113
113 What About This? int * data 9 8 7 push(6); 6
114
114 What About This? int * data 9 8 7 push(6); 6
115
115 What About This? int * data 9 8 7 push(6); 6
116
116 And Pop?
117
117 What About This? int * data 9 8 7 pop(); 6
118
118 What About This? int * data 9 8 7 pop(); 6 int return 6
119
119 What About This? int * data 9 8 7 pop(); 6 int return 6
120
120 What About This? int * data 9 8 7 pop(); int return 6
121
121 What About This? int * data 9 8 7 pop(); int return 6
122
122 Linked Lists!
123
123 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
124
124 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
125
125 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements are then chained together into a sequence. 123 Linked Lists
126
126 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements are then chained together into a sequence. 1234 Linked Lists
127
127 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements are then chained together into a sequence. 1234 Linked Lists
128
128 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements are then chained together into a sequence. 1234 Linked Lists
129
129 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements are then chained together into a sequence. 1234137 Linked Lists
130
130 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements are then chained together into a sequence. 134137 Linked Lists
131
131 Can efficiently splice new elements into the list or remove existing elements anywhere in the list. Never have to do a massive copy step; insertion is efficient in the worst-case. Has some tradeoffs; we'll see this later. Linked Lists
132
132 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Linked Lists
133
133 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Linked Lists
134
134 In C++, a structure is a type consisting of several individual variables all bundled together. To create a structure, we must Define what fields are in the structure, then Create a variable of the appropriate type. Similar to using classes – need to define and implement the class before we can use it. Structs
135
135 You can define a structure by using the struct keyword: struct TypeName { /* … field declarations … */ }; For those of you with a C background: in C++, “ typedef struct ” is not necessary. Structs
136
136 struct Tribute { string name; int districtNumber; }; Tribute t; t.name = "Katniss Everdeen"; t.districtNumber = 12; Structs
137
137 struct Tribute { string name; int districtNumber; }; Tribute t; t.name = "Katniss Everdeen"; t.districtNumber = 12; Structs
138
138 struct Tribute { string name; int districtNumber; }; Tribute t; t.name = "Katniss Everdeen"; t.districtNumber = 12; Structs
139
139 In C++, a class is a pair of an interface and an implementation. Interface controls how the class is to be used. Implementation specifies how it works. A struct is a stripped-down version of a class : Purely implementation, no interface. Primarily used to bundle information together when no interface is needed. Structs
140
140 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Structs
141
141 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Structs
142
142 We have seen the new keyword used to allocate arrays, but it can also be used to allocate single objects. The syntax: new T ( args ) creates a new object of type T passing the appropriate arguments to the constructor, then returns a pointer to it. Structs
143
143 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; Structs
144
144 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; Structs
145
145 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; t Structs
146
146 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; ???? name districtNumbe r t Structs
147
147 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; ???? name districtNumbe r t Structs
148
148 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; ???? name districtNumbe r t Structs
149
149 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; ???? name districtNumbe r t Structs
150
150 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; ???? name districtNumbe r t Because t is a pointer to a Tribute, not an actual Tribute, we have to use the arrow operator to access the fields pointed at by t. Structs
151
151 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; ???? name districtNumbe r t Structs
152
152 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; Katniss Everdeen ???? name districtNumbe r t Structs
153
153 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; Katniss Everdeen ???? name districtNumbe r t Structs
154
154 struct Tribute { string name; int districtNumber; }; Tribute* t = new Tribute; t->name = "Katniss Everdeen"; t->districtNumber = 12; Katniss Everdeen 12 name districtNumbe r t Structs
155
155 As with dynamic arrays, you are responsible for cleaning up memory allocated with new. You can deallocate memory with the delete keyword: delete ptr; This destroys the object pointed at by the given pointer, not the pointer itself. ptr 137 Structs
156
156 As with dynamic arrays, you are responsible for cleaning up memory allocated with new. You can deallocate memory with the delete keyword: delete ptr; This destroys the object pointed at by the given pointer, not the pointer itself. ptr 137 Structs
157
157 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Building our Vocabulary
158
158 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Building our Vocabulary
159
159 When working with pointers, we sometimes wish to indicate that a pointer is not pointing to anything. In C++, you can set a pointer to NULL to indicate that it is not pointing to an object: ptr = NULL; This is not the default value for pointers; by default, pointers default to a garbage value. The Null Pointer
160
160 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Building our Vocabulary
161
161 In order to use linked lists, we will need to introduce or revisit several new language features: Structures Dynamic allocation Null pointers Building our Vocabulary
162
162 And now... linked lists!
163
163 A linked list is a chain of cells. Each cell contains two pieces of information: Some piece of data that is stored in the sequence, and A link to the next cell in the list. We can traverse the list by starting at the first cell and repeatedly following its link. Linked Lists
164
164 For simplicity, let's assume we're building a linked list of string s. We can represent a cell in the linked list as a structure: struct Cell { string value; /* ? */ next; }; The structure is defined recursively! Linked Lists
165
165 For simplicity, let's assume we're building a linked list of string s. We can represent a cell in the linked list as a structure: struct Cell { string value; Cell* next; }; The structure is defined recursively! Linked Lists
166
166 For simplicity, let's assume we're building a linked list of string s. We can represent a cell in the linked list as a structure: struct Cell { string value; Cell* next; }; The structure is defined recursively! Linked Lists
167
167 Building Linked Lists!
168
168 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
169
169 Vector Big O Monday Stack? Linked Lists Dynamic Allocation Today’s Goals River of Linked Lists
170
170 Today’s Goals 1.Practice with dynamic allocation 2.Introduction to linked lists
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.