440 likes | 560 Views
LinkedList. Many slides from Horstmann modified by Dr V. Linked List. A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of a linked list is efficient.
E N D
LinkedList Many slides from Horstmann modified by Dr V
LinkedList • A linked list consists of a number of links, each of which has a reference to the next link. • Adding and removing elements in the middle of a linked list is efficient. • Visiting the elements of a linked list in sequential order is efficient • Random access is not efficient
Insertingan Element into a Linked List is done by using a ListIterator object for the LinkedList object
Java's LinkedList class • Easy access to first and last elements with methods • void addFirst(Object obj) • void addLast(Object obj) • Object getFirst() • Object getLast() • Object removeFirst() • Object removeLast()
ListIterator • ListIterator object gives access to elements inside a LinkedList object • ListIterator protects the linked list while giving access • ListIterator encapsulates a position anywhere in the linked list
A ListIterator object in a LinkedList List Iterator position
List Iterator • Think of an iterator as pointing between two links
List Iterator • The listIterator method of the LinkedList class gets a list iterator LinkedList list = new LinkedList(); . . . ListIterator iterator = list.listIterator();
List Iterator • Thenextmethodmoves the iteratoriterator.next(); • nextthrowsa NoSuchElementException if you are already past the end of the list
List Iterator • hasNextreturns true if there is a next element if (iterator.hasNext()) iterator.next();
List Iterator • The next method returns the object of the link that it is passing while iterator.hasNext() { Object obj = iterator.next(); //do something with the object }
List Iterator • To move the list position backwards, use: • hasPrevious • previous
Adding and Removing from a LinkedList • The add method: • Adds an object after the iterator • Moves the iterator position past the new element iterator.add("Juliet");
Adding and Removing from a LinkedList • The remove method: • Removes and • Returns the object that was returned by the last call to next or previous
Adding and Removing from a LinkedList • This loop removes all objects that fulfill a certain condition while (iterator.hasNext()) { Object obj = iterator.next(); if (obj fulfills condition) iterator.remove(); }
File ListTest.java • ListTest is a sample program that • inserts elements into a list • iterates through the list, adding and removing elements • prints the list
File ListTest.java 01: import java.util.LinkedList; 02: import java.util.ListIterator; 03: 04: /** 05: A program that demonstrates the LinkedList class 06: */ 07: public class ListTest 08: { 09: public static void main(String[] args) 10: { 11: LinkedList staff = new LinkedList(); 12: staff.addLast("Dick"); 13: staff.addLast("Harry"); 14: staff.addLast("Romeo"); 15: staff.addLast("Tom"); 16: 17: // | in the comments indicates the iterator position
18: 19: ListIterator iterator = staff.listIterator(); // |DHRT 20: iterator.next(); // D|HRT 21: iterator.next(); // DH|RT 22: 23: // add more elements after second element 24: 25: iterator.add("Juliet"); // DHJ|RT 26: iterator.add("Nina"); // DHJN|RT 27: 28: iterator.next(); // DHJNR|T 29: 30: // remove last traversed element 31: 32: iterator.remove(); // DHJN|T 33: 34: // print all elements 35: 36: iterator = staff.listIterator(); 37: while (iterator.hasNext())
38: System.out.println(iterator.next()); 39: } 40: }
Abstract Data Types • Abstract data type defines the fundamental operations on the data • Abstract data type does not specify an implementation
Abstract Data Types • Abstract list • An ordered sequence of items that can be traversed sequentially • Allows for insertion and removal of elements at any position • Abstract array • An ordered sequence of items • Allows for random access by specifying an integer index
Fundamental Operations on Array List public class ArrayList { public Object get(int index) {. . . } public void set(int index, Object value) {. . . } }
Fundamental Operations on Linked List public class LinkedList { public ListIteratior listIterator() {. . . } . . . } public interface ListIteratior { Object next(); boolean hasNext(); void add(Object value); void remove(); void set(Object value); . . . }
Efficiency of Linked List • Adding or removing an element • A fixed number of link references need to be modifiedto add or remove a link, regardless of the size of the list • Thus, an element can be added or moved in constant time • In big-Oh notations: O(1)
Efficiency of Linked List • Random access • On average n/2 elements need to be skipped • In big-Oh notation: O(n)
Efficiency of Array List • Adding or moving an element • On average n/2 elements need to be moved • In big-Oh notations: O(n) • Random access • In big-Oh notation: O(1)
Abstract Data Type Stack • Allows insertion and removal of elements only at one end • Traditionally called the top of the stack • New items are added to the top of the stack • Items are removed at the top of the stack • Called last in, first out or LIFO order • Think of a stack of books
A Stack of Books • A stack can be visualized as a stack of books. • You place books on top and remove from the top.
Abstract Data Type Stack • The Stack class is a concrete implementation of a stack in the Java library • The Stack class uses an Object[] to implement a stack …………………………………………… • OR you can just use the LinkedList class and only use the addFirst() and removeFirst() methods.
Abstract Data Type Stack • Sample code to use the Stack class Stack s = new Stack(); s.push("A"); s.push("B"); s.push("C"); //the following loop prints C, B, A int i = s.size(); while (i > 0) { System.out.println(s.pop()); i--; }
Abstract Data Type Queue • Items added to one end of the queue (the tail) • Items removed from the other end (the head) • Called first in, first out or FIFO order • Think of a queue of people
A Queue • A Queue can be visualized as a queue of people. • People join the tail of the queue and wait until they reach the head.
Abstract Data Type Queue • No implementing class in the Java library • Can be implemented using a linked list
A Queue Implementation public class Queue { /** Constructs an empty queue */ public Queue() { list = new LinkedList(); } /** Adds an item to the tail of the queue @param x the item to add */ public void add(Object x) { list.addLast(x);
} /** Removes an item from the head of the queue @return the removed item */ public Object remove() { return list.removeFirst(); } /** Gets the number of items in the queue @return the size */ public int size() { return list.size() } private LinkedList list; }