Title: Linked Structures
1Linked Structures
- Computer Science and Engineering
2Linked List
- We will study
- The concept of linked structure (list)
- Java Linked list
- State design pattern-based realization of a
linked list
3Consider an Array
- Array is fixed size (non-mutable, static)
- Array insert is difficult
- Default indexing 0..n-1, predefined sequence of
elements - Allows random access
Insert here
N-1
0 1 2 3 4
4Linked List
- A linked list is an empty list or collection of a
number of nodes, each containing one data element
and one (or more) links to other nodes. - Linked lists permit dynamic insertion and removal
of nodes at any point in the list. Linked lists
do not allow random access. - The simplest kind of linked list is a singly
linked list, which has one link per node. This
link points to the next node in the list, or to a
null value if it is the last node. This kind of
list allows sequential access to elements. - It is used where the size and structure of the
data needs to be flexible and dynamic.
5Varieties of Linked List
- Simple linked list
- Linked list with special node(s) for head and/or
tail - Null terminator
- Sentinel as terminator
- Single link, double link, multi-link, circular
linked - Pointer to first and last node
6A node of linked list
public class Node Object item Node
next some times you have explicit
representation for a head and or tail.
7Node Class
8Linked List Operations
- Construct
- Insert (first, last, anywhere)
- Delete (first, last, anywhere)
- Size
- Search for an item
- Process Traverse, iterate to do something
9JDK1.4 Linked List
Object
AbstractCollection
Collection
AbstractList
List
AbstractSequentialList
List, java.io.Serializable, java.lang.Cloneable
LinkedList
10LinkedLists inheritance and related classes
- Most of the linked list functionality is
specified in the interface List. - Serializable defines the disk archiving (object
read/write) functionality. - Cloneable defines the cloning ability.
- LinkedList class defines the variables needed for
storing the header and size. - A ListIterator class offers the Iterator facility.
11Linked List class
//constructors add (index I, Object o) //
inserts add(Object o) //appends addFirst(Object
o) addLast(Object o) Object remove(index
i) boolean remove(Object o) Object
removeFirst() Object removeLast() Object
get(index i) Object getFirst() Object
getLast() Object set(index i, Object o) returns
replaced Object
12Linked List Class (contd.)
ListIerator listIterator(index i) boolean
contains(Object o) clear() int indexOf(Object
o) int lastIndexOf(Object o) // first
occurrence or 1 Object toarray() int size()
13(No Transcript)
14java.util.LinkedList Empty List
object
header
Doubly linked list with a special node
header. For an empty list all three fields point
to the header itself
15Java.util.LinkedList Non-Empty List
16 remove(Object o)
- Locate the Entry e with Object o (Object can be
null). - If no such Entry throw an exception.
- Update pointers
- 3.1 Update previous entrys next to es next
- 3.2 Update es nexts previous to es previous
- 3.3 update size size size 1
17Remove an entry
Step 3.2
Step 3.1
18Remove an entry
Header
Step 2
Object
Object
Object
Entry e
Step 1
19Java Code for Step 3 of remove
-
- e.previous.next e.next //3.1
- e.next.previous e.previous //3.2
- size-- //3.3
20add(int index, Object o)
- Inserts an Entry of Object o before Entry at the
given index. - Retrieve the Entry e at the given index.
- Construct an Entry out of Object o and add it
before e - 2.1 Construct Entry x with o as Object, e as
next and e.previous as previous. - 2.2 Update next pointer of xs previous Entry
- 2.3 Update previous pointer of xs next Entry.
- 2.4 size
21add(int index, Object o) Step 2
Entry e at given index
Entry x
22add(int index, Object o) Step 2
- Let e be the entry at the given index.
-
- Entry x new Entry(o, e, e.previous) //step 2.1
- x.previous.next x //step 2.2
- x.next.previous x //step 2.3
- size
23add(int index, Object o) Step 2
header
Object
Object
Object
Entry e at given index
2.2
2.3
24Iterator Pattern
- When you want to access elements of a collection
sequentially without the knowledge of the
underlying representation. - Ex 1 MineSweeper surroundIterator that presents
you list of all valid surrounding cells (0-8) for
sequential access - Ex 2 Maze sequence of valid neighbors
- Ex 3 winterMonths sequence of all winter months
from a list of all months can be defined for
polymorphic dispatch for various types of
geographic zones - A simple iterator interface hasNext(), next(),
hasPrevious(), previous(), currentElem()
25Java Linked List Iterator
- Implemented as an inner class.
- Review an inner class is a class embedded within
another class. Scope is limited to the embedding
class. - In fact, LinkedList has two inner classes
ListItr and Entry. - A listIterator(int index) index specifies the
starting point in the list for generating the
elements of the iterator. You may request a
partial list Iterator.
26Using a List
- Problem Make a list of all routes in city map.
- class Route int origin
- int dest
- Route (int a, int b)
- LinkedList map new LinkedList()
- map.add( route(23,45))
- etc.
- //print me a list of all train routes
- // assume a trainIterator is available
- //think about the implementation of trainIterator
27Using an Iterator
- // generate the iterator
- ListIterator tR map.trainIterator()
- while (tr.hasNext())
- System.out.println(tr.next())
-