1 / 43

LinkedList

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.

denton
Download Presentation

LinkedList

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. LinkedList Many slides from Horstmann modified by Dr V

  2. 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

  3. Insertingan Element into a Linked List is done by using a ListIterator object for the LinkedList object

  4. 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()

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

  6. A ListIterator object in a LinkedList List Iterator position

  7. Conceptual View of the ListIterator

  8. List Iterator • Think of an iterator as pointing between two links

  9. List Iterator • The listIterator method of the LinkedList class gets a list iterator LinkedList list = new LinkedList(); . . . ListIterator iterator = list.listIterator();

  10. List Iterator • Thenextmethodmoves the iteratoriterator.next(); • nextthrowsa NoSuchElementException if you are already past the end of the list

  11. List Iterator • hasNextreturns true if there is a next element if (iterator.hasNext()) iterator.next();

  12. 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 }

  13. List Iterator • To move the list position backwards, use: • hasPrevious • previous

  14. 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");

  15. 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

  16. 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(); }

  17. 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

  18. 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

  19. 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())

  20. 38: System.out.println(iterator.next()); 39: } 40: }

  21. Abstract Data Types • Abstract data type defines the fundamental operations on the data • Abstract data type does not specify an implementation

  22. 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

  23. An Abstract View of a Linked List

  24. A Concrete View of a Linked List

  25. An Abstract View of an Array List

  26. A Concrete View of an Array List

  27. Fundamental Operations on Array List public class ArrayList { public Object get(int index) {. . . } public void set(int index, Object value) {. . . } }

  28. 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); . . . }

  29. 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)

  30. Efficiency of Linked List • Random access • On average n/2 elements need to be skipped • In big-Oh notation:     O(n)

  31. 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)

  32. Efficiency of Operations for Arrays and List

  33. 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

  34. A Stack of Books • A stack can be visualized as a stack of books. • You place books on top and remove from the top.

  35. A Stack of Books

  36. 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.

  37. 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--; }

  38. 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

  39. 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.

  40. A Queue

  41. Abstract Data Type Queue • No implementing class in the Java library • Can be implemented using a linked list

  42. 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);

  43. } /** 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; }

More Related