Download presentation
Presentation is loading. Please wait.
1
Linked Structures – Part 2
COMP 103 Linked Structures – Part 2
2
RECAP-TODAY RECAP TODAY Linked Structures – alternative to arrays
Linked Node (value, reference to next Linked Node) TODAY Iterating and printing a linked list Inserting and deleting in linked lists Advanced issues for the ‘curious’: Memory management and Garbage collection in the Java Virtual Machine (page 5 talks about ‘compacting’) value
3
Using Linked Nodes Making a big list, working forwards:
LinkedNode<Integer> squares = new LinkedNode<Integer>(1, null); LinkedNode<Integer> last = squares; for (int i = 2; i < 1000; i++) { last.setNext( new LinkedNode<Integer>(i*i, null) ); last = last.getNext(); } 9 1 4 squares / last
4
Iterating through a linked list
9 LinkedNode<Integer> rest = squares; while (rest != null) { System.out.printf("%6d \n", rest.get()); rest = rest.next(); } or for (LinkedNode<Integer> rest=squares; rest!=null; rest=rest.next()) { System.out.printf("%6d \n", rest.get()); or we could do it with recursion.... squares 4 rest / 1
5
Example: two methods to print out a linked list
/** Print the values in the list starting at a node */ public void printList(LinkedNode<E> list) { if (list == null) return; System.out.printf("%s, ", list.get()); printList(list.next()); } or for (LinkedNode<E> rest=list; rest!=null; rest=rest.next() ) System.out.printf("%s, ", rest.get()); recursive “cat” list “dog” “cow” iterative as per previous slide
6
Example: printList ( the recursive version )
/** Print the values in the list starting at a node */ public void printList(LinkedNode<E> list) { if (list == null) return; System.out.printf("%s, ", list.get()); printList(list.next()); } printList( ) list cat dog cow / "cat" dog cow / printList( ) calls (return) "dog" cow / printList( ) calls (return) "cow"
7
What about printing the last value?
list cat dog cow / dog cow / lastValue( ) calls “cow” cow / lastValue( ) calls “cow”
8
Exercise: return the value in the LAST node
/** Return the value in the last node of the list starting at a node */ public E lastValue (LinkedNode<E> list) { if (list == null) return null; // in case the whole list is empty if (list.next() == null) return list.get()); return lastValue( list.next() ); } or if (list == null) return null; // in case the whole list is empty LinkedNode<E> rest=list; while (rest.next != null) rest = rest.next(); return rest.get(); recursive lastValue( ) cat dog cow / list iterative
9
Inserting (recursive)
/** Insert the value at position n in the list (counting from 0) Assumes list is not empty, and n>0 */ public void insert (E item, int n, LinkedNode<E>list) { if (n == 1 ) list.setNext(new LinkedNode<E>(item, list.next()); else insert(item, n-1, list.next()); } eg. insert X at position 2 in mylist recursive mylist Q W E R T X
10
Inserting (iterative alternative)
/** Insert the value at position n in the list (counting from 0) Assumes list is not empty, and n>0 */ public void insert (E item, int n, LinkedNode<E>list) { int pos =0; LinkedNode<E> rest=list; // rest is the pos’th node while (pos <n-1) { pos++; rest=rest.next(); } rest.setNext(new LinkedNode<E>(item, rest.next()); iterative mylist Q W E R T
11
Removing (recursive) /** Remove the value from the list Assumes list is not empty, and value not in first node */ public void remove (E item, LinkedNode<E>list) { if (list.next() == null) return; // we are at the end of the list if (list.next().get().equals(item) ) list.setNext( list.next().next()); else remove(item, list.next()); } eg. remove R from mylist eg. remove Q from mylist - ouch: how can I alter mylist??! recursive W Q R E T mylist
12
Removing (iterative alternative)
or public void remove (E item, LinkedNode<E>list) { LinkedNode<E> rest=list; while (rest.next()!= null && !rest.next().get().equals(item)) rest=rest.next(); if (rest.next()!=null) rest.setNext(rest.next().next()); } iterative NB: two tests
13
Recursion in linked structures
Is this tricky? yes! we hope you can read the code and see it makes sense writing it (correctly) is quite hard just step along and drop off the end (e.g. print all of a list) look for a node and stop there (e.g. printLast). look for a node and stop at one before that (e.g. insert, remove) QUICK TIP: There are three typical patterns
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.