1 / 35

Question of the Day

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.

allen-perez
Download Presentation

Question of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Question of the Day • What three letter word completes the first word and starts the second one: DON ??? CAR

  2. 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

  3. Lecture 17:Arrays versus Linked-lists

  4. 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

  5. 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!

  6. 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];

  7. 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

  8. 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

  9. When Memory Is Too Large

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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;} }

  15. 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;} }

  16. 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();} }

  17. Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head elem

  18. Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem

  19. Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem

  20. Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem

  21. Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head n elem

  22. Inserting at Head Algorithm addFirst(elem)Node<T> n = new Node<T>();n.setElement(elem);n.setNext(head);head = n head

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. Removing the Head

  29. Removing the Head

  30. 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

  31. 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

  32. 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

  33. 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

  34. Your Turn • Get into your groups and complete activity head

  35. 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

More Related