cs 2430 day 35. agenda introduction to linked lists bag as linked list stack as linked list
TRANSCRIPT
![Page 1: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/1.jpg)
CS 2430
Day 35
![Page 2: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/2.jpg)
Agenda
• Introduction to linked lists
• Bag as linked list
• Stack as linked list
![Page 3: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/3.jpg)
Growing slowly
• Do you remember the grow() method?
• What is its big O complexity?
• O(N), where N is the number of elements in the Bag, Queue, etc.
• Therefore, add(), enqueue(), etc., had worst case big O of O(N)
![Page 4: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/4.jpg)
Growing faster
• Can we make a growable Bag class with O(1) worst case add() method?
• Yes, we can!
• We cannot use an array
• We will use a “linked list”
![Page 5: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/5.jpg)
Introduction to linked lists
![Page 6: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/6.jpg)
Linked lists
• Arrays are random access, i.e., access to array element is O(1)
• Linked lists can have data scattered all over memory
• Therefore, linked lists are (mostly) sequential access
![Page 7: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/7.jpg)
Nodes
• Each element of a linked list is a Node
• Nodes have “info” and “next” fields
• The “next” field points to the next node in the list
![Page 8: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/8.jpg)
The Node classclass Node
{
public Object info;
public Node next; // Note the type!
public Node(Object theInfo, Node theNext)
{
info = theInfo;
next = theNext;
}
}
![Page 9: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/9.jpg)
An empty list
Node list; // points to null. . .
►►
Ølist
![Page 10: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/10.jpg)
Insert x at the back
list = new Node(x, null);. . .
What is the big O?O(1)
►►
xlist ØØ
![Page 11: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/11.jpg)
Insert y at the back
list.next = new Node(y, null);. . .
►►
list x ØyØ
![Page 12: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/12.jpg)
Insert z at the back
list.next.next = new Node(z, null);. . .
►►
list x Øy Øz
![Page 13: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/13.jpg)
Delete from the front
list = list.next;. . .
What is the big O?O(1)
►►
list x y z Ø
![Page 14: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/14.jpg)
Insert at front
list = new Node(w, list);. . .
What is the big O?O(1)
►►
list
w
y z Ø
![Page 15: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/15.jpg)
Print everything out
Node p = list;while (p != null){ System.out.println(p.info); p = p.next;}
►►►►►►
list
p
x y z Ø
![Page 16: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/16.jpg)
Always draw pictures when working linked list problems
![Page 17: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/17.jpg)
Any questions?
![Page 19: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/19.jpg)
Growable Bag as linked listpublic class Bag
{
private Node list;
public Bag() { . . . }
public boolean add(Object obj) { . . . } // insert at front
public boolean isEmpty() { . . . }
public int size() { . . . }
public void reset() { . . . }
public boolean contains(Object target) { . . . }
public boolean remove(Object obj) { . . . }
}
We will do some of the methods
![Page 20: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/20.jpg)
public class Bag
{
private Node list;
public boolean add(Object obj)
{
list = new Node(obj, list);
return true;
}
}
![Page 21: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/21.jpg)
public class Bag
{
private Node list;
. . .
public boolean isEmpty()
{
return list == null;
}
public void reset()
{
list = null;
}
}
![Page 22: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/22.jpg)
public class Bag
{
private Node list;
. . .
public int size()
{
int count = 0;
Node p = list;
while (p != null)
{
count++;
p = p.next;
}
return count;
}
}
What is the big O?
O(N)
I think we can do better than this!
![Page 23: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/23.jpg)
public class Bag
{
private Node list;
private int count = 0;
public void reset()
{
count = 0;
list = null;
}
. . .
}
![Page 24: CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list](https://reader030.vdocument.in/reader030/viewer/2022032805/56649efb5503460f94c0d20c/html5/thumbnails/24.jpg)
public class Bag
{
private Node list;
private int count;
. . .
public boolean add(Object obj)
{
list = new Node(obj, list);
count++;
return true;
}
public int size()
{
return count;
}
}
Now size(), add(), reset() and isEmpty() are all O(1)