Abstract Data structures - Spruce Creek High School

Download Report

Transcript Abstract Data structures - Spruce Creek High School

David Weinberg presents
Linked Lists: The Background
Linked Lists are similar to ArrayLists in
their appearance and method of
manipulation
 They do NOT NOT NOT NOT NOT have
indices, if it does, you’re a noob. (Yes, you)
 They are composed of objects called
nodes that are “linked” together through
references to other nodes

Linked List: The Theory
Data 2
This
is 3a Node.
Data
Data 4
This is known as the
This is known as the
Data
Each
node
has references
Head
of the
Linked
tail of the Linked List
Listto other nodes which are
considered
its “neighbors”
•Think of this
as an array without indexes…
•Each Node knows only its left andThis
rightrepresents
neighbors the
data a node holds
but not all of the elements in the array
Linked List: An Example
Suppose
One
The
Now
approach
amount
you
suppose
were
would
ofyou
managing
operations
beused
an array,
aaLinked
required
test…
lets List:
discuss
to remove
this:a question or add a
question or swap a question can be quite large (for very large arrays)
Arrays do not have dynamic length, but there is a bigger problem.
Our Example Linked
array: List:
Q1
Q1
Q2
Q2
Q3
Q3
Q4
Q5
Q4
Q5
First you
Now
suppose
must you
access
wanted
the index,
to remove
which
Q2is O(1) efficiency
After
this must
you
shiftthe
all
of
indices
to
the
left,forsuch
asis O(1)
Therefore,
themust
efficiency
of information
thetheremove
an array
is
Now you
remove
atoperation
this
index,
which
making
O(n). Q3 at index 1, rather than index 2.
The Node
Now
to remove
we
must
is now
remove
a removed
node,the
weNode
from
first access
the
fromlist
the
and
this
listnode,
isbyreturned
setting
whichthe
orisdeleted,
reference
O(1)- assuming
from
we have
Q1
depending
to Q2found
asonQ1
the
itto
already
users
Q3. Efficiency:
preference.
(Best case)-.
O(1)
The total efficiency for this Linked List
is O(1) which is must faster than an array, O(n).
Linked List: The Types

There are 3 types of Linked Lists
The final
first has
second
typeisalready
is
doubly
circularly
been
Linked
Linked
introduced,
Lists:
Lists:Linear or Singular Linked
Lists:
Q1
Q1
Q1
Q2
Q2
Q2
Q3
Q3
Q3
Q4
Q4
Q4
Characteristics:
Characteristics:
•Each Node points to its left
“right”
andneighbor
right neighbor
only
•Circularly Linked Lists are either Linear or Doubly
•The
•A
TailTail
is commonly
is considered
used
NULL,
and is
which
the last
signifies
node the end
Linked Lists
•You only
can only
traverse
traverse
bothrightwards
forwards and
starting
backwards
at the Head
starting
•Their tail points to their head, and if its doubly linked,
at either end
their head points to their tail.
•All circularly linked lists have both a head and a tail
•You can iterate through the Linked List both forwards,
backwards and also through it multiple times with only
using the getLeft() and getRight() methods
Q5
Q5
Q5
Linked List: The Efficiency

Earlier, the remove method for the Linked List was discussed, here
is a table of Linked List efficiency (Remember, its O(n) for all cases
Other
Operations of LL vs. Arrays
in an array type data
structure):
Operation
Array Efficiency LL Efficiency
Comments
Add or Remove Operation for the Linked List
Each index or node
Location of
Worst Case
Tail
Type
Search
O(n)Linear
O(n)- Linear
might have to be
Insertion
Efficiency
searched
Front
O(1)
Depending on
No All
Any
Random
O(n)
Delete
implementation,
Occurrences of An
O(n)
O(n)
End
ArrayO(n)
can equal
Object
basic O(1)
LL Efficiency
Front
Yes
Any
Random
O(n)
End
O(1)
Linked List: Implementation

Implementing a Node:
public class ListNode
{
public Object value;
//Get+Sets needed if private
public ListNode next;
//Get+Sets needed if private
//public ListNode prev;
//If doubly linked.
/*Constructors*/
ListNode visualization
}
Value<Object>
Next<ListNode>
-This has its own value and
Next node-
Linked List: Implementation

Implementing a Linear Linked List:
public class LinearLinkedList:
private ListNode head;
public boolean isEmpty() {){/*Implemented Later*/)};
public void addFirst(Object info){/*Implemented Later*/};
public void addLast(Object info){/*Implemented Later*/};
public void removeFirst(){/*Implemented Later*/};
public void removeLast(){/*Implemented Later*/};
public void remove(Object info) {/*Implemented Later*/};
public void toString() {/*Implemented Later*/};
Q1
Q2
Q3
Q4
Q5
Linked List: isEmpty();

Linear:
if(head == null)
return true;
return false;

Just checks to see if there is a
starting node
Doubly:
if(head == null)
return true;
return false;

Circular Doubly Linked List:
if(head == null)
return true;
return false;
Linked List: addFirst(Object info);

Linear
Circular
Doubly Doubly
if(isEmpty())
head = new ListNode(info, null,
null);null);
else
else
{{
head = new ListNode(info, head);
ListNode
head;
ListNode current
current =
= head;
head =Q1
new ListNode(info,
null);
Info
Q2 head,
head = new ListNode(info,
head, tail);Q3
current.setPrev(head);
current.setPrev(head);
}
tail.setNext(head);
}
Q1
Q2
Q3
Q4
Q4
Q5
Q5
Linked List: Parsing

To manipulate a Linked List, you parse through it using the
pointers with recursion:
/*Start the method at the head for the first count if you want the LL’s length*/
public int count(ListNode current)
{
if(current != null)
int total = 1;
if(current.getRight() != null)
total += count(current.getRight());
/* Used if its doubly Linked!*/
if(current.getLeft() != null)
total += count(current.getLeft());
return total;
}