0
votes

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?

1
In removeBack(), have another look at if (head.getNext() == null) { return null; } and at the return head.getData(); at the end, and explain to yourself why you believe those are correct. Hint: They are not. - Andreas
@Andreas thank you for your advice! I'm having a bit of trouble learning Stacks and linked lists. I know my question was probably stupid, but my brain is just so fried. Thanks again. - Jonathan
We all mess up, don't worry about that. I've learned from experience, that quite often when you have code that doesn't behave and you ask someone to help you, and then try to explain to them what you're doing, the act of explaining makes you think of it differently, and you suddenly see clearly what you did wrong. You then thank the person for the help, even if they haven't said a word. --- So I was serious when I said "explain to yourself why you believe those are correct". The act of explaining may clear/redirect your mind to see what is wrong with that code. - Andreas
Here is another hint for you: What is the return value? Seems you want to return the value (data) 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. Is head the last node? - Andreas

1 Answers

0
votes

First of all this code is unnecessary, as an object that has no reference will just be thrown away.

t.setNext(null);
t = null;

This also may help removeBack()

if(head == null) {
   return null;
}

LindedListNode current = head;

while(current.getNext() != null) //searching for last node
   current = current.getNext();

current = null; //make the last node null