6. Linked Structures
Feel free to use your laptop
You are strongly encourage to work with others
When you get stuck, ask those sitting around you for help
Get used to working together in the labs
Peer teaching and peer learning has been empirically shown to be very effective
6.1. Linked Structure
The goal is to create a linked structure with a series of Node objects.
6.1.1. Node Class
Create a generic
NodeclassDownload and run the
NodeTestclass to ensure theNodeclass is implemented correctlyThis test class must be added to the project’s test folder
6.1.2. Create the Linked Structure
Write a static method in the same class as the
maincalledmakeLinkedStructurethat willCreate a linked structure containing the numbers 0 – 9
One number referenced in each node
Return a reference to the head of the linked structure
public static Node<Integer> makeLinkedStructure() { // ... }
Verify it works as expected by adding this to the
mainmethod and running itNode<Integer> head = makeLinkedStructure(); Node<Integer> currentNode = head; while (currentNode != null) { System.out.println(currentNode.getData()); currentNode = currentNode.getNext(); }
6.2. Manipulating the Linked Structure
Start manipulating the linked structure by adding and removing elements.
6.2.1. Adding to the Front of the Structure
Write a static method in the same class as the
maincalledaddToFrontthat willTake a reference to the head of a linked structure and an integer to add
Create a new
Nodecontaining a reference to the integer passed to the method as an argumentInsert the newly created
Nodeto the front of the linked structureReturn a reference to the new head of the linked structure
public static <T> Node<T> addToFront(Node<T> head, T toAdd) { // ... }
Verify it works as expected by adding this to the
mainmethod and running ithead = addToFront(head, 99); currentNode = head; while (currentNode != null) { System.out.println(currentNode.getData()); currentNode = currentNode.getNext(); }
6.2.2. Removing from the Front of the Structure
Write a static method in the same class as the
maincalledremoveFromFrontthat willTake a reference to the head of a linked structure
Remove the first
Nodefrom the structureReturn a reference to the new head of the linked structure
public static <T> Node<T> removeFromFront(Node<T> head) { // ... }
Verify it works as expected by adding this to the
mainmethod and running ithead = removeFromFront(head) ; currentNode = head; while (currentNode != null) { System.out.println(currentNode.getData()); currentNode = currentNode.getNext(); }
6.2.3. Adding to the Middle of the Structure
Write a static method in the same class as the
maincalledaddToMiddlethat willTake a reference to the head of a linked structure, an integer to add, and a number the new integer should be added after
Create a new
Nodecontaining a reference to the integer passed to the method as an argumentInsert the new
Nodeafter theNodecontaining the specified integer to be added afterReturn a reference to the head of the linked structure
For example, calling
addToMiddle(head, 99, 5), will add aNodecontaining a reference to99after the node containing a reference to the number5
public static <T> Node<T> addToMiddle(Node<T> head, T toAdd, T addAfter) { // ... }
Verify it works as expected by adding this to the
mainmethod and running ithead = addToMiddle(head, 99, 5); currentNode = head; while (currentNode != null) { System.out.println(currentNode.getData()); currentNode = currentNode.getNext(); }
Note
What should happen if the specified value for addAfter is not contained in the linked structure?
6.2.4. Removing from the Middle of the Structure
Write a static method in the same class as your
maincalledremoveFromMiddlethat willTake a reference to the head of a linked structure and a value to be removed from the linked structure
Remove the
Nodecontaining a reference to the specified value to be removed from the structureReturn a reference to the head of the linked structure
public static <T> Node<T> removeFromMiddle(Node<T> head, T toRemove) { // ... }
Verify it works as expected by adding this to the
mainmethod and running ithead = removeFromMiddle(head, 99) ; currentNode = head; while (currentNode != null) { System.out.println(currentNode.getData()); currentNode = currentNode.getNext(); }
Note
What should happen if the specified value for toRemove is not contained in the linked structure?