Title: Implementation Of LinkedLists
1Implementation Of Linked-Lists
2Outline
- Linked list nodes
- Linked list operations
- Insertion
- Append
- Deletion
- Linked list representation implementation
- Other types of linked lists
- Sorted
- Doubly-linked
- Circular
3Linked Lists
- Stores a collection of items non-contiguously.
- Each item in the list is stored with an
indication of where the next item is. - Must know where first item is.
- The list will be a chain of objects, called
nodes, of type ListNode that contain the data and
a reference to the next ListNode in the list. - Allows addition or deletion of items in the
middle of collection with only a constant amount
of data movement. Contrast this with array.
4ListNode Definition
- public class ListNode ltDataTypegt
-
- DataType data
- ListNodeltDataTypegt next
- // constructors
- ListNode(DataType d, ListNodeltDataTypegt n)
-
- data d
- next n
-
- ListNode(DataType d)
- this (d, null)
- ListNode()
- this (null)
5Linked List Insertion
current
- Insert X immediately after current position
6Implementing Insertion Step By Step
- Insertion immediately after current position
- // create a new node
- tmp new ListNodeltDataTypegt()
7Implementing Insertion Step By Step
- Insertion immediately after current position
- // create a new node
- tmp new ListNodeltDataTypegt()
- // place x in the element field
- tmp.data x
x
8Implementing Insertion Step By Step
- Insertion immediately after current position
- // create a new node
- tmp new ListNodeltDataTypegt()
- // place x in the element field
- tmp.data x
- // xs next node is b
- tmp.next current.next
x
9Implementing Insertion Step By Step
- Insertion immediately after current position
- // create a new node
- tmp new ListNodeltDataTypegt()
- // place x in the element field
- tmp.data x
- // xs next node is b
- tmp.next current.next
- // as next node is x
- current.next tmp
x
10Implementing Insertion Shorter Version
- A shorter version
- // create a new node
- tmp new ListNodeltDataTypegt(x,current.next)
- // as next node is x
- current.next tmp
x
11Implementing Insertion Shorter Version
- A shorter version
- // create a new node
- tmp new ListNodeltDataTypegt(x,current.next)
- // as next node is x
- current.next tmp
x
12Implementing Insertion Shortest Version
- An even shorter version
- // create a new node
- current.next new ListNodeltDataTypegt(x,current.ne
xt)
x
13Implementing Append
- Insert X immediately at the end of the list
- // last refers to the last node in the linked
list - last.next new ListNodeltDataTypegt()
- last last.next // adjust last
- last.data x // place x in the node
- last.next null // adjust next
- Most efficient approach
- last last.next new ListNode (x, null)
last
last
14Implementing Basic Deletion
- Delete an item immediately after current position
- Basic deletion is a bypass in the linked list.
15Implementing Basic Deletion
- Need a reference to node prior to the one to be
deleted. - current.next current.next.next
16Important Note
- Remember!!!
- What we store in a ListNode is a reference to the
object, NOT the object itself nor a copy of the
object!
17Iterate Through The Linked List
- If items are stored in contiguous array
- //step through array, outputting each item
- for (int index 0 index lt a.length index)
- System.out.println (aindex)
- If items are stored in a linked list
- // step through list, outputting each item
- for(ListNode pl.first p!null pp.next)
- System.out.println (p.data)
18Implementing a Linked List
- So what would a Linked List implementation look
like? - What happens if we want to
- Delete the first item?
- Insert an item before the first item?
class MyLinkedList ltDataTypegt // Field
ListNodeltDataTypegt first // Methods
void insert(DataType x, ???) void
delete(DataType x, ???) void append(DataType
x) ...
19Header Nodes
- Deletion of first item and insertion of new first
item are special cases. - Can be avoided by using header node
- contains no data, but serves to ensure that first
"real" node in linked has a predecessor. - To go to the first element, set current to
header.next - List is empty if header.next null
- Searching routines will skip header.
20Linked Lists List Implementation (partial)
- public class LinkedListltTgt implements List
-
- ListNodeltTgt header
- // Constructor
- public LinkedList()
- header new ListNodeltTgt(null)
- // Test if the list is logically empty.
- public boolean isEmpty( )
- return header.next null
- // Make the list logically empty
- public void makeEmpty( )
- header.next null
-
21Representing the current position
- How do we specify where an operation should
occur? - Index position (int?)
- ListNode
// Methods void insert(DataType x, ???) void
delete(DataType x, ???) void insert(DataType x,
int current) void delete(DataType x, int
current) void insert(DataType x, ListNode
current) void delete(DataType x, ListNode
current)
22List Iterator Class
- Maintains a notion of the current position (aka
cursor). - The List class provides methods that do not
depend on any position (such as isEmpty, and
makeEmpty). - A List iterator (ListItr) provides other methods
such which act on the current position stored in
the iterator - next() / advance()
- hasNext() / isValid()
- retrieve()
23Linked List Iterator Implementation
- public class ListItr ltDataTypegt
-
- ListNodeltDataTypegt current // Current
position - ListItr(ListNodeltDataTypegt node)
-
- public boolean hasNext()
-
- public void next()
-
- public DataType retrieve()
-
-
24Example Usage
- A method that computes the number of elements in
any list - public static int listSize (List theList)
-
- int size 0
- ListItr itr
- for(itrtheList.first()itr.hasNext()itr.next
()) - size
- return size
25Linked Lists Implementation
- public class List ltTgt
-
- // Header node
- private ListNodeltTgt header
-
- // Check if list is empty
- boolean isEmpty() ???
- // Make the list empty
- void makeEmpty () ???
- // Cursor to header node
- public ListItrltTgt zeroth() ???
- // Cursor to first node
- public ListItrltTgt first() ???
- // Cursor to (first) node containing x
- public ListItrltTgt find(T x) ???
26Java Implementations
- Mostly straightforward all routines are short.
- ListItr maintains a reference to the list that it
is bound to as a private data member. - Because ListItr is in the same package as List,
if List's data access is (package) friendly, it
can be accessed by ListItr.
27Note on Exceptions
- Some routines throw ItemNotFound exceptions.
- However, do not overuse exceptions, because they
must always be caught or propagated. As an
example, advance does not throw an exception,
even if we have already advanced past the end.
Otherwise, listSize would need a try/catch block.
28Linked List Properties
- Running Time Analysis
- insert next, prepend - O(1)
- delete next, delete first - O(1)
- find - O(n)
- retrieve current position - O(1)
- Advantages
- Growable (compared to array)
- Easy (quick) in read/insert/delete the first and
the last element (if we also store the last
position not just the head/current position) - Disadvantages
- Calling to operator new (compare to array)
- overhead one reference for each node
29Print all elements of Linked List
- Method 1 Without Iterator, Simple Looping
- public class LinkedList ltTgt
-
- public void print()
-
- // step through list, outputting each
item - ListNodeltTgt p header.next
- while (p ! null)
-
- System.out.println (p.data)
- p p.next
-
-
30Print all elements of Linked List(2)
- Method 2 Without Iterator, Using Recursion
- public class LinkedList ltTgt
-
- private void printRec (ListNodeltTgt node)
-
- if (node ! null)
-
- System.out.println (node.data)
- printRec (node.next)
-
-
- public void print ()
- printRec (header.next)
31Print all elements of Linked List(3)
- Method 3 Recursion
- class ListNode ltTgt
-
- public void print ()
-
- System.out.println (data)
- if (next ! null)
- next.print ()
-
-
- class LinkedList ltTgt
-
- public void print()
-
- if (header.next ! null)
- header.next.print ()
-
32Print all elements of Linked List(4)
- Method 4 Using iterator
- class LinkedList ltTgt
-
- public void print()
-
- ListItrltListNodeltTgtgt itr first()
- while(itr.hasNext())
-
- itr.next()
- System.out.println(itr.retrieve())
-
-
33Sorted Linked Lists
- Maintains items in sorted order.
- Almost all operations are the same as linked
lists, except for insert. - In fact, a sorted linked list IS-A linked list,
suggesting that inheritance may be used. Extend a
SortListItr from ListItr. - However, items in a sorted linked list should be
Comparable.
34Simple Implementation
- Use inheritance, and create Insert method
- public void insert( Comparable X )
- Note however, that in this implementation, we
have no assurance that the list is sorted,
because it can be accessed by either a
SortListItr or a ListItr. - See the textbook for details.
35Important Note
- ListItr used the equals member function in find
and remove. - Must make sure MyInteger has an equals member
function if we are storing a linked list of
MyIntegers. - Signature MUST BE
- public boolean equals( Object Rhs )
- The following signature is wrong!
- public boolean equals( Comparable Rhs )
- If you try it, the method from class Object will
be inherited and used - public boolean equals (Object obj)
-
- return (this obj)
36Other Linked Lists
- Doubly-linked lists Each list node stores both
the previous and next nodes in the list. Useful
for traversing linked lists in both directions. - Circular-linked lists Last node's next
references the first node. Works with or without
headers.
37Doubly-linked lists Wrong InsertNext
- newNode new DoublyLinkedListNode ( x )
- 1 newNode.prev current
- 2 newNode.prev.next newNode
-
-
2
1
38Doubly-linked lists insertNext
- 1 newNode new DoublyLinkedListNode (x)
- 2 newNode.prev current
- 3 newNode.next current.next
- 4 newNode.prev.next newNode
- 5 newNode.next.prev newNode
- 6 current newNode
39Doubly-linked lists DeleteCurrent
- current.prev.next current.next
- current.next.prev current.prev
- current current.prev
40JDK package java.util
- Collection - List - LinkedList
- Iterator - ListIterator
41Summary
- ListNode
- List, LinkedList
- Iterator class
- a class that maintains a current position and
performs all routines that depend on knowing the
position in the list. - Advantage Disadvantage of linked list
- Growable
- Overhead a pointer, new operator
- Sequential access only