Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Bag Chapter 3.

Similar presentations


Presentation on theme: "Linked Bag Chapter 3."— Presentation transcript:

1 Linked Bag Chapter 3

2 Outline Pros and cons of ArrayBag Links and linked structures
Class Node private class Node LinkedBag Methods Pros and cons of LinkedBag

3 Pros and Cons of ArrayBag
quick, direct access to known positions fast add/remove at end of array Cons fixed size or cost of copying entries slightly slower remove in middle wasted space

4 Alternative to Arrays Each entry remembers who’s next
bag itself remembers who’s first Known as a “chain” Fencer Architect Business Man (no one) Golfer Doctor

5 Adding to the Chain New guy remembers who was first
bag remembers the new person Fencer Doctor Architect Business Man (no one) Golfer Professor Doctor

6 Removing from the Chain
From front is easy reverse adding at front Fencer Doctor Architect Business Man (no one) Golfer Professor Doctor

7 Removing from the Chain
From middle is harder find person remembering guy leaving have them remember the one after the person leaving Fencer Architect Fencer Business Man (no one) Golfer Doctor

8 Chains: Why? How? Don’t need a whole array of spaces
one extra space for each object in the bag to remember who comes next But the objects don’t have extra space for remembering the following object! would require an extra instance variable can’t add an extra instance variable to a String (e.g.) we need some help – a little helper class

9 Minions Minions remember a person and a minion
Gru remembers which minion comes first

10 Class Node (Our “Minion”)
Remembers one object and one node must be able to remember any sort of object generic class next node remembers same kind of thing public class Node<T> { private T data; private Node<T> next; } a Node & another Node & some data more data

11 Class LinkedBag (Our “Gru”)
Remembers which Node comes first first node remembers the kind of thing that’s in the bag (same parameter) public class LinkedBag<T> { private Node<T> first; private int numEntries; } a LinkedBag & 4 a Node & some data

12 LinkedBag Constructor
Bag starts empty no first entry, zero entries public LinkedBag() { first = null; numberOfEntries = 0; } NOTE: no memory allocation no need to worry about partially initialized objects! no need for checkInitialization

13 Linked Bag Bag with doctor, architect, golfer and fencer & 4 & & & & /

14 Following the Chain curNode keeps track of where we are
start at first minion/node (curNode = first) go to next minion/node (curNode = curNode.next) we know how many minions/nodes there are & 4 & & & & / curNode / &

15 Minions at Risk Clients should not be able to access Nodes
could accidentally/maliciously change values curNode.next = myBag.getFirst(); & 4 & & & & / curNode /

16 Private Classes One class can actually belong to another
Node can belong to LinkedBag Can be public or private private  only owner class can use it owner gets full access either way! Declare owned class inside owner class public class Owner { private class Owned {

17 Owned Generic Classes Don’t need <T> on owned class
owner is already <T>, so owned is, too! public class LinkedNode<T> { private class Node { private T data; // same type as bag’s base type private Node next; } private Node first; private int numberOfEntries;

18 Node Constructors Will need to create new nodes
data filled in, next maybe filled in (null if not) code goes inside Node class inside LinkedBag public Node(T value) { this(value, null); } public Node(T value, Node n) { data = value; next = n;

19 Add an Entry First add: Second (and later) add: / “First” / & & 1 & /
“First” / & & 1 & / “Later” “First” & 2 & 1 & 1 & / &

20 add(T) Method LinkedBag method (not a Node method!)
create Node to remember new data value its next field is whatever was first even if there was nothing in the bag (first == null) public boolean add(T newData) { first = new Node(newData, first); ++numberOfEntries; return true; }

21 toArray() Method Need to make a new array & copy values
need to follow the trail of minions public T[] toArray() { T[] result = (T[]) new Object[numberOfEntries]; currentNode = first; for (int i = 0; i < numberOfEntries; ++i) { result[i] = currentNode.data; currentNode = currentNode.next; } return result;

22 Testing Core Methods Tests for LinkedBag are exactly the same as for ArrayBag same methods; same behaviour expected replace new ArrayBag with new LinkedBag should get same results

23 getCurrentSize() Method
Return the number of entries public int getCurrentSize() { return numberOfEntries(); } we could count the number of entries what would that code look like? but why bother? & 4

24 isEmpty() Method Two versions numberOfEntries is zero: first is null:
public boolean isEmpty() { return numberOfEntries == 0; } first is null: return first == null; /

25 getFrequencyOf(T) Method
Loop thru Nodes counting those equals public int getFrequencyOf(T anEntry) { Node curNode = first; int count = 0; for (int i = 0; i < numberOfEntries; ++i) { if (curNode.data.equals(anEntry)) { ++count; } curNode = curNode.next; return count;

26 contains(T) Method Loop thru until find data or end of bag
public int getFrequencyOf(T anEntry) { Node curNode = first; for (int i = 0; i < numberOfEntries; ++i) { if (curNode.data.equals(anEntry)) { return true; } curNode = curNode.next; return false;

27 remove() Method Reverse add method to remove first entry
possible problem: nothing in bag public T remove() { if (first != null) { Node toDelete = first; first = toDelete.next; --numberOfEntries; return toDelete.data; } return null; toDelete & “One” “Two” & 1 & 4 & 1 & & 3

28 clear() Method Same as before! keep removing until bag is empty
public void clear() { while (!isEmpty()) { remove(); }

29 remove(T) Considerations
Remove node by updating links links to update in node before the one with data find the node before the node with the data but the data might not be there but the data might be in the first node actually, we already know how to do the first node… hmm. & & & / removing the Golfer Node before Golfer needs to change link

30 remove(T) Clever Plan Find the node with the desired data
Swap in the data from the first position Remove the first node using remove() & 4 & & & & / curNode &

31 remove(T) Method what if the node with the data is the first node?
public boolean remove(T anEntry) { Node curNode = findNode(anEntry); // private method if (curNode != null) { curNode.data = first.data; remove(); // updates numberOfEntries return true; } return false; what if the node with the data is the first node?

32 findNode(T) Method Returns the node with the wanted data
returns null if the data wasn’t found private Node findNode(T anEntry) { Node curNode = first; for (int i = 0; i < numberOfEntries; ++i) { if (curNode.data.equals(anEntry)) { return curNode; } curNode = curNode.next; return null;

33 Exercise Easy: Harder: Rewrite contains to use findNode
Why can’t frequencyOf use findNode? How could we change findNode so it could? What changes would we need to make to remove(T) and contains(T) if we did? Rewrite frequencyOf with the new findNode

34 Pros & Cons of LinkedBag
quick, direct access to first position fast add/remove at front of bag little/no copying cost grows and shrinks as required Cons slightly slower remove in middle chain requires more memory than array (!)

35 ArrayBag vs. LinkedBag Linked grow smoothly; Array in jumps
Linked shrinks smoothly; Array doesn’t Linked uses more memory Linked slightly slower to search following links slower than incrementing index Add quick in both Remove quick when removing last added

36 Next Time How do we tell what’s better?


Download ppt "Linked Bag Chapter 3."

Similar presentations


Ads by Google