Download presentation
Presentation is loading. Please wait.
Published byLaurel Warren Modified over 9 years ago
1
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis, and Thomas Kuehne, VUW COMP 103 Marcus Frean Linked Structures
2
2 RECAP-TODAY RECAP We were looking at costs, Binary Search, then Sorting and recursing TODAY A whole new way to implement Collections: Linked Structures Announcements oops, I forgot to record the QuickSort lecture – hopefully fixed now
3
3 (from the end of last lecture) Stable or Unstable ? Faster if almost-sorted ? MergeSort: Stable: doesn’t jump any item over an unsorted region ⇒ two equal items preserve their order Same cost on all input “natural merge” variant doesn’t sort already sorted regions ⇒ will be very fast: O(n) on almost sorted lists QuickSort: Unstable: Partition “jumps” items to the other end ⇒ two equal items likely to reverse their order Cost depends on choice of pivot simplest choice can be very slow: O(n 2 ) even on almost sorted lists better choice (median of three) ⇒ O(n log(n)) on almost sorted lists
4
4 (from the end of last lecture) Some “Big-Oh” costs revisited Implementing Collections: ArrayList: O(n) to add/remove, except at end Stack:O(1) ArraySet:O(n) (cost of searching) SortedArraySetO( log(n) ) to search (with binary search) O(n) to add/remove (cost of moving up/down) O( n 2 ) to add n items O( n log(n) ) to initialise with n items. (with fast sorting)
5
5 Better Implementations for Collections So far we have relied on arrays as a supporting data structure for collection classes → ArrayList, SortedArraySet, ArrayQueue, ArrayPriorityQueue…. Array advantages: quick random access Array disadvantages: growing them is costly (remember “ensureCapacity”?) need contiguous chunks of memory maintaining order is costly (Lecture 12) How to address these disadvantages?
6
6 Array versus Linked Structure (analogy) Acreelman.blogspot.com
7
7 How can we insert faster ? Fast lookup in array ⇒ items must be sorted Maintaining the sorting order is costly since inserting new items requires moving old ones ⇒ You can’t insert fast with a sorted array! To make insert faster, we would need each item to know its neighbours not by position but by topology But, how do we keep track of the order ? BAEDC GIH F B A ED C H I G F
8
8 Linked List Put each value in an object with a field for a link to the next Traverse the list by following the links Insert (F) by changing links ⇒ No need to shift everything up Remove (D) by changing links ⇒ No need to shift things down B A E D C G I H F order of operations is important
9
9 Every node in a linked list can be considered to be the “head” of a sublist That’s why linked lists – and other linked structures – lend themselves to be used with recursive methods Linked Lists are Recursive Structures List of length 3 List of length 2 List of length 1 C M X P shorthand for "null": end of the list
10
10 Memory management What are references/pointers ? A pointer/reference is an address of a chunk of memory, where the data can be stored How do you get this memory allocated ? You’ve been doing it since the start of Comp102, using new: creating an object allocates some chunk memory for the object new returns the address of the chunk of memory copying the address does not copy the chunk of memory Memory should be recycled after use: In Java, you don’t have to worry about freeing memory. The garbage collector automatically frees up any memory chunks that no longer have anything pointing/referring to them In languages without a garbage collector (eg C, C++), you have to do this yourself. OK to ignore in small programs, but requires special care in large programs!
11
11 A Linked Node class: public class LinkedNode { private int value; private LinkedNode next; public LinkedNode(int item, LinkedNode nextNode) { value = item; next = nextNode; } public int getValue() { return value; } public LinkedNode next(){ return next; } public void setValue(int item) { value = item; } public void setNext(LinkedNode nextNode) { next = nextNode; } } 4 5
12
12 A Generic Linked Node class: public class LinkedNode { private E value; private LinkedNode next; public LinkedNode(E item, LinkedNode nextNode) { value = item; next = nextNode; } public E getValue() { return value; } public LinkedNode next(){ return next; } public void setValue(E item) { value = item; } public void setNext(LinkedNode nextNode) { next = nextNode; } an E
13
13 Using Linked Nodes LinkedNode colours = new LinkedNode (“red”, null); colours.setNext(new LinkedNode (“blue”, null)); colours = new LinkedNode (“green”, colours); System.out.printf(“1st: %s ”,colours.getValue() ); System.out.printf(“2nd: %s ”,colours.next().getValue() ); System.out.printf(“3rd: %s ”,colours.next().next().getValue() ); 1 st : green 2 nd : red 3 rd : blue colours “red”“blue”“green”
14
14 Using Linked Nodes Removing a node in the middle: colours.setNext( ); “Copy” colours, then “remove” first node LinkedNode copy = colours; colours = colours.next(); “red” colours “green”“blue” copy No references. Garbage collector will get it eventually colours.next().next()
15
15 Using Linked Nodes Making a list of squared ints: LinkedNode squares = null; for (int i = 1; i ( i*i, squares ); This builds the list backwards… Exercise: Build the list working forwards 49 1 squares /
16
16 Iterating through a linked list LinkedNode rest = squares; while (rest != null) { UI.println(rest.getValue()); rest = rest.next(); } or for (LinkedNode rest = squares; rest != null; rest = rest.next()) { UI.println(rest.getValue()); } or we could do it with recursion.... 49 1 squares rest /
17
17 list list’ T wo ways to print a linked list /** Print the values in the list starting at a node */ public void printList(LinkedNode list) { if (list == null) return; UI.println(list.getValue()); if (list.next() == null) return; printList(list.next()); } “dog”“cat” “cow” recursive call base case public void printList(LinkedNode list’) { if (list’ == null) return; UI.println(list’.getValue()); if (list’.next() == null) return; printList(list’.next()); } handling an empty list
18
18 /** Print the values in the list starting at a node */ public void printList(LinkedNode list ) { if (list == null) return; UI.println(list.getValue()); printList(list.next()); } Example: printList ( the recursive version ) printList( list ) "cat" "dog" "cow" “dog” “cat” “cow” animals printList( animals ); printList( list’ ) printList( list’’ ) printList( list’’’ ) call return
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.