Download presentation
Presentation is loading. Please wait.
Published byLorena Baldwin Modified over 9 years ago
1
CS 2430 Day 35
2
Agenda Introduction to linked lists Bag as linked list Stack as linked list
3
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)
4
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”
5
Introduction to linked lists
6
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
7
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
8
The Node class class Node { public Object info; public Node next;// Note the type! public Node(Object theInfo, Node theNext) { info = theInfo; next = theNext; } }
9
An empty list Node list; // points to null... ► ► Ølist
10
Insert x at the back list = new Node(x, null);... What is the big O? O(1) ► ► xlistØØ
11
Insert y at the back list.next = new Node(y, null);... ► ► listxØyØ
12
Insert z at the back list.next.next = new Node(z, null);... ► ► listxØyØz
13
Delete from the front list = list.next;... What is the big O? O(1) ► ► listxyzØ
14
Insert at front list = new Node(w, list);... What is the big O? O(1) ► ► list w yzØ
15
Print everything out Node p = list; while (p != null) { System.out.println(p.info); p = p.next; } ► ► ► ► ► ► list p xyzØ
16
Always draw pictures when working linked list problems
17
Any questions?
18
Go to THE THING: https://xray.ion.uwplatt.edu/summerss
19
Growable Bag as linked list public 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
20
public class Bag { private Node list; public boolean add(Object obj) { list = new Node(obj, list); return true; } }
21
public class Bag { private Node list;... public boolean isEmpty() { return list == null; } public void reset() { list = null; } }
22
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!
23
public class Bag { private Node list; private int count = 0; public void reset() { count = 0; list = null; }... }
24
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.