Upload
candie
View
33
Download
0
Tags:
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
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
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.
LECTURE 21:ARRAYS VERSUS LINKED-LISTS
CSC 212 – Data Structures
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?
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];
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
Arrays
When Memory Is Too Large
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
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
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
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
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)
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Removing the Head
Removing the Head
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
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
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
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
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
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
elemnextelemnextelemnextelemnext
Your Turn
Get into your groups and complete activity
LinearNode LinearNode LinearNode LinearNode
headsize 3
SLinkedList
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