1 / 27

Understanding Linked List Structures in Java

Learn about linked structures and Java Linked Lists, including design patterns and array limitations. Explore varieties of linked lists and operations, with a focus on implementation and methods.

dhoule
Download Presentation

Understanding Linked List Structures in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Structures Computer Science and Engineering B.Ramamurthy

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

  3. 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 B.Ramamurthy

  4. 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. B.Ramamurthy

  5. 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 B.Ramamurthy

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

  7. 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() Node Class B.Ramamurthy

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

  9. JDK1.4 Linked List Object AbstractCollection Collection AbstractList List AbstractSequentialList List, java.io.Serializable, java.lang.Cloneable LinkedList B.Ramamurthy

  10. 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. LinkedList’s inheritance and related classes B.Ramamurthy

  11. 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 B.Ramamurthy

  12. 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(); B.Ramamurthy

  13. B.Ramamurthy

  14. previous next java.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 B.Ramamurthy

  15. previous next Header null Object Object Object Java.util.LinkedList Non-Empty List B.Ramamurthy

  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 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 B.Ramamurthy

  17. previous next Header null Object Object Object Entry e Remove an entry Step 3.2 Step 3.1 B.Ramamurthy

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

  19. Java Code for Step 3 of remove … e.previous.next = e.next; //3.1 e.next.previous = e.previous; //3.2 size--; //3.3 … B.Ramamurthy

  20. 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++ B.Ramamurthy

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

  22. 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++; … B.Ramamurthy

  23. previous next previous next header Step 2.1 Object o add(int index, Object o): Step 2 header Object Object Object Entry e at given index 2.2 2.3 B.Ramamurthy

  24. 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() B.Ramamurthy

  25. 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. B.Ramamurthy

  26. 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 B.Ramamurthy

  27. Using an Iterator // generate the iterator ListIterator tR = map.trainIterator(); while (tr.hasNext()) System.out.println(tr.next()); B.Ramamurthy

More Related