![Page 1: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/1.jpg)
Programming Abstractions
Cynthia Lee
C S 106X
![Page 2: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/2.jpg)
Topics this week:
Memory and Pointers
› Revisit some topics from last week
› Deeper look at what a pointer is
• Hexadecimal!
• Address-of operator: &
› Dynamic Memory allocation
› Dereference operator: *
› Dynamic Memory with classes
• The -> operator
› Linked nodes
› Linked List data structure
› (if we have time) Priority Queue and Heap data structure
TODAY’S TOPICS NOT ON THE MIDTERM
2
![Page 3: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/3.jpg)
List code example: Draw a picture!
Before:
front->next->next = new LinkNode();
front->next->next->data = 40;
A. After:
B. After:
C. Using “next” that is NULL gives error
D. Other/none/more than one
data next
10
data next
20 NULLfront
data next
10
data next
40front
data next
20 NULL
data next
10
data next
20front
data next
40 NULL
struct ListNode {ListNode(int d = 0,
ListNode *n = NULL) {data = d;next = n;
}int data;ListNode *next;
}
![Page 4: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/4.jpg)
FIRST RULE OF LINKED NODE/LISTS CLUB:
DRAW A PICTURE OF LINKED LISTS
Do no attempt to code linked nodes/lists without pictures!
![Page 5: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/5.jpg)
Linked List Data StructurePutting the ListNode to use
![Page 6: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/6.jpg)
A LinkedList class
Let's write a collection class named LinkedList.
Has the same public members as ArrayList, Vector, etc.
› add, clear, get, insert, isEmpty, remove, size, toString
The list is internally implemented as a chain of linked nodes
› The LinkedList keeps a pointer to its front node as a field
› NULL is the end of the list; a NULL front signifies an empty list
front
add(value)insert(index, value)remove(index)size()toString()...
LinkedList
ListNode ListNode ListNodedata next
42
data next
-3
data next
17
element 0 element 1 element 2
![Page 7: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/7.jpg)
Traversing a list? (BUG version)
What's wrong with this approach to traverse and print the list?
while (list != NULL) {
cout << list->data << endl;list = list->next; // move to next node
}
It loses the linked list as it is printing it!
data next
10
data next
990list
...
data next
20
![Page 8: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/8.jpg)
Traversing a list (12.2) (bug fixed version)
The correct way to print every value in the list:
ListNode* current = list;while (current != NULL) {
cout << current->data << endl;current = current->next; // move to next node
}
Changing current does not damage the list.
data next
10
data next
990list
...
data next
20
![Page 9: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/9.jpg)
LinkedList.h
class LinkedList {public:
LinkedList();~LinkedList();void add(int value);void clear();int get(int index) const;void insert(int index, int value);bool isEmpty() const;void remove(int index);void set(int index, int value);int size() const;
private:ListNode* front;
};
front =
LinkedList
![Page 10: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/10.jpg)
Implementing add
// Appends the given value to the end of the list.void LinkedList::add(int value) {
...}
What pointer(s) must be changed to add a node to the end of
a list?
What different cases must we consider?
front = data next
42
data next
-3
data next
17
element 0 element 1 element 2
![Page 11: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/11.jpg)
Case 1: Add to empty list
Before adding 20: After:
We must create a new node and attach it to the list.
For an empty list to become non-empty, we must change
front.
front = front = data next
20
element 0
![Page 12: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/12.jpg)
Case 2: Non-empty list
Before adding value 20 to end of list:
After:
front = data next
42
data next
-3
front = data next
42
data next
-3
data next
20
element 0 element 1 element 2
element 0 element 1
![Page 13: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/13.jpg)
Don't fall off the edge!
Must modify the next pointer of the last node.
Where should current be pointing, to add 20 at the end?
Q: What loop test will stop us at this place in the list?
A. while (current != NULL) { ...
B. while (front != NULL) { ...
C. while (current->next != NULL) { ...
D. while (front->next != NULL) { ...
front = data next
42
data next
-3
element 0 element 1
![Page 14: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/14.jpg)
Code for add
// Adds the given value to the end of the list.void LinkedList::add(int value) {
if (front == NULL) {// adding to an empty listfront = new ListNode(value);
} else {// adding to the end of an existing listListNode* current = front;while (current->next != NULL) {
current = current->next;}current->next = new ListNode(value);
}}
![Page 15: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/15.jpg)
Implementing get
// Returns value in list at given index.int LinkedList::get(int index) {
...}
front = data next
42
data next
-3
data next
17
element 0 element 1 element 2
![Page 16: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/16.jpg)
Code for get
// Returns value in list at given index.// Precondition: 0 <= index < size()int LinkedList::get(int index) {
ListNode* current = front;for (int i = 0; i < index; i++) {
current = current->next;}return current->data;
}
![Page 17: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/17.jpg)
Implementing insert
// Inserts the given value at the given index.void LinkedList::insert(int index, int value) {
...}
front = data next
42
data next
-3
data next
17
element 0 element 1 element 2
![Page 18: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/18.jpg)
Inserting into a list
Before inserting element at index 2:
After:
Q: How many times to advance current to insert at index i ?
A. i - 1 B. i C. i + 1 D. none of the above
front = data next
48
data next
-3
data next
17
element 0 element 1 element 2
front = data next
48
data next
-3
data next
22
element 0 element 1 element 2
data next
17
element 3
![Page 19: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/19.jpg)
Code for insert
// Inserts the given value at the given index.// Precondition: 0 <= index <= size()void LinkedList::insert(int index, int value) {
if (index == 0) {// adding to an empty listfront = new ListNode(value, front);
} else {// inserting into an existing listListNode* current = front;for (int i = 0; i < index - 1; i++) {
current = current->next;}current->next =
new ListNode(value, current->next);}
}
![Page 20: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/20.jpg)
Implementing remove
// Removes value at given index from list.void LinkedList::remove(int index) {
...}
What pointer(s) must be changed to remove a node from a
list?
What different cases must we consider?
front = data next
48
data next
-3
data next
22
element 0 element 1 element 2
data next
17
element 3
![Page 21: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/21.jpg)
Removing from a list
Before removing element at index 2:
After:
Where should current be pointing?How many times should it advance from front?
front = data next
48
data next
-3
data next
22
element 0 element 1 element 2
data next
22
trash
data next
17
element 3
front = data next
48
data next
-3
data next
17
element 0 element 1 element 2
![Page 22: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/22.jpg)
Removing from front
Before removing element at index 0:
After:
To remove the first node, we must change front.
front = data next
-3
data next
20
front = data next
42
data next
-3
data next
20
element 0 element 1 element 2
element 0 element 1
data next
42
trash
![Page 23: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/23.jpg)
Removing the only element
Before: After:
We must change the front field to store NULL instead of a
node.
Do we need a special case to handle this?
front = front = data next
20
element 0
![Page 24: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/24.jpg)
Code for remove
// Removes value at given index from list.// Precondition: 0 <= index < size()void LinkedList::remove(int index) {
ListNode* trash;if (index == 0) { // removing first element
trash = front;front = front->next;
} else { // removing elsewhere in the listListNode* current = front;for (int i = 0; i < index - 1; i++) {
current = current->next;}trash = current->next;current->next = current->next->next;
}delete trash;
}
![Page 25: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/25.jpg)
Other list features
Add the following public members to the LinkedList:
size()
isEmpty()
set(index, value)
clear()
toString()
Add a size field to the list to return its size more efficiently.
Add preconditions and exception tests as appropriate.
![Page 26: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/26.jpg)
Priority QueueEmergency Department waiting room operates as a priority queue: patients
are sorted according to priority (urgency), not “first come, first serve” (in
computer science, “first in, first out” or FIFO).
Image is in the public domain.
http://commons.wikimedia.org/wiki/File:Columbus_Fire_Medic_7.JPG
![Page 27: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/27.jpg)
Some priority queue implementation options
Unsorted linked list
Insert new element in front
Remove by searching list for highest-priority item
Sorted linked list
Always insert new elements where they go in priority-sorted order
Remove from front (will be highest-priority because sorted)
data next
75
data next
8head
data next
20 NULL
data next
8
data next
20head
data next
75 NULL
![Page 28: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/28.jpg)
Unsorted linked list
Add is FAST
Just throw it in the list at the front
O(1)
Remove/peek is SLOW
Hard to find item the highest priority
item—could be anywhere
O(N)
Priority queue implementations
Th
is file
is lic
en
sed
un
der
the
Cre
ative C
om
mon
sA
ttri
bu
tion
-Sh
are
Alik
e 3
.0 U
np
ort
ed
licen
se.
Keyah
Ch
eatu
mh
ttp
://c
om
mon
s.w
ikim
ed
ia.o
rg/w
iki/F
ile:M
essy_
Ro
om
.JP
G
data next
75
data next
8head
data next
20 NULL
![Page 29: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/29.jpg)
Sorted linked list
Add is SLOW
Need to step through the list to find where
item goes in priority-sorted order
O(N)
Remove/peek is FAST
Easy to find item you are looking for (first
in list)
O(1)
Priority queue implementations
data next
8
data next
20head
data next
75 NULL
Imag
e is in
th
e p
ub
lic d
om
ain
.
http
://c
om
mon
s.w
ikim
ed
ia.o
rg/w
iki/F
ile:W
all_
Clo
set.jp
g
![Page 30: Programming Abstractionsstanford.edu/class/archive/cs/cs106x/cs106x.1162/lectures/15-LinkedList.pdfA. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next-3 data](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed6e405df0eda5e752ae8d0/html5/thumbnails/30.jpg)
We want the best of both
Fast add AND fast remove/peek
We will investigate trees as a way to get the best of both worlds
Priority queue implementations
+ =
Fast add Fast remove/peek