Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,247 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,228 people online right now. Registration is fast and FREE... Join Now!




Help with my Linked List program

 
Reply to this topicStart new topic

Help with my Linked List program, It compiles but something is not right

Dio1080
27 Aug, 2008 - 03:39 PM
Post #1

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 12


My Contributions
Ok, my program compiles but when I run it, something is not right can anybody take a look at it too see whats wrong? There are two parts, one is a class and the other is a main
java

public class LinkedList56 {

private class node{
int data;
node next;

}
//create an empty linked list
public LinkedList56(){
first = null;
}
//return true if the list is empty, otherwise return false
public boolean empty(){
if(first == null){


return true;
}
else
return false;
}
//insert a value x at the end
public void InsertAtEnd(LinkedList56 x, int a){
//create new node
node q = new node();
node p = new node();
q.data = a;
q.next = null;
//empty list
if(first == null){
first = q;

}
//list is not empty
else
p = first;
while(p.next != null){
p = p.next;

}
p.next = q;
}
//if value x is in the list, romove x
public void Delete(int a){
node pointer = new node();
node doublepointer = new node();

pointer = first;
doublepointer = pointer.next;

while(doublepointer.data != a){
pointer = doublepointer.next;
//preptr = preptr.next;
doublepointer = doublepointer.next;
}
pointer = doublepointer.next;
}


//Display the data values in the linked list
public void Display(){
System.out.println();
}
//pointer to the first node in the list
private node first;
}


-------------------------------------------MAIN-----------------------------------------
java

public class Program2 {
public static void main(String[] args)throws Exception{

LinkedList56 x = new LinkedList56();

for(int a = 1; a < 10; a++){
if((a%2 == 0)){
x.InsertAtEnd(x,a);

x.Display();

x.Delete(2);
System.out.println("list after deleting 2:");
x.Display();
x.Delete(6);
System.out.println("list after deleting 6:");
x.Display();
if(!x.empty()){
System.out.println("List is not empty");
}
}
}
}
}

User is offlineProfile CardPM
+Quote Post

nick2price
RE: Help With My Linked List Program
27 Aug, 2008 - 03:50 PM
Post #2

D.I.C Head
**

Joined: 23 Nov, 2007
Posts: 231



Thanked: 6 times
My Contributions
It would be easier if u say why it is not right? For all we know, its not right cos u want it too hack the local bank! Why isnt it right?
User is offlineProfile CardPM
+Quote Post

Dio1080
RE: Help With My Linked List Program
27 Aug, 2008 - 03:59 PM
Post #3

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 12


My Contributions
i think it has something to do with the delete function, line 44 in the code...but not sure why....
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Help With My Linked List Program
27 Aug, 2008 - 04:27 PM
Post #4

D.I.C Head
**

Joined: 23 Nov, 2007
Posts: 231



Thanked: 6 times
My Contributions
I have never actually used nodes before, are they really needed for this program? What do you want to achieve? I am finding your coding style quite hard to read with the use of single character variable names. But isnt somthing like pointer = first; just meaning empty node = empty node? Anyways, thats besides the point. The problem u have does lie in your Delete method, more specifically your while loop within this. You have a NullPointerException which means the reference on which you're calling a method doesn't actually point to any object; i.e., it's null.
You can find your problem by using System.out.println statements printing out your variables or you can debug it using somthing like Xlint to get a more detailed description. Alternatively, you can wait for pbl to give you the reason straight away, but what can i say, we cant all be as knowledgable as him :-)
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help With My Linked List Program
27 Aug, 2008 - 04:34 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
This seems weird to me

CODE

   x.InsertAtEnd(x,a);  


If x is a LikedList, a standard version of it or your implementation, why should you pass the same linked list to your insert method as parameter ?

Got the answer, the parameter is not used

CODE

    // what do you do with X ?
     public void InsertAtEnd(LinkedList56 x, int a){  
       //create new node  
        node q = new node();  
        node p = new node();  
        q.data = a;  
        q.next = null;  
        //empty list  
        if(first == null){  
           first = q;  
          
        }  
        //list is not empty  
        else  
           p = first;  
        while(p.next != null){  
           p = p.next;  
          
        }  
        p.next = q;  
     }  


Your constructor of node does not initialized "data" OK will be 0 but initialize "first" to null

CODE

        node doublepointer = new node();       // creates a node "data" not initialized    
      
        pointer = first;                                   // this might be null
        doublepointer = pointer.next;  
      
        while(doublepointer.data != a){           // doublePointer points to null

User is offlineProfile CardPM
+Quote Post

Dio1080
RE: Help With My Linked List Program
27 Aug, 2008 - 04:43 PM
Post #6

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 12


My Contributions
ok, so then its not just the while loop in the delete function, pbl kind confused me a little bit.

This post has been edited by Dio1080: 27 Aug, 2008 - 04:44 PM
User is offlineProfile CardPM
+Quote Post

nick2price
RE: Help With My Linked List Program
27 Aug, 2008 - 04:47 PM
Post #7

D.I.C Head
**

Joined: 23 Nov, 2007
Posts: 231



Thanked: 6 times
My Contributions
Take pbl's advise. Remember, the while loop was throwing a NullPointerException because it had no object to point too, the objects are created elsewhere (where pbl has corrected)
User is offlineProfile CardPM
+Quote Post

Dio1080
RE: Help With My Linked List Program
27 Aug, 2008 - 05:52 PM
Post #8

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 12


My Contributions
ok, i got it to work, thanks for your help guys
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help With My Linked List Program
27 Aug, 2008 - 07:59 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(Dio1080 @ 27 Aug, 2008 - 04:59 PM) *

i think it has something to do with the delete function, line 44 in the code...but not sure why....

Sorry...

your "next" is null points to anything... you should check that you have no next node before

pointer = doublepointer.next;

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:52AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month