public void Enqueue(int value){
        node end = endList.next;
        end.next=null;
        endList.data=value;
        endList.next=end;
        endList=end;

Here is my code for an Enqueue method but when I run it

mylist.Enqueue(12);

It gives me an error. Is something wrong with my method or am I testing it incorrectly? Thanks.

What is the error message? I'm not exactly sure how this is supposed to operate, but I'm assuming you endList.next = new node(); as otherwise endList.next will already be pointing to a node.

First, you need to be clear when you ask a question. As dmanw100 asked, what is the error message? Also, what is node class structure? I could assume that it is implemented as below but my assumption may lead me to a wrong cause.

class node {
  node next;
  integer data;
  ...
}

Second, I don't know what you endList is. Is it a node class at end? How did you initialise it? I am assuming (again!) that you initilise it to be null.

class MyQueue {
  node head;
  node endList;
  public MyQueue() {
    head=null;
    endList=head;
  }
}

// now in your test program
MyQueue myList = new MyQueue();
myList.enqueue(12);  // <-- ERROR, NullPointerException

When you enqueue, you must check whether the value is null (not assign a value). If it is null, you simply create a new node and assign the head & tail of the queue to the new node.

Another flaw in your code. You also must NOT use end = endList.next; because it is a null value and will not be tied to the endList!

/*
Assume you have successfully created a list and the endList stores a proper value
  (you won't with your current implementation).
    +---+---+---+
    | x | x | x |--> null
    +---+---+---+
      ^       ^
      |       |
    head     endList

From your current implementation with end=endList.next, it will produce...
    end = null
    +---+---+---+
    | x | x | x |--> null
    +---+---+---+     ^
      ^       ^       |__ end
      |       |
    head     endList

Then you attempt to assign a value to the null (end).
You will get an exception (NullPointerException)
*/    

List class

    List()
    {
        endList = new node(null, null);
        beginList = new node(null,endList);
        // our list is setup as an empty list

    }
    ...
        public static void main(String[] args) {
    // TODO Auto-generated method stub



    List mylist = new List();
    mylist.add(5);
    mylist.add(7);
    mylist.add(5);
    mylist.add(15);
    mylist.add(5);

    java.util.Iterator anIterator = mylist.iterator();
    for (int i=0;i<mylist.theSize;i++)
        System.out.print(anIterator.next() + " ");

    anIterator = mylist.new myIterator();
    anIterator.next();

    mylist.Enqueue(12);
    anIterator = mylist.new myIterator();
    for (int i=0;i<mylist.theSize;i++)
        System.out.print(anIterator.next() + " ");

Node Class

    public node()
    {
        next = null;
        data = null;
    }

    public node(e newdata, node newnext)
    {
        data = newdata;
        next = newnext;
    }
e data;
node next;
public int value;

...
Error: Exception in thread "main" java.lang.NullPointerException

My apologizes for not providing enough information. Based on your assumptions, it did clear up a lot of confusion.

Looking at your code, my assumption is correct -- you attempt to insert a value into a null value. In your enqueue(), you need to check whether the beginList has data (not null). If it has not, assign it to the beginList. Otherwise, you need to check if the endList has data (not null). If it has not, assign to the endList. Otherwise, you will need to create a new node and then append to your endList. Do not attempt to append before and then create because you are attempting to work on a null value.

Thank you very much I got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.