Question of the Day

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

description

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. - PowerPoint PPT Presentation

Transcript of Question of the Day

Page 1: Question of the Day

Question of the Day

What three letter word completes the first word and starts the second one:

DON ??? CAR

Page 2: Question of the Day

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

Page 3: Question of the Day

LECTURE 17:ARRAYS VERSUS LINKED-LISTS

Page 4: Question of the Day

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

Computers Use Collections

Page 5: Question of the Day

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

Page 6: Question of the Day

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

Page 7: Question of the Day

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

Page 8: Question of the Day

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

Page 9: Question of the Day

When Memory Is Too Large

Page 10: Question of the Day

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

Page 11: Question of the Day

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

Recurring refrain: best determined by situation Understanding each implementation very

important

Better Option

Page 12: Question of the Day

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

Page 13: Question of the Day

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

Page 14: Question of the Day

public class Node<T> {private T elem;private Node<T> next;public Node(T e, Node<T> n) { elem = e; next = n;}public T getElement() { return elem;}public Node<T> getNext() { return next;}public void setElement(T newE) { elem = newE;}public void setNext(Node<T> newNext) { next = newNext;}

}

1st Node Class (of many)

Page 15: Question of the Day

public class Node<T> {private T elem;private Node<T> next;public Node(T e, Node<T> n) { elem = e; next = n;}public T getElement() { return elem;}public Node<T> getNext() { return next;}public void setElement(T newE) { elem = newE;}public void setNext(Node<T> newNext) { next = newNext;}

}

1st Node Class (of many)

Page 16: Question of the Day

public class SList<T> {private Node<T> 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

Page 17: Question of the Day

Inserting at Head

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

head

elem

Page 18: Question of the Day

Inserting at Head

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

head

elem

n

Page 19: Question of the Day

Inserting at Head

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

head

elem

n

Page 20: Question of the Day

Inserting at Head

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

head

elem

n

Page 21: Question of the Day

Inserting at Head

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

head

elem

n

Page 22: Question of the Day

Inserting at Head

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

head

Page 23: Question of the Day

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

Page 24: Question of the Day

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

Page 25: Question of the Day

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

Page 26: Question of the Day

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

Page 27: Question of the Day

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

Page 28: Question of the Day

Removing the Head

Page 29: Question of the Day

Removing the Head

Page 30: Question of the Day

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

refershead = head.next;

head

Page 31: Question of the Day

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

refershead = head.next;

head

Page 32: Question of the Day

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

refershead = head.next;

head

Page 33: Question of the Day

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

refershead = head.next;

head

Page 34: Question of the Day

Your Turn

Get into your groups and complete activity

head

Page 35: Question of the Day

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