38
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

  • Upload
    candie

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Question of the Day

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?

Page 2: Question of the Day

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?

Everybody on the boat was married.

Page 3: Question of the Day

LECTURE 21:ARRAYS VERSUS LINKED-LISTS

CSC 212 – Data Structures

Page 4: Question of the Day

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 int top;private T[] Stack;public ArrayStack(){ s = (T[]) new Object[100]; }public void push(T elem) { stack[top] = elem; top += 1;}public T pop() { ... return stack[top]; }// More code goes here, but I’m out of space!

How Could We Do This?

Page 5: 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 6: 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 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

When Memory Is Too Large

Page 9: 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 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

Recurring refrain: best determined by situation Understanding each implementation very

important

Better Option

Page 11: Question of the Day

elemnext

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

Page 12: Question of the Day

elemnextelemnextelemnextelemnext

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

Page 13: Question of the Day

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

}

1st Node Class (of many)

Page 14: Question of the Day

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

}

1st Node Class (of many)

Page 15: Question of the Day

public class SLinkedList<T> {private LinearNode<T> head;private int size;public SLinkedList() { 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 16: Question of the Day

elemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1

elem

LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 17: Question of the Day

elemnextelemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1

elem

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

n

Page 18: Question of the Day

elemnextelemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1

elem

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

n

Page 19: Question of the Day

elemnextelemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1

elem

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

n

Page 20: Question of the Day

elemnextelemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1

elem

LinearNode LinearNode LinearNode LinearNode

SLinkedList

n

headsize 3

Page 21: Question of the Day

elemnextelemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1

elem

LinearNode LinearNode LinearNode LinearNode

headsize 4

SLinkedList

n

Page 22: Question of the Day

elemnextelemnextelemnextelemnext

Inserting at Head

Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1LinearNode LinearNode LinearNode LinearNode

headsize 4

SLinkedList

Page 23: Question of the Day

Removing an Internal Node

Linked list's length equals number of elements

Requires unlinking LinearNode from linked list Nothing fancy needed, just adjust previous next link

LinearNode 0nly existed via links, so this does it all

Page 24: Question of the Day

elemnextelemnextelemnextelemnext

Removing an Internal Node

Linked list's length equals number of elements

Requires unlinking LinearNode from linked list Nothing fancy needed, just adjust previous next link

LinearNode 0nly existed via links, so this does it all

LinearNode LinearNode LinearNode LinearNode

headsize 4

SLinkedList

Page 25: Question of the Day

elemnextelemnextelemnextelemnext

Removing an Internal Node

Linked list's length equals number of elements

Requires unlinking LinearNode from linked list Nothing fancy needed, just adjust previous next link

LinearNode 0nly existed via links, so this does it all

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 26: Question of the Day

elemnextelemnextelemnextelemnext

Removing an Internal Node

Linked list's length equals number of elements

Requires unlinking LinearNode from linked list Nothing fancy needed, just adjust previous next link

LinearNode 0nly existed via links, so this does it all

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 27: Question of the Day

elemnextelemnextelemnextelemnext

Removing an Internal Node

Linked list's length equals number of elements

Requires unlinking LinearNode from linked list Nothing fancy needed, just adjust previous next link

LinearNode 0nly existed via links, so this does it all

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 28: Question of the Day

elemnextelemnextelemnextelemnext

Removing an Internal Node

Linked list's length equals number of elements

Requires unlinking LinearNode from linked list Nothing fancy needed, just adjust previous next link

LinearNode 0nly existed via links, so this does it all

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 29: Question of the Day

Removing the Head

Page 30: Question of the Day

Removing the Head

Page 31: Question of the Day

elemnextelemnextelemnext

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

LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 32: Question of the Day

elemnextelemnextelemnext

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

LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 33: Question of the Day

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.getNext(); elemnextelemnextelemnext

Removing the Head

LinearNode LinearNode LinearNode

headsize 2

SLinkedList

Page 34: Question of the Day

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

elem

DNode Instance

nextprev

Page 35: Question of the Day

Link to previous node in list also in each node

Each DNode contains: Element (data) reference Link to next DNode Prev(ious) DNode also linked

Doubly Linked List

Sequence of 4 DNodes

Page 36: Question of the Day

Doubly Linked List

DNode could extend LinearNode next & elem fields are needed by both

classes Only difference is prev field added by DNode

DLinkedList not subclass of SLinkedList Both classes define identical methods… …are entirely different when implemented

1

Page 37: Question of the Day

elemnextelemnextelemnextelemnext

Your Turn

Get into your groups and complete activity

LinearNode LinearNode LinearNode LinearNode

headsize 3

SLinkedList

Page 38: Question of the Day

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