Presentation is loading. Please wait.

Presentation is loading. Please wait.

LinkedList Many slides from Horstmann modified by Dr V.

Similar presentations


Presentation on theme: "LinkedList Many slides from Horstmann modified by Dr V."— Presentation transcript:

1 LinkedList Many slides from Horstmann modified by Dr V

2 Linked List A linked list consists of a number of links, each of which has a reference to the next link. 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. 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 Visiting the elements of a linked list in sequential order is efficient Random access is not efficient Random access is not efficient

3 Inserting an 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 Easy access to first and last elements with methods o void addFirst(Object obj) o void addLast(Object obj) o Object getFirst() o Object getLast() o Object removeFirst() o Object removeLast()

5 ListIterator ListIterator object gives access to elements inside a LinkedList object ListIterator object gives access to elements inside a LinkedList object ListIterator protects the linked list while giving access ListIterator protects the linked list while giving access ListIterator encapsulates a position anywhere in the linked list 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 Think of an iterator as pointing between two links

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

10 List Iterator The next method moves the iterator iterator.next(); The next method moves the iterator iterator.next(); next throws a NoSuchElementException if next throws a NoSuchElementException if you are already past the end of the list you are already past the end of the list

11 List Iterator hasNext returns true if there is a next element hasNext returns 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 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: To move the list position backwards, use: o hasPrevious o previous

14 Adding and Removing from a LinkedList The add method: The add method: o Adds an object after the iterator o Moves the iterator position past the new element iterator.add("Juliet");

15 Adding and Removing from a LinkedList The remove method: The remove method: o Removes and o 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 This loop removes all objects that fulfill a certain condition while (iterator.hasNext()) { Object obj = iterator.next(); if (obj fulfills condition) iterator.remove(); iterator.remove(); }

17 File ListTest.java ListTest is a sample program that ListTest is a sample program that o inserts elements into a list o iterates through the list, adding and removing elements o 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 defines the fundamental operations on the data Abstract data type does not specify an implementation Abstract data type does not specify an implementation

22 Abstract Data Types Abstract list Abstract list o An ordered sequence of items that can be traversed sequentially o Allows for insertion and removal of elements at any position Abstract array Abstract array o An ordered sequence of items o 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 Adding or removing an element o A fixed number of link references need to be modified to add or remove a link, regardless of the size of the list o Thus, an element can be added or moved in constant time o In big-Oh notations: O(1)

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

31 Efficiency of Array List Adding or moving an element Adding or moving an element o On average n/2 elements need to be moved o In big-Oh notations: O(n) Random access Random access o 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 Allows insertion and removal of elements only at one end o Traditionally called the top of the stack New items are added to the top of the stack New items are added to the top of the stack Items are removed at the top of the stack Items are removed at the top of the stack Called last in, first out or LIFO order Called last in, first out or LIFO order Think of a stack of books Think of a stack of books

34 A Stack of Books A stack can be visualized as a stack of books. A stack can be visualized as a stack of books. You place books on top and remove from the top. 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 is a concrete implementation of a stack in the Java library The Stack class uses an Object[] to implement a stack 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. 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 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 added to one end of the queue (the tail) Items removed from the other end (the head) Items removed from the other end (the head) Called first in, first out or FIFO order Called first in, first out or FIFO order Think of a queue of people Think of a queue of people

39 A Queue A Queue can be visualized as a queue of people. A Queue can be visualized as a queue of people. People join the tail of the queue and wait until they reach the head. 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 No implementing class in the Java library Can be implemented using a linked list Can be implemented using a linked list

42 A Queue Implementation public class Queue { /** /** Constructs an empty queue Constructs an empty queue */ */ public Queue() public Queue() { list = new LinkedList(); list = new LinkedList(); } /** /** Adds an item to the tail of the queue Adds an item to the tail of the queue @param x the item to add @param x the item to add */ */ public void add(Object x) public void add(Object x) { list.addLast(x); list.addLast(x);

43 } /** /** Removes an item from the head of the queue Removes an item from the head of the queue @return the removed item @return the removed item */ */ public Object remove() public Object remove() { return list.removeFirst(); return list.removeFirst(); } /** /** Gets the number of items in the queue Gets the number of items in the queue @return the size @return the size */ */ public int size() public int size() { return list.size() return list.size() } private LinkedList list; private LinkedList list;}


Download ppt "LinkedList Many slides from Horstmann modified by Dr V."

Similar presentations


Ads by Google