Linked Structures Computer Science and Engineering 1232022 B

Linked Structures Computer Science and Engineering 1/23/2022 B. Ramamurthy 1

Linked List We will study: The concept of linked structure (list) Java Linked list State design pattern-based realization of a linked list 1/23/2022 B. Ramamurthy 2

Consider an 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 234 1/23/2022 B. Ramamurthy 3

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. 1/23/2022 B. Ramamurthy 4

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 1/23/2022 B. Ramamurthy 5

A node of linked list public class Node { Object item; Node next; } some times you have explicit representation for a head and or tail. 1/23/2022 B. Ramamurthy 6

Node Class Node // data Object item; Node next; // methods public Node() public Object get. Element() public Node get. Next() public void set. Element(Object new. Item) public void set. Next(Node new. Next) public boolean equals(Node other) public String to. String() 1/23/2022 B. Ramamurthy 7

Linked List Operations Construct Insert (first, last, anywhere) Delete (first, last, anywhere) Size Search for an item Process: Traverse, iterate to do something 1/23/2022 B. Ramamurthy 8

JDK 1. 4 Linked List Object Abstract. Collection Abstract. List Abstract. Sequential. List Linked. List 1/23/2022 B. Ramamurthy List, java. io. Serializable, java. lang. Cloneable 9

Linked. List’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. Linked. List class defines the variables needed for storing the header and size. A List. Iterator class offers the Iterator facility. 1/23/2022 B. Ramamurthy 10

Linked List class //constructors add (index I, Object o); // inserts add(Object o); //appends add. First(Object o); add. Last(Object o); Object remove(index i); boolean remove(Object o); Object remove. First(); Object remove. Last(); Object get(index i); Object get. First(); Object get. Last(); Object set(index i, Object o); returns replaced Object 1/23/2022 B. Ramamurthy 11

Linked List Class (contd. ) List. Ierator list. Iterator(index i); boolean contains(Object o); clear(); int index. Of(Object o); int last. Index. Of(Object o); // first occurrence or – 1 Object[] toarray(); int size(); 1/23/2022 B. Ramamurthy 12

1/23/2022 B. Ramamurthy 13

java. util. Linked. List Empty List previous object next header: Doubly linked list with a special node header. For an empty list all three fields point to the header itself 1/23/2022 B. Ramamurthy 14

Java. util. Linked. List Non-Empty List previous next null Object 1/23/2022 Object B. Ramamurthy Header Object 15

remove(Object o) 1. Locate the Entry e with Object o (Object can be null). 2. If no such Entry throw an exception. 3. 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 1/23/2022 B. Ramamurthy 16

Remove an entry previous next null Header Step 3. 2 Object Entry e Step 3. 1 1/23/2022 B. Ramamurthy 17

Remove an entry previous next null Header Step 2 Object Entry e Step 1 1/23/2022 B. Ramamurthy 18

Java Code for Step 3 of remove … e. previous. next = e. next; //3. 1 e. next. previous = e. previous; //3. 2 size--; //3. 3 … 1/23/2022 B. Ramamurthy 19

add(int index, Object o) Inserts an Entry of Object o before Entry at the given index. 1. Retrieve the Entry e at the given index. 2. 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++ 1/23/2022 B. Ramamurthy 20

add(int index, Object o): Step 2 previous next header Object Entry e at given index previous next Object o 1/23/2022 B. Ramamurthy Step 2. 1 Entry x 21

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++; … 1/23/2022 B. Ramamurthy 22

add(int index, Object o): Step 2 previous next header Object 2. 2 previous Object Entry e at given index 2. 3 next Object o 1/23/2022 header B. Ramamurthy Step 2. 1 23

Iterator Pattern When you want to access elements of a collection sequentially without the knowledge of the underlying representation. Ex 1: Mine. Sweeper: surround. Iterator that presents you list of all valid surrounding cells (0 -8) for sequential access Ex 2: Maze: sequence of valid neighbors Ex 3: winter. Months: 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: has. Next(), next(), has. Previous(), previous(), current. Elem() 1/23/2022 B. Ramamurthy 24

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, Linked. List has two inner classes: List. Itr and Entry. A list. Iterator(int index) : index specifies the starting point in the list for generating the elements of the iterator. You may request a partial list Iterator. 1/23/2022 B. Ramamurthy 25

Using a List Problem: Make a list of all routes in city map. class Route { int origin; int dest; Route (int a, int b){ }} Linked. List map = new Linked. List(); map. add( route(23, 45)); etc. //print me a list of all train routes // assume a train. Iterator is available //think about the implementation of train. Iterator 1/23/2022 B. Ramamurthy 26

Using an Iterator // generate the iterator List. Iterator t. R = map. train. Iterator(); while (tr. has. Next()) System. out. println(tr. next()); 1/23/2022 B. Ramamurthy 27
- Slides: 27