380 likes | 516 Views
Question of the Day. While walking across a bridge I saw a boat filled with people . Nobody boarded or left the boat, but on board the boat there was not a single person . How is this possible?. Question of the Day.
E N D
Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a single person. How is this possible?
Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a singleperson. How is this possible? Everybody on the boat was married.
CSC 212 – Data Structures Lecture 21:Arrays versus Linked-lists
How Could We Do This? • Already know simple way to hold data:array • Primitives or references can be stored in entries • Multi-dimensional arrays organize into tables (or more) • Arrays of generic type can limit code rewriting public class ArrayStack<T> {private inttop;private T[] Stack;public ArrayStack(){ s = (T[]) new Object[100]; }public void push(Telem) {stack[top] = elem; top += 1;}public T pop() { ... return stack[top]; }// More code goes here, but I’m out of space!
Why Not Arrays? • Many limitations arise when using arrays • Must specify unchangeable size when createdPirate[] rum = new Pirate[1];Pirate[] h2o = new Pirate[variable];rum = new Pirate[60]; // old rum was lost!h2o = rum; // h20 now alias to rum’s instance • Waste memory requiring maximum size for array// Each Amazon.com customer uses 400+MB of RAM!Rating[] hertz = new Rating[100000000];
Arrays • Provide simple way of storing & accessing data • When needed, moving data around is difficult • Cannot resize an array without copying entire thing • If array allocated too small, then program crashes • Waste memory if too large an array is allocated • But access times are really, really fast
Arrays • Provide simple way of storing & accessing data • When needed, moving data around is difficult • Cannot resize an array without copying entire thing • If array allocated too small, then program crashes • Waste memory if too large an array is allocated • But access times are really, really fast
Better Option • Linked lists adjust size to reflect current needs • Implementation that avoids array’s sizing problems • First recursivestructure you will use this year • There exist many linked list implementations • Best depends on situation and how it will be used • Each approach has own strengths & weaknesses
Better Option • Linked lists adjust size to reflect current needs • Implementation that avoids array’s sizing problems • First recursivestructure you will use this year • There exist many linked list implementations • Best depends on situation and how it will be used • Each approach has own strengths & weaknesses • Recurring refrain: best determined by situation • Understanding each implementation very important
Singly Linked List LinearNode elem next • Linked lists are linear sequence of nodes • “Thingy” is definition of “Node” • Each LinearNode contains: • Element reference to data stored in LinearNode • Link to next LinearNode in linked list
Singly Linked List LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next • Linked lists are linear sequence of nodes • “Thingy” is definition of “Node” • Each LinearNode contains: • Element reference to data stored in LinearNode • Link to next LinearNode in linked list
1st Node Class (of many) public class LinearNode<T> {private Telement;private LinearNode<T> next;public LinearNode(Te, LinearNode<T> n) {element = e;next= n;}public TgetElement() { return element;}public LinearNode<T> getNext() { return next;}public void setElement(TnewE) {element = newE;}public void setNext(LinearNode<T> newNext) {next = newNext;} }
1st Node Class (of many) public class LinearNode<T> {private Telement;private LinearNode<T> next;public LinearNode(Te, LinearNode<T> n) {element = e;next = n;}public TgetElement() { return element;}public LinearNode<T> getNext() { return next;}public void setElement(TnewE) {element = newE;}public void setNext(LinearNode<T> newNext) {next = newNext;} }
Singly Linked List public class SLinkedList<T> {private LinearNode<T> head;private intsize;public SLinkedList() {head = null; // Make an empty listsize = 0;}public booleanisEmpty() { return (head == null);}public TgetFirstElement() {// Handle situation when list is empty return head.getElem();} }
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 3 LinearNode LinearNode LinearNode elem elem elem next next next elem
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 4 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem
Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size 4 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size 4 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
Removing the Head • Resized with each addition or removal • Linked list's headnode has nothing to unlink • No previous node whose nextfield to be updated • Instead, just need to advance where head refershead = head.getNext(); SLinkedList head size 3 LinearNode LinearNode LinearNode elem elem elem next next next
Removing the Head • Resized with each addition or removal • Linked list's headnode has nothing to unlink • No previous node whose nextfield to be updated • Instead, just need to advance where head refershead = head.getNext(); SLinkedList head size 3 LinearNode LinearNode LinearNode elem elem elem next next next
Removing the Head • Resized with each addition or removal • Linked list's headnode has nothing to unlink • No previous node whose nextfield to be updated • Instead, just need to advance where head refershead = head.getNext(); SLinkedList head size 2 LinearNode LinearNode LinearNode elem elem elem next next next
Doubly Linked List DNode Instance prev elem next • Link to previous node in list also in each node • Each DNode contains: • Element (data) reference • Link to nextDNode • Prev(ious) DNodealso linked
Doubly Linked List Sequence of 4 DNodes • Link to previous node in list also in each node • Each DNode contains: • Element (data) reference • Link to next DNode • Prev(ious) DNodealso linked
Doubly Linked List • DNodecould extend LinearNode • next & elem fields are needed by both classes • Only difference is prev field added by DNode • DLinkedListnot subclass of SLinkedList • Both classes define identical methods… • …are entirely different when implemented 1
Your Turn • Get into your groups and complete activity SLinkedList head size 3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next
For Next Lecture • Read 4.6 (& web article) for Friday • How can we use linked lists for Stacks? • When would we want to use linked list-based Stack? • When would linked list-based Stack suck? • Why use singly- vs. doubly-linked lists? • Angel also has Weekly Assignment #8 • Pulls everything together and shows off your stuff • Keep planning out on your classes for Project #1 • Due week from Sat. and requires actual thought