Start New Discussion within our Software Development Community

Hi all. I could really use some help on adding a node to the end of a doubly linked list. First off here is my node class along with the class where the adding should take place. For simplicity I did not add the other parts and classes of the code cause I do not think they are relevant to understanding the concept.

The main focus is the add method. So far I've created a node with the element inside it. After that I'm really confused as to what should point to what. Any comments on the concept would help. Thanks.

public class ListNode<T> {
	public T element;
	public ListNode<T> next, prev;
	
	public ListNode(T ele) {
		element = ele;
		prev = next = null;
	}
}


//Here is the class where the node is being added. 

public class LinkedListVector<T> implements VectorADT<T> {

	private ListNode<T> head; // link to the head of the linked list.
	private ListNode<T> tail; // to the tail of the linked list
	private int myCt; // Number of items contained in this vector.

	// Create empty vector
	public LinkedListVector() {

		myCt = 0;
		head = tail = null;

	}

	// Insert obj after the final element in this vector.
	public void add(T obj) {

		ListNode<T> newNode = new ListNode<T>(obj);
		ListNode<T> current = head; 

		
		//myCt++;
}
	public String toString() {
		String s;
		if (isEmpty())
			s = "empty vector";
		else {
			ListNode<T> temp = head;
			s = "< " + temp.element;
			for (int i = 1; i < myCt; i++) {
				s += ", " + temp.next.element;
				temp = temp.next;
			}
			s += " >";
		}
		return s;
	}

Please create a small, simple program that compiles, executes and shows the problem.
Hard to test code that does not execute.
You can leave out the parts the are not relevant.

Have you tried debugging the code by adding printlns to show how the variables change as the code is executed?

This question has already been answered. Start a new discussion instead.