Presentation is loading. Please wait.

Presentation is loading. Please wait.

241-423 ADSA: Linked Lists/5 1 241-423 Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, 2013-2014 5. Linked.

Similar presentations


Presentation on theme: "241-423 ADSA: Linked Lists/5 1 241-423 Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, 2013-2014 5. Linked."— Presentation transcript:

1 241-423 ADSA: Linked Lists/5 1 241-423 Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, 2013-2014 5. Linked Lists

2 241-423 ADSA: Linked Lists/5 2 Contents 1. An ArrayList can be Slow 2. What is a Linked List? 3. Java Assignment Differences 4. Implementing a Linked List 5. Using a Linked List 6. Doubly Linked Lists 7. The (Doubly) LinkedList Collection 8. Palindromes

3 241-423 ADSA: Linked Lists/5 3 1. An ArrayList can be Slow Inserting/removing an element inside an ArrayList requires data shifting Inserting/removing an element inside an ArrayList requires data shifting –O(n) operations

4 241-423 ADSA: Linked Lists/5 4 Each element (node) inside a linked list is linked to the previous node and successor (next) node. Each element (node) inside a linked list is linked to the previous node and successor (next) node. This allows for more efficient insertion and deletion of nodes. Why? This allows for more efficient insertion and deletion of nodes. Why? 2. What is a Linked List? 53142 continued

5 241-423 ADSA: Linked Lists/5 5 Inserting a new node only involves breaking one link, and linking the list to both ends of the new node: Inserting a new node only involves breaking one link, and linking the list to both ends of the new node: –all are O(1) operations continued

6 241-423 ADSA: Linked Lists/5 6 Removal of a node only requires the breaking of its two links, removal of the node, and then the relinking of the list: Removal of a node only requires the breaking of its two links, removal of the node, and then the relinking of the list: –all are O(1) operations continued

7 241-423 ADSA: Linked Lists/5 7 The insertion/removal of a node is a local operation The insertion/removal of a node is a local operation –only the links next to the node need to be changed –the other nodes in the list are not affected –fast: O(1) An ArrayList must shift lots of elements when an element is inserted/removed An ArrayList must shift lots of elements when an element is inserted/removed –slow: O(n)

8 241-423 ADSA: Linked Lists/5 8 3. Java Assignment Differences 32 Foo a = new Foo(); Foo b; b = a; int a = 32; int b; b = a; b 32 a b a Foo object copy the link (the reference) copy the value

9 241-423 ADSA: Linked Lists/5 9 4. Implementing a Linked List Each node is an object containing a value and a link (reference) to the next node (object) in the list Each node is an object containing a value and a link (reference) to the next node (object) in the list –a singly-linked list The list uses a 'front' variable to point to the first object in the list. The list uses a 'front' variable to point to the first object in the list. The reference in the last object is null. The reference in the last object is null.

10 241-423 ADSA: Linked Lists/5 10 Nodes in a singly-linked list are accessed by moving forward one node at a time from the front Nodes in a singly-linked list are accessed by moving forward one node at a time from the front –called sequential access –a linked list is not a direct access structure like an array –this means that access is slower than in an array linked list access (if index is known): O(n) linked list access (if index is known): O(n) array list access (if index is known): O(1) array list access (if index is known): O(1) Accessing a Node

11 241-423 ADSA: Linked Lists/5 11 Nodes in a Linked List Each Node object contains two variables: Each Node object contains two variables: –nodeValue, of generic type T –next, a reference that links to the next node

12 241-423 ADSA: Linked Lists/5 12 The Node Class public class Node { public T nodeValue; // data held by the node public Node next; // next node in the list public Node() { nodeValue = null; next = null; } public Node(T item) { nodeValue = item; next = null; }

13 241-423 ADSA: Linked Lists/5 13 The variables in the Node class are public to simplify the coding using linked lists The variables in the Node class are public to simplify the coding using linked lists –bad style (from Ford & Topp, not me ☺) The Node class is self ‑ referencing: The Node class is self ‑ referencing: –next refers to (points to) an object of the same type

14 241-423 ADSA: Linked Lists/5 14 Creating a Linked List // create two nodes (figure (a) Node p = new Node ("red"); Node q = new Node ("green") // link p to q p.next = q;// figure (b) // set front to point at the first node Node front = p;// figure (c) continued

15 241-423 ADSA: Linked Lists/5 15 If the linked list is empty, front is assigned null. If the linked list is empty, front is assigned null.

16 241-423 ADSA: Linked Lists/5 16 Scanning a Linked List We scan a singly linked list by starting at the front, and then move along the list one Node at a time We scan a singly linked list by starting at the front, and then move along the list one Node at a time –sequential access (O(n)) –stop when we reach null toString() is an example of a scanning method, which builds a string as it moves along the list. toString() is an example of a scanning method, which builds a string as it moves along the list.

17 241-423 ADSA: Linked Lists/5 17 toString() public static String toString(Node front) // build a string from the list of the form // "[ n1, n2,..., nx ]" { if (front == null) // empty list return "[]"; Node curr = front; // start at the front String s = "[" + curr.nodeValue; while(curr.next != null) { curr = curr.next; // move along list s += ", " + curr.nodeValue; } s += "]"; return s; }

18 241-423 ADSA: Linked Lists/5 18 Moving to a List Position To move to an element at position x, we need to start at the front and move through the list counting up to x To move to an element at position x, we need to start at the front and move through the list counting up to x –sequential access again (O(n)) The first element of the list is at position 0. The first element of the list is at position 0. continued

19 241-423 ADSA: Linked Lists/5 19 Node curr = front; // start at the front of list for (int i = 0; i < xPos; i++) curr = curr.next; // move along list

20 241-423 ADSA: Linked Lists/5 20 Updating the Front of the List Inserting or deleting an element at the front of a list is easy (and fast) because the 'front' variable always points to the first element: Inserting or deleting an element at the front of a list is easy (and fast) because the 'front' variable always points to the first element: –the operations are O(1)

21 241-423 ADSA: Linked Lists/5 21 Node newNode = new Node (item); // insert item at the front of the list newNode.next = front; front = newNode; Insert at the Front

22 241-423 ADSA: Linked Lists/5 22 front = front.next;// move front to next node Delete from the Front

23 241-423 ADSA: Linked Lists/5 23 General Insertion To insert a new node before a node referenced by 'curr', the code must have access to the previous node, 'prev', since its link must be changed. To insert a new node before a node referenced by 'curr', the code must have access to the previous node, 'prev', since its link must be changed. continued

24 241-423 ADSA: Linked Lists/5 24 Node curr =... // set to point to a node Node prev =... // set to point to previous node Node newNode = new Node (item); // new node // update links newNode.next = curr;// step 1 prev.next = newNode;// step 2 continued

25 241-423 ADSA: Linked Lists/5 25 The insertion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the insertion position, which is O(n). The insertion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the insertion position, which is O(n).

26 241-423 ADSA: Linked Lists/5 26 General Deletion Deleting a node at position curr requires access to the predecessor node prev. Deleting a node at position curr requires access to the predecessor node prev. Node curr =... // set to point to a node Node prev =... // set to point to previous node // connect prev to curr.next prev.next = curr.next; curr.next = null; continued

27 241-423 ADSA: Linked Lists/5 27 The deletion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the deletion position, which is O(n) The deletion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the deletion position, which is O(n) –this is shown in the remove() method, which is explained next

28 241-423 ADSA: Linked Lists/5 28 Removing a Target Node To remove the first node having a specified value, scan the list to find the node. To remove the first node having a specified value, scan the list to find the node. The scan must use two references that move together down the list The scan must use two references that move together down the list –one reference (curr) points to the current node in the scan –the other reference (prev) points to the previous node continued

29 241-423 ADSA: Linked Lists/5 29 Once 'curr' finds the node, the code uses 'prev' to unlink 'curr'. Once 'curr' finds the node, the code uses 'prev' to unlink 'curr'. continued

30 241-423 ADSA: Linked Lists/5 30 At the start, point 'curr' at the front of the list and set 'prev' to null, since the first node does not have a predecessor. At the start, point 'curr' at the front of the list and set 'prev' to null, since the first node does not have a predecessor. Move 'curr' and 'prev' down the list until curr.nodeValue matches the target or curr == null. Move 'curr' and 'prev' down the list until curr.nodeValue matches the target or curr == null. continued

31 241-423 ADSA: Linked Lists/5 31 If the target is found then 'curr' points at the node and 'prev' to the predecessor node. If the target is found then 'curr' points at the node and 'prev' to the predecessor node. But there are two possible cases: But there are two possible cases: –the target node is the first node, so 'prev' is null –the target node is not the first node, so 'prev' points to something continued

32 241-423 ADSA: Linked Lists/5 32 Case 1:'prev' is null which means that 'curr' points to the first node. Case 1:'prev' is null which means that 'curr' points to the first node. –so we only have to delete the front of the list front = curr.next; curr.next = null;

33 241-423 ADSA: Linked Lists/5 33 Case 2:The match occurs in the middle of the list. Both 'curr' and 'prev' have non-null values. Unlink the current node. Case 2:The match occurs in the middle of the list. Both 'curr' and 'prev' have non-null values. Unlink the current node. prev.next = curr.next; curr.next = null; continued

34 241-423 ADSA: Linked Lists/5 34 The generic remove() is passed a reference to the front of the list and the target value. The generic remove() is passed a reference to the front of the list and the target value. The method returns the value of 'front', which may have been updated if the first node was deleted. The method returns the value of 'front', which may have been updated if the first node was deleted.

35 241-423 ADSA: Linked Lists/5 35 remove() Method public static Node remove(Node front, T target) /* Delete the first occurrence of the target in the linked list referenced by front; return the value of front */ { // initialize pointers Node curr = front; Node prev = null; boolean foundItem = false; // set to true if we find the target :

36 241-423 ADSA: Linked Lists/5 36 // scan until find item or end of list (O(n)) while (curr != null && !foundItem) { // check for a match if (target.equals(curr.nodeValue)) { if (prev == null) // remove first Node (O(1)) front = front.next; else // erase middle Node (O(1)) prev.next = curr.next; curr.next = null; foundItem = true; } else { // advance curr and prev prev = curr; curr = curr.next; } return front; // may be updated } // end of remove()

37 241-423 ADSA: Linked Lists/5 37 5. Using a Linked List import java.util.Random; import java.util.Scanner; import ds.util.Node; import ds.util.Nodes; // methods using Node public class ListExample { public static void main(String[] args) { // the initial list is empty Node front = null; Random rnd = new Random(); Scanner keyIn = new Scanner(System.in); :

38 241-423 ADSA: Linked Lists/5 38 System.out.print("Enter the size of the list: "); int listCount = keyIn.nextInt(); // create a list Node newNode; for (int i = 0; i < listCount; i++) { newNode = new Node (rnd.nextInt(100)); newNode.next = front; // insert at list front front = newNode; } System.out.print("Original list: "); System.out.println( Nodes.toString(front) ); :

39 241-423 ADSA: Linked Lists/5 39 System.out.print("Ordered list: "); Node p; while (front != null) { // list not empty p = getMaxNode(front); // get largest System.out.print(p.nodeValue + " "); front = Nodes.remove(front, p.nodeValue); } System.out.println(); } // end of main()

40 241-423 ADSA: Linked Lists/5 40 public static > Node getMaxNode(Node front) { Node maxNode = front; // initial values Node curr = front.next; T maxValue = front.nodeValue; while (curr != null) { // try to update maxNode and maxValue if (maxValue.compareTo(curr.nodeValue)< 0) { maxValue = curr.nodeValue; maxNode = curr; } curr = curr.next; } return maxNode; } // end of getMaxNode() } // end of ListExample class

41 241-423 ADSA: Linked Lists/5 41 Execution

42 241-423 ADSA: Linked Lists/5 42 6. Doubly Linked Lists A node in a doubly-linked list contain two references that point to the next node and the previous node. A node in a doubly-linked list contain two references that point to the next node and the previous node. front points to the first node in the list front points to the first node in the list back points at the last node in the list back points at the last node in the list continued

43 241-423 ADSA: Linked Lists/5 43 A doubly-linked list can be scanned in both directions: A doubly-linked list can be scanned in both directions: –a forward scan starts at 'front' and ends when the link is to the same object as 'back' –a backward scan starts at 'back' and ends when the link is to the same object as 'front' continued

44 241-423 ADSA: Linked Lists/5 44 Like a singly-linked list, a doubly linked list is a sequential structure. Like a singly-linked list, a doubly linked list is a sequential structure. To move forward or backward, use the node links 'next' and 'prev'. To move forward or backward, use the node links 'next' and 'prev'. Unlike a singly linked list, the insert and delete operations only need a single reference to the node. Unlike a singly linked list, the insert and delete operations only need a single reference to the node. continued

45 241-423 ADSA: Linked Lists/5 45 Insertion into a doubly linked list requires four reference assignments. Insertion into a doubly linked list requires four reference assignments. prevNode = curr.prev; newNode.prev = prevNode;// 1 prevNode.next = newNode;// 2 curr.prev = newNode;// 3 newNode.next = curr;// 4 continued

46 241-423 ADSA: Linked Lists/5 46 To delete a node curr, link the predecessor (curr.prev) of 'curr' to the successor of 'curr' (curr.next). To delete a node curr, link the predecessor (curr.prev) of 'curr' to the successor of 'curr' (curr.next). prevNode = curr.prev; succNode = curr.next; succNode.prev = prevNode;// 1 prevNode.next = succNode;// 2 curr.prev = null; curr.next = null; continued

47 241-423 ADSA: Linked Lists/5 47 In a singly-linked list, adding and removing a node at the front of the list are O(1) operations. In a singly-linked list, adding and removing a node at the front of the list are O(1) operations. With a doubly linked list, you can add and remove a node at the back of the list with the same O(1) efficiency. With a doubly linked list, you can add and remove a node at the back of the list with the same O(1) efficiency.

48 241-423 ADSA: Linked Lists/5 48 7. The (Doubly) LinkedList Collection In Ford & Topp's DSA package

49 241-423 ADSA: Linked Lists/5 49 UML for LinkedList

50 241-423 ADSA: Linked Lists/5 50 The LinkedList() constructor creates an empty list. The LinkedList() constructor creates an empty list. The toString() method returns a string representing the list as a comma-separated sequence of elements enclosed in brackets. The toString() method returns a string representing the list as a comma-separated sequence of elements enclosed in brackets. LinkedList Methods continued

51 241-423 ADSA: Linked Lists/5 51 Reuse the Collection methods: Reuse the Collection methods: –isEmpty(), size(), contains(), toArray() add() inserts a new element at the back of the list and returns true. add() inserts a new element at the back of the list and returns true. remove() with an Object reference deletes the first occurrence of the object in the list remove() with an Object reference deletes the first occurrence of the object in the list –the method returns true or false depending on whether a match was found

52 241-423 ADSA: Linked Lists/5 52 LinkedList Examples LinkedList aList = new LinkedList (); alist.add("Red"); alist.add("Green"); alist.add("Blue); System.out.println("Size = " + aList.size()); System.out.println("List contains the string 'White' is " + aList.contains("White"); Size = 3 List contains the string 'White' is false

53 241-423 ADSA: Linked Lists/5 53 aList.add("Black");// add Black at the end aList.add("Blue");// add Blue at the end aList.remove("Blue");// delete first "Blue" System.out.println(aList);// uses toString() [Red, Green, Black, Blue]

54 241-423 ADSA: Linked Lists/5 54 LinkedList Index Methods The LinkedList can access and update an element with get() and set(), and modify the list with the add() and remove(). The LinkedList can access and update an element with get() and set(), and modify the list with the add() and remove(). The index methods have O(n) worst case running time. Use these methods only for small data sets. The index methods have O(n) worst case running time. Use these methods only for small data sets.

55 241-423 ADSA: Linked Lists/5 55 Example // create list containing [5,7,9,4,3] Integer i= list.get(1); // i has value 7 list.remove(1); // remove value at position 1 continued

56 241-423 ADSA: Linked Lists/5 56 list.set(2, 8); // store node 8 at position 2 list.add(2, 6); // store value 6 at position 2

57 241-423 ADSA: Linked Lists/5 57 Accessing the Ends of a LinkedList Methods for the front of the list: Methods for the front of the list: –getFirst(), addFirst(), removeFirst() For the back of the list: For the back of the list: –getLast(), addLast(), removeLast() They all are O(1) operations They all are O(1) operations

58 241-423 ADSA: Linked Lists/5 58 End-of-List Examples LinkedList list = new LinkedList (); list.addFirst("Tom"); list.addFirst("Debbie"); list.addLast("David"); ist.addLast("Maria"); continued

59 241-423 ADSA: Linked Lists/5 59 // identify the elements at the ends of the list System.out.println("First element is " + list.getFirst()); System.out.println("Last element is " + list.getLast()); First element is Debbie Last element is Maria continued

60 241-423 ADSA: Linked Lists/5 60 // Exchange the first and last elements in the list. // remove elements at the ends of the list String firstElem = aList.removeFirst(); String lastElem = aList.removeLast(); // add elements back in switched positions aList.addLast(firstElem); aList.addFirst(lastElem); continued

61 241-423 ADSA: Linked Lists/5 61 // Output elements in the list by position. // Repeatedly delete first element and display its // value until list is empty while (!aList.isEmpty()) System.out.print(aList.removeFirst() + " "); Maria Tom David Debbie

62 241-423 ADSA: Linked Lists/5 62 A linked list is a natural way to implement a queue. A linked list is a natural way to implement a queue. –the element at the front of the queue can be removed with getFirst() –a new element can be added to the back of the queue by using addLast() Linked List as a Queue

63 241-423 ADSA: Linked Lists/5 63 8. Palindromes A palindrome is a string that reads the same forward and backward: A palindrome is a string that reads the same forward and backward: –e.g. "level", "noon.", "Stack Cats" –ignore non-letters and let capitals == lowercase isPalindrome() takes a LinkedList object as an argument and returns true if the sequence is a palindrome; false otherwise. isPalindrome() takes a LinkedList object as an argument and returns true if the sequence is a palindrome; false otherwise. continued

64 241-423 ADSA: Linked Lists/5 64 Panic in a Titanic, I nap. Race car Yawn a more Roman way. A Toyota's a Toyota

65 241-423 ADSA: Linked Lists/5 65 public static boolean isPalindrome(LinkedList aList) { // list must have 2 or more elements while (aList.size() > 1) { // compare elements on opposite ends of list if ( !aList.getFirst().equals(aList.getLast()) ) return false; // delete the matching elements aList.removeFirst(); aList.removeLast(); } // if we get here then list is a palindrome return true; }

66 241-423 ADSA: Linked Lists/5 66 The "?" Wildcard isPalindrome() does not refer to the generic type of the list. In this case, we may use: isPalindrome() does not refer to the generic type of the list. In this case, we may use: LinkedList aList "?" means that we don't care about the type of the elements of the list. "?" means that we don't care about the type of the elements of the list.

67 241-423 ADSA: Linked Lists/5 67 Checking for a Palindrome import java.util.Scanner; import ds.util.LinkedList; public class CheckPali { public static void main(String[] args) { LinkedList charList = new LinkedList (); // get input line from user System.out.print("Enter a string: "); Scanner keyIn = new Scanner(System.in); String str = keyIn.nextLine(); :

68 241-423 ADSA: Linked Lists/5 68 // put all letters into list as lowercase chars for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isLetter(ch)) charList.addLast( Character.toLowerCase(ch) ); } if (isPalindrome(charList)) System.out.println("'" + str + "' is a palindrome"); else System.out.println("'" + str + "' is not a palindrome"); } // end of main() // isPalindrome() method goes here } // end of CheckPali class

69 241-423 ADSA: Linked Lists/5 69 Execution


Download ppt "241-423 ADSA: Linked Lists/5 1 241-423 Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, 2013-2014 5. Linked."

Similar presentations


Ads by Google