350 likes | 434 Views
Question of the Day. What three letter word completes the first word and starts the second one: DON ??? CAR. Question of the Day. What three letter word completes the first word and starts the second one: DON ??? CAR Key completes the first word (donkey) and a key starts a car.
E N D
Question of the Day • What three letter word completes the first word and starts the second one: DON ??? CAR
Question of the Day • What three letter word completes the first word and starts the second one: DON ??? CAR • Key completes the first word (donkey) and a key starts a car
Computers Use Collections • Computers best processing huge data sets • Perform specific tasks and output results:Analyze what items often bought togetherPrint & mail tuition billsCompute receipt for groceries • Filter & provide few items for user to choose fromPersonalized recommendations from AmazonGoogle’s list of matching web pagesSystems to determine stock to be reordered • Most of year examines how to organize data • Space and time issues must be evaluated as factor
How Could We Do This? • Already know simple way to organize data: array • Primitives or references can be stored in entries • Create matrices with entries referring to another array • Arrays of generic type can limit code rewriting public class ScoreLog<T> {private intcount;private T[] s;public ScoreLog(){ s = (T[]) new Object[100]; }public void addScore(TnewScore) {s[count] = newScore;count += 1;}public TgetLastScore() { return s[count - 1]; }// 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 elem node Node • Linked lists are linear sequence of nodes • “Thingy” is definition of “Node” • Each Node contains: • Element reference to data stored in Node • Link to next Node in linked list
Singly Linked List • Linked lists are linear sequence of nodes • “Thingy” is definition of “Node” • Each Node contains: • Element reference to data stored in Node • Link to next Node in linked list
1st Node Class (of many) public class Node<T> {private Telem;private Node<T> next;public Node(Te, Node<T> n) {elem= e;next= n;}public TgetElement(){ return elem;}public Node<T> getNext(){ return next;}public void setElement(TnewE) {elem= newE;}public void setNext(Node<T> newNext) {next = newNext;} }
1st Node Class (of many) public class Node<T> {privateTelem;private Node<T> next;public Node(Te, Node<T> n) {elem= e;next= n;}publicTgetElement(){ return elem;}public Node<T> getNext(){ return next;}public void setElement(TnewE) {elem= newE;}public void setNext(Node<T> newNext) {next = newNext;} }
Singly Linked List public class SList<T> {private Node<T> head;private intsize;public SList() {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)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head elem
Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem
Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem
Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem
Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem
Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking Nodefrom the linked list • Nothing fancy needed, just adjust previous next link • Node 0nly existed via links, so this does all we need
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking Nodefrom the linked list • Nothing fancy needed, just adjust previous next link • Node 0nly existed via links, so this does all we need head
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking Nodefrom the linked list • Nothing fancy needed, just adjust previous next link • Node 0nly existed via links, so this does all we need head
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking Nodefrom the linked list • Nothing fancy needed, just adjust previous next link • Node 0nly existed via links, so this does all we need head
Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking Nodefrom the linked list • Nothing fancy needed, just adjust previous next link • Node 0nly existed via links, so this does all we need head
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.next; head
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.next; head
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.next; head
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.next; head
Your Turn • Get into your groups and complete activity head
For Next Lecture • Read GT3.3 – 3.4 for Wednesday • How to insert an element into the middle of list? • Item in the middle of list can be accessed, how? • Are we limited to singly-linked lists? • Angel also has programming assignment #1 • Pulls everything together and shows off your stuff