Computer Science and Engineering Linked Structures Computer Science and Engineering 2/21/2019 B.Ramamurthy
Linked List We will study: The concept of linked structure (list) Java Linked list State design pattern-based realization of a linked list 2/21/2019 B.Ramamurthy
Consider 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 2/21/2019 B.Ramamurthy
Linked 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. 2/21/2019 B.Ramamurthy
Varieties 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 2/21/2019 B.Ramamurthy
A node of linked list public class Node { Object item; Node next; } some times you have explicit representation for a head and or tail. 2/21/2019 B.Ramamurthy
Node Class Node // data Object item; Node next; // methods public Node() public Object getElement() public Node getNext() public void setElement(Object newItem) public void setNext(Node newNext) public boolean equals(Node other) public String toString() 2/21/2019 B.Ramamurthy
Linked List Operations Construct Insert (first, last, anywhere) Delete (first, last, anywhere) Size Search for an item Process: Traverse, iterate to do something 2/21/2019 B.Ramamurthy
JDK1.4 Linked List Object AbstractCollection Collection AbstractList AbstractSequentialList List, java.io.Serializable, java.lang.Cloneable LinkedList 2/21/2019 B.Ramamurthy
LinkedList’s 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. 2/21/2019 B.Ramamurthy
Linked 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 2/21/2019 B.Ramamurthy
Linked 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(); 2/21/2019 B.Ramamurthy
2/21/2019 B.Ramamurthy
java.util.LinkedList Empty List previous next object header: Doubly linked list with a special node header. For an empty list all three fields point to the header itself 2/21/2019 B.Ramamurthy
Java.util.LinkedList Non-Empty List Object null previous next Header 2/21/2019 B.Ramamurthy
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 entry’s next to e’s next 3.2 Update e’s next’s previous to e’s previous 3.3 update size; size = size –1 2/21/2019 B.Ramamurthy
Remove an entry Object null previous next Header Entry e Step 3.2 2/21/2019 B.Ramamurthy
Remove an entry null previous next Header Step 2 Object Object Object Entry e Step 1 2/21/2019 B.Ramamurthy
Java Code for Step 3 of remove … e.previous.next = e.next; //3.1 e.next.previous = e.previous; //3.2 size--; //3.3 2/21/2019 B.Ramamurthy
add(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 x’s previous Entry 2.3 Update previous pointer of x’s next Entry. 2.4 size++ 2/21/2019 B.Ramamurthy
add(int index, Object o): Step 2 header previous next Entry e at given index Object o previous next Step 2.1 Entry x 2/21/2019 B.Ramamurthy
add(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++; 2/21/2019 B.Ramamurthy
add(int index, Object o): Step 2 header previous next header Object Object Object Entry e at given index 2.2 2.3 Object o previous next Step 2.1 2/21/2019 B.Ramamurthy
Iterator 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() 2/21/2019 B.Ramamurthy
Java 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. 2/21/2019 B.Ramamurthy
Using 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 2/21/2019 B.Ramamurthy
Using an Iterator // generate the iterator ListIterator tR = map.trainIterator(); while (tr.hasNext()) System.out.println(tr.next()); 2/21/2019 B.Ramamurthy