EDIT: I fixed it! It was because I kept using head.method() in my class definition when all I had to do was write method().
I am attempting to design an implementation of a basic Linked List. I cannot get my code to compile or run. This is the first exercise in the Linked List Tutorial. My knowledge of coding is limited. The compile errors are copied below. I suspect it has something to do with my use of incorrect access specifiers. Corrections, tips, and general guidance are welcome.
Thank you for your time.
// Start of Class Declaration of "MyLinkedList"
class MyLinkedList {
// Start of Class Declaration of "SinglyListNode"
public class SinglyListNode {
int val;
SinglyListNode next;
// Constructor Declaration of SinglyListNode (i.e., Initializing SinglyListNode)
public SinglyListNode(int x) {
val = x;
}
}
// End of Class Declaration of "SinglyListNode"
// Instantiate an Object of SinglyListNode
// Also an instance variable of class MyLinkedList
public SinglyListNode head;
// Constructor Declaration of MyLinkedList (i.e., Initializing MyLinkedList)
public MyLinkedList() {
head = null;
}
// method 1
private SinglyListNode getNode(int index) {
SinglyListNode curr = head;
for (int i = 0; i < index && curr != null; i++) {
curr = curr.next;
}
return curr;
}
// method 2
private SinglyListNode getTailNode() {
SinglyListNode curr = head;
while (curr != null && curr.next != null) {
curr = curr.next;
}
return curr;
}
// method 3
public int getValue(int index) {
SinglyListNode curr = head.getNode(index);
return curr == null ? -1 : curr.val;
}
// method 4
public void addNodeAtHead(int val) {
SinglyListNode curr = new SinglyListNode(val);
curr.next = head;
head = curr;
}
// method 5
public void addNodeAtTail(int val) {
SinglyListNode curr = new SinglyListNode(val);
head.getTailNode().next = curr;
}
// method 6
public void addNodeAtIndex(int index, int val) {
SinglyListNode curr = new SinglyListNode(val);
if (head.getTailNode() == head.getNode(index)) curr = head.addNodeAtTail(val);
if (head.getNode(index) != null) {
curr.next = head.getNode(index);
head.getNode(index-1).next = curr;
}
}
// method 7
public void deleteNodeAtIndex(int index) {
if (head.getNode(index) != null) {
head.getNode(index-1).next = head.getNode(index).next;
}
}
}ERRORS
Line 44: error: cannot find symbol [in SinglyListNode.java] SinglyListNode curr = head.getNode(index); ^ symbol: method getNode(int) location: variable head of type MyLinkedList.SinglyListNode
Line 58: error: cannot find symbol [in SinglyListNode.java] head.getTailNode().next = curr; ^ symbol: method getTailNode() location: variable head of type MyLinkedList.SinglyListNode
Line 64: error: cannot find symbol [in SinglyListNode.java] if (head.getTailNode() == head.getNode(index)) curr = head.addNodeAtTail(val); ^ symbol: method getTailNode() location: variable head of type MyLinkedList.SinglyListNode
Line 64: error: cannot find symbol [in SinglyListNode.java] if (head.getTailNode() == head.getNode(index)) curr = head.addNodeAtTail(val); ^ symbol: method getNode(int) location: variable head of type MyLinkedList.SinglyListNode
Line 64: error: cannot find symbol [in SinglyListNode.java] if (head.getTailNode() == head.getNode(index)) curr = head.addNodeAtTail(val); ^ symbol: method addNodeAtTail(int) location: variable head of type MyLinkedList.SinglyListNode
Line 65: error: cannot find symbol [in SinglyListNode.java] if (head.getNode(index) != null) { ^ symbol: method getNode(int) location: variable head of type MyLinkedList.SinglyListNode
Line 66: error: cannot find symbol [in SinglyListNode.java] curr.next = head.getNode(index); ^ symbol: method getNode(int) location: variable head of type MyLinkedList.SinglyListNode
Line 67: error: cannot find symbol [in SinglyListNode.java] head.getNode(index-1).next = curr; ^ symbol: method getNode(int) location: variable head of type MyLinkedList.SinglyListNode
Line 16: error: cannot find symbol [in Driver.java] return Serializer.serialize(obj.get( ^ symbol: method get(int) location: variable obj of type MyLinkedList
Line 19: error: cannot find symbol [in Driver.java] obj.addAtHead( ^ symbol: method addAtHead(int) location: variable obj of type MyLinkedList Line
23: error: cannot find symbol [in Driver.java] obj.addAtTail( ^ symbol: method addAtTail(int) location: variable obj of type MyLinkedList
Line 27: error: cannot find symbol [in Driver.java] obj.addAtIndex( ^ symbol: method addAtIndex(int,int) location: variable obj of type MyLinkedList
Line 32: error: cannot find symbol [in Driver.java] obj.deleteAtIndex( ^ symbol: method deleteAtIndex(int) location: variable obj of type MyLinkedList
13 errors