Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR."— 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  Key completes the first word (donkey) and a key starts a car

3

4  Computers best processing huge data sets  Perform specific tasks and output results: Analyze what items often bought together Print & mail tuition bills Compute receipt for groceries  Filter & provide few items for user to choose from Personalized recommendations from Amazon Google’s list of matching web pages Systems to determine stock to be reordered  Most of year examines how to organize data  Space and time issues must be evaluated as factor Computers Use Collections

5  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 { private int count; private T[] s; public ScoreLog(){ s = (T[]) new Object[100]; } public void addScore(T newScore) { s[count] = newScore; count += 1; } public T getLastScore() { return s[count - 1]; } // More code goes here, but I’m out of space! How Could We Do This?

6 Why Not Arrays?  Many limitations arise when using arrays  Must specify unchangeable size when created Pirate[] 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  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

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

9 When Memory Is Too Large

10  Linked lists adjust size to reflect current needs  Implementation that avoids array’s sizing problems  First recursive structure 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

11  Linked lists adjust size to reflect current needs  Implementation that avoids array’s sizing problems  First recursive structure 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 best determined by situation  Recurring refrain: best determined by situation  Understanding each implementation very important Better Option

12  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 elem Node node

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

14 public class Node { private T elem; private Node next; public Node(T e, Node n) { elem = e; next = n; } public T getElement() { return elem; } public Node getNext() { return next; } public void setElement(T newE) { elem = newE; } public void setNext(Node newNext) { next = newNext; } } 1 st Node Class (of many)

15

16 public class SList { private Node head; private int size; public SList() { head = null; // Make an empty list size = 0; } public boolean isEmpty() { return (head == null); } public T getFirstElement() { // Handle situation when list is empty return head.getElem(); } } Singly Linked List

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

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

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

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

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

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

23 Removing an Internal Node  Linked list's length equals number of elements  Requires unlinking Node from 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 Node from 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 Node from 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 Node from 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 Node from 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

30  Resized with each addition or removal  Linked list's head node has nothing to unlink  No previous node whose next field to be updated  Instead, just need to advance where head refers head = head.next; head

31 Removing the Head  Resized with each addition or removal  Linked list's head node has nothing to unlink  No previous node whose next field to be updated  Instead, just need to advance where head refers head = head.next; head

32 Removing the Head  Resized with each addition or removal  Linked list's head node has nothing to unlink  No previous node whose next field to be updated  Instead, just need to advance where head refers head = head.next; head

33 Removing the Head  Resized with each addition or removal  Linked list's head node has nothing to unlink  No previous node whose next field to be updated  Instead, just need to advance where head refers head = 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


Download ppt "Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR."

Similar presentations


Ads by Google