week 4 - wednesday. what did we talk about last time? started linked lists
TRANSCRIPT
![Page 1: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/1.jpg)
CS221Week 4 - Wednesday
![Page 2: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Started linked lists
![Page 3: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/4.jpg)
Bitmap ManipulatorProject 1
![Page 5: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/5.jpg)
Interview question
You are given a singly linked list It may have a loop in it, that is, a
node that points back to an earlier node in the list
If you try to visit every node in the list, you’ll be in an infinite loop
How can you see if there is a loop in a linked list?
![Page 6: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/6.jpg)
Implementations
![Page 7: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/7.jpg)
Doubly linked list Node consists of data, a next pointer, and a
previous pointer Advantages: bi-directional movement Disadvantages: slower, 4 pointers must change
for every insert/delete Xhead
23 47 58
X tail
![Page 8: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/8.jpg)
Definition Let’s try a simple definition for a linked list:public class LinkedList {private static class Node {public int data;public Node next;public Node previous;
}
private Node head = null;private Node tail = null;public int size = 0;…}
![Page 9: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/9.jpg)
Delete from head
![Page 10: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/10.jpg)
Index Of
![Page 11: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/11.jpg)
Insert at index i
![Page 12: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/12.jpg)
Assuming that the list has been kept in orderInsert in order
![Page 13: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/13.jpg)
Generic Linked List with Iterator
![Page 14: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/14.jpg)
Things aren't that different! The generic part is really easy once
you get the syntax set up You use T instead of int (or
whatever type you designed your linked lists to hold)
The trickier thing is to define an iterator class that can keep track of where you are inside the list It has to be a non-static inner class!
![Page 15: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/15.jpg)
Definitionpublic class LinkedList<T> implements Iterable<T> {private class Node {public T data;public Node next;public Node previous;
}private class ListIterator implements Iterator<T> {public Node current;}
private Node head = null;private Node tail = null;public int size = 0;…}
![Page 16: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/16.jpg)
ConstructorCreate a new iterator that points at the head of the list
![Page 17: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/17.jpg)
boolean hasNext()Whether or not there is something in the current spot in the list
![Page 18: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/18.jpg)
T next()Get the current thing in the list
![Page 19: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/19.jpg)
void remove()Remove the current item from the list (optional)
![Page 20: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/20.jpg)
Other kinds of linked lists
![Page 21: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/21.jpg)
Circular linked lists
Linked lists can be made circular such that the last node points back at the head node
This organization is good for situations in which we want to cycle through all of the nodes in the list repeatedly
tail
23 47 58
![Page 22: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/22.jpg)
Performance of a circular linked list
Insert at front (or back) Θ(1)
Delete at front Θ(1) Delete at back costs Θ(n) unless we
used doubly linked lists Search
Θ(n)
![Page 23: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/23.jpg)
Skip lists We can design linked lists with multiple
pointers in some nodes We want ½ of the nodes to have 1 pointer,
¼ of the nodes to have 2 pointers, 1/8 of the nodes to have 3 pointers…
head
145
3 29
2841
58
XXX
![Page 24: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/24.jpg)
Performance of skip lists
If ordered, search is Θ(log n)
Go to index is Θ(log n)
Insert at end Θ(log n)
Delete Totally insane, at least Θ(n)
Trees end up being a better alternative
![Page 25: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/25.jpg)
Self organizing lists We want to make items that are used
frequently easy to get at Several different approaches, mostly based on
finding items repeatedly Move to front: After finding an item, put it in the
front Transpose: After finding an item, move it up by
one Count: Keep the list ordered by how often you get
a particular item (requires a counter in each node) Ordering: Sort the list according to some feature
of the data
![Page 26: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/26.jpg)
Quiz
![Page 27: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/27.jpg)
Upcoming
![Page 28: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/28.jpg)
Next time…
Special kinds of linked lists Circular lists Skip lists Self-organizing lists
Stack implementation with linked lists
Queue implementation with linked lists
![Page 29: Week 4 - Wednesday. What did we talk about last time? Started linked lists](https://reader035.vdocuments.us/reader035/viewer/2022062317/5a4d1b6e7f8b9ab0599b48d6/html5/thumbnails/29.jpg)
Reminders Keep reading Chapter 3 Keep working on Project 1
Due this Friday, September 18 by 11:59pm My office hours this Friday from 3:30-5pm
are canceled due to travel CS Club tonight at 6pm Anyone want to work for ITS? For women in CS:
http://www.meetup.com/Ladies-Technically-Speaking-Lancaster/