I'm trying to write a method to insert a node and remove a node at the back of a linked list. Here's the main class I'm writing the methods in. They're at the bottom (insertBack and removeBack):
public class LinkedList { private LinkedListNode head;
public LinkedList()
{
head = null;
}
public LinkedListNode getHead()
{
return head;
}
public void setHead(LinkedListNode h)
{
head = h;
}
public void insertFront(Object item)
{
if (head == null)
{
LinkedListNode nextNode = new LinkedListNode (item);
head = nextNode;
}
else
{
LinkedListNode nextNode = new LinkedListNode (item);
nextNode.setNext(head);
head = nextNode;
}
}
public Object removeFront()
{
if (head == null)
{
return head;
}
else
{
LinkedListNode t = head;
Object ret = head.getData();
head = head.getNext();
t.setNext(null);
t = null;
return ret;
}
}
//insert a front at the end of linked list
public void insertBack(Object myData)
{
LinkedListNode newNode = new LinkedListNode(myData);
if (head == null)
{
head = newNode;
}
else
{
LinkedListNode current = head;
while (current.getNext() != null)
{
current = current.getNext();
}
current.setNext(newNode);
}
}
//remove a node at the end of linked list
public Object removeBack()
{
if (head == null)
{
return null;
}
if (head.getNext() == null)
{
return null;
}
LinkedListNode secondToLast = head;
while (secondToLast.getNext().getNext() != null)
{
secondToLast = secondToLast.getNext();
}
secondToLast.setNext(null);
return head.getData();
}
}
There's probably formatting errors as I pasted this here, but I'm still trying to figure out how to use this site. When I run my driver class, shown below, I get the results shown below that.
public static void main(String[] args)
{
ValueData data1 = new ValueData("a", 50);
ValueData data2 = new ValueData("b", 70);
ValueData data3 = new ValueData("c", 100);
LinkedList myList = new LinkedList();
//practice insertBack and removeBack
myList.insertBack(data1);
myList.insertBack(data2);
myList.insertBack(data3);
System.out.println("Test insertBack and removeBack");
System.out.println((ValueData) myList.removeBack());
System.out.println((ValueData) myList.removeBack());
System.out.println((ValueData) myList.removeBack());
System.out.println();
//compare insertFront and removeFront
System.out.println("Test insertFront and removeFront");
myList.insertFront(data1);
myList.insertFront(data2);
myList.insertFront(data3);
System.out.println((ValueData)myList.removeFront());
System.out.println((ValueData)myList.removeFront());
System.out.println((ValueData)myList.removeFront());
System.out.println();
RESULTS
Test insertBack and removeBack
a 50.0
a 50.0
null
Test insertFront and removeFront
c 100.0
b 70.0
a 50.0
Can anyone help me figure out why my removeFront and removeBack methods are not working?
removeBack()
, have another look atif (head.getNext() == null) { return null; }
and at thereturn head.getData();
at the end, and explain to yourself why you believe those are correct. Hint: They are not. - Andreasdata
) of the last element, i.e. the node that is being removed from the list. ---if (head.getNext() == null) { return null; }
The if statement detects that list has only one element, so that is the last element. Do you really want to leave the list unchanged and return null? ---return head.getData();
You're supposed to return the data of the last element. Ishead
the last node? - Andreas