41
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI) IKI10100: Lecture 1 st March 2007 Implementation Of Linked-Lists

Implementation Of Linked-Lists

Embed Size (px)

DESCRIPTION

Implementation Of Linked-Lists. Outline. Linked list nodes Linked list operations Insertion Append Deletion Linked list representation & implementation Other types of linked lists Sorted Doubly-linked Circular. ListNode. ListNode. ListNode. ListNode. A0. A1. A2. A3. first. - PowerPoint PPT Presentation

Citation preview

Implementation Of Linked-ListsRuli Manurung (acknowledgments to Denny & Ade Azurat)
*
Sorted
Doubly-linked
Circular
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.
ListNode
A0
A1
A2
A3
first
ListNode
ListNode
ListNode
adj.
1 in physical contact; touching along all or most of one side
2 near, next, or adjacent
SYN. adjacent
{
current
a
a
current
b
c
d
b
c
d
x
// create a new node
tmp = new ListNode<DataType>();
// create a new node
tmp = new ListNode<DataType>();
tmp.data = x;
// create a new node
tmp = new ListNode<DataType>();
tmp.data = x;
tmp.next = current.next;
// create a new node
tmp = new ListNode<DataType>();
tmp.data = x;
tmp.next = current.next;
current.next = tmp;
// a’s next node is x
current.next = tmp;
// a’s next node is x
current.next = tmp;
x
a
current
b
// last refers to the last node in the linked list
last.next = new ListNode<DataType>();
last = last.next; // adjust last
last.next = null; // adjust next
last
last
a
b
c
d
a
b
c
X
d
last
last
Basic deletion is a bypass in the linked list.
a
b
x
a
b
current
current
Implementing Basic Deletion
Need a reference to node prior to the one to be deleted.
current.next = current.next.next;
Remember!!!
*
If items are stored in contiguous array:
//step through array, outputting each item
for (int index = 0; index < a.length; index++)
System.out.println (a[index]);
// step through list, outputting each item
for(ListNode p=l.first; p!=null; p=p.next)
System.out.println (p.data);
What happens if we want to
Delete the first item?
class MyLinkedList <DataType>
IKI10100: Lecture
Header 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.
A1
A2
A3
header
public class LinkedList<T> implements List
{
// Test if the list is logically empty.
public boolean isEmpty( )
{ return header.next == null; }
public void makeEmpty( )
Index position (int?)
a
x
current
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()
ListItr(ListNode<DataType> node)
IKI10100: Lecture
Example Usage
A method that computes the number of elements in any list:
public static int listSize (List theList)
{
boolean isEmpty() {???}
public ListItr<T> find(T x) {???}
// Cursor to node before node containing x
public ListItr<T> findPrevious(T x) {???}
// Insert x after current cursor position
public void insert(T x, ListItr<T> current) {???}
// Remove (first) node containing x
public void remove(T x) {???}
Mostly straightforward; all routines are short.
ListItr maintains a reference to the list that it is bound to as a private data member.
*
Some routines throw ItemNotFound exceptions.
*
find - O(n)
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
overhead one reference for each node
*
public class LinkedList <T>
ListNode<T> p = header.next;
public class LinkedList <T>
{
Method 3: Recursion
class ListNode <T>
Method 4: Using iterator
while(itr.hasNext())
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.
*
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.
*
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)
{
Other 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.
A
head
tail
prev
next
A
B
C
first
prev
next
2 newNode.prev = current;
3 newNode.next = current.next;
4 newNode.prev.next = newNode;
5 newNode.next.prev = newNode;
6 current = newNode;
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
Sequential access only
http://telaga.cs.ui.ac.id/WebKuliah/java5/docs/api/java/util/package-summary.html
Chapter 6 & 17