0

I had basically no issue with linked lists in C++, but this is getting to me for some reason. I had a single node being printed out using the other classes in the package that were provided, but as I go on I just keep running into walls.

The code below is in shambles due to me tinkering around. I just have no idea where to go from here. As of now that is getting a null pointer exception.

Just for information: poll() is just removing the current head and returning it, offer() is adding to the rear. As of now the exception is at oldLast.next = last in the offer method.

I am not asking for anyone to completely solve this. I just need some tips to progress.

public class FIFOQueue implements Queue {

//put your name as the value of the signature.
String signature = "name";

Node head = new Node(null);
Node pointer = head;
Node first;
Node last;
Node prev;
Node curr;

class Node {
    Process process;
    Node next;


    Node(Process p) {
        this.process = p;
        this.next = null;
    }

}

@Override
public void offer(Process p) {


    if(head == null)
    {
        head = new Node(p);
        first = head;
        last = head;

    }

    else
    {

        Node oldLast = last;
        Node newNode = new Node(p);

        last = newNode;
        oldLast.next = last;


    }



}


@Override
public Process poll() {


    if(isEmpty())
        throw new NoSuchElementException();

    Node oldPointer = first;

    first = first.next;
    head = first;


        return oldPointer.process;
}

@Override
public boolean isEmpty() {

return head == null;

}

@Override
public String getSignature() {
    return signature;
}

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Clannadqs
  • 5
  • 5
  • 1
    You need to be more specific. You're more likely to get an answer if you say "I get an `ArrayIndexOutOfBoundsException` on line 15" than "Things don't work and I hope you can help me". – Kayaman Feb 10 '17 at 06:58
  • True enough, sorry. As of now I am getting the exception from " oldLast.next = last;" in the offer function. – Clannadqs Feb 10 '17 at 07:02
  • If you're getting an `NPE` from there, then your `oldLast` is null. See http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it for whenever you get a `NullPointerException`. – Kayaman Feb 10 '17 at 07:06
  • Will do. Thanks for the help! I'm going to assume there are more issues with the code, though. – Clannadqs Feb 10 '17 at 07:09

2 Answers2

0

I think your core issue is here:

Node prev;
Node curr;

These are confusing you. Remove them.

  1. Node prev; - This should be in the Node class.
  2. Node curr; - This should be a local variable, not an instance variable.

Also

Node head = new Node(null);

does not gel with

if(head == null)
{
    head = new Node(p);

Either make head == null mean the list is empty or something else - but be consistent.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • I don't believe I have used either yet. This is a work in progress that I was tinkering around with to see if something worked. Using those two variables was my next move. I have only declared them so far. – Clannadqs Feb 10 '17 at 07:13
  • Woke up and saw your edit. Thanks for getting back to me. I'm kind of confused as to how those two don't line up. Should head stay null? – Clannadqs Feb 10 '17 at 13:55
  • Your comment basically helped me solve my issue. Figured out that the initial check should have been to see if the actual data in the node was null instead of checking if the entire node was null. Thanks! – Clannadqs Feb 10 '17 at 14:53
0

(Posted on behalf of the OP).

public void offer(Process p) {


    if(head.process == null)
    {
        head = new Node(p);
        first = head;
        last = head;
    }


        last.next = new Node(p);
        last = last.next;

}

This solved my issue. Can't believe I let this confuse me.

halfer
  • 19,824
  • 17
  • 99
  • 186