Help with my Linked List program

It compiles but something is not right

Page 1 of 1

8 Replies - 836 Views - Last Post: 27 August 2008 - 08:59 PM Rate Topic: -----

#1 Dio1080  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-March 08

Help with my Linked List program

Posted 27 August 2008 - 04:39 PM

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
 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-----------------------------------------
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");
	}
}
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with my Linked List program

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Help with my Linked List program

Posted 27 August 2008 - 04:50 PM

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?
Was This Post Helpful? 0
  • +
  • -

#3 Dio1080  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-March 08

Re: Help with my Linked List program

Posted 27 August 2008 - 04:59 PM

i think it has something to do with the delete function, line 44 in the code...but not sure why....
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Help with my Linked List program

Posted 27 August 2008 - 05:27 PM

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 :-)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Help with my Linked List program

Posted 27 August 2008 - 05:34 PM

This seems weird to me

   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

	// 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

		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


Was This Post Helpful? 0
  • +
  • -

#6 Dio1080  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-March 08

Re: Help with my Linked List program

Posted 27 August 2008 - 05:43 PM

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 August 2008 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Help with my Linked List program

Posted 27 August 2008 - 05:47 PM

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)
Was This Post Helpful? 0
  • +
  • -

#8 Dio1080  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-March 08

Re: Help with my Linked List program

Posted 27 August 2008 - 06:52 PM

ok, i got it to work, thanks for your help guys
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Help with my Linked List program

Posted 27 August 2008 - 08:59 PM

View PostDio1080, on 27 Aug, 2008 - 04:59 PM, said:

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;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1