Lesson 8: Advanced Data Structures in Java
Activity 32: Creating a Custom Linked List in Java
Solution:
- Create a class named, SimpleObjLinkedList.
public class SimpleObjLinkedList {
- Create a class named Node that represents each element in a Linked List. Each node will have an Object that it needs to hold, and it will have a reference to the next Node. The LinkedList class will have a reference to the head node and will be able to traverse to the next Node by using Node.getNext(). Head being the first element, we could traverse to the next element by moving next in the current Node. Like this, we could traverse till the last element of the list:
static class Node {
Object data;
Node next;
Node(Object d) {
data = d;
next = null;
}
Node getNext() {
return next;
}
void setNext(Node node) {
next = node;
}
Object getData() {
return data;
}
}
- Implement a toString() method to represent this object. Starting from the head Node, iterate all the nodes until the last node is found. On each iteration, construct...