Linked List help

Thread Solved

Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

Linked List help

 
0
  #1
Aug 27th, 2008
The program compiles but is not run right can somebody help plz? There are two parts a class and a main.

  1. public class LinkedList56 {
  2.  
  3. private class node{
  4. int data;
  5. node next;
  6.  
  7. }
  8. //create an empty linked list
  9. public LinkedList56(){
  10. first = null;
  11. }
  12. //return true if the list is empty, otherwise return false
  13. public boolean empty(){
  14. if(first == null){
  15.  
  16.  
  17. return true;
  18. }
  19. else
  20. return false;
  21. }
  22. //insert a value x at the end
  23. public void InsertAtEnd(LinkedList56 x, int a){
  24. //create new node
  25. node q = new node();
  26. node p = new node();
  27. q.data = a;
  28. q.next = null;
  29. //empty list
  30. if(first == null){
  31. first = q;
  32.  
  33. }
  34. //list is not empty
  35. else
  36. p = first;
  37. while(p.next != null){
  38. p = p.next;
  39.  
  40. }
  41. p.next = q;
  42. }
  43. //if value x is in the list, romove x
  44. public void Delete(int a){
  45. node pointer = new node();
  46. node doublepointer = new node();
  47.  
  48. pointer = first;
  49. doublepointer = pointer.next;
  50.  
  51. while(doublepointer.data != a){
  52. pointer = doublepointer.next;
  53. //preptr = preptr.next;
  54. doublepointer = doublepointer.next;
  55. }
  56. pointer = doublepointer.next;
  57. }
  58.  
  59.  
  60. //Display the data values in the linked list
  61. public void Display(){
  62. System.out.println();
  63. }
  64. //pointer to the first node in the list
  65. private node first;
  66. }

--------------------------------------MAIN----------------------------------
  1. public class Program2 {
  2. public static void main(String[] args)throws Exception{
  3.  
  4. LinkedList56 x = new LinkedList56();
  5.  
  6. for(int a = 1; a < 10; a++){
  7. if((a%2 == 0)){
  8. x.InsertAtEnd(x,a);
  9.  
  10. x.Display();
  11.  
  12. x.Delete(2);
  13. System.out.println("list after deleting 2:");
  14. x.Display();
  15. x.Delete(6);
  16. System.out.println("list after deleting 6:");
  17. x.Display();
  18. if(!x.empty()){
  19. System.out.println("List is not empty");
  20. }
  21. }
  22. }
  23. }
  24. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

Re: Linked List help

 
0
  #2
Aug 27th, 2008
its the delete function, something is not right, line 44...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Linked List help

 
0
  #3
Aug 29th, 2008
Thought it would be easier if we all chipped in and helped you out but it would beat the actual purpose of learning things.

If you are still in the stages of developing your programs in a text editor, litter your program with log statements at appropriate places and find the point wherein the program starts behaving in an unexpected manner. If using an IDE like Eclipse, debug your application and inspect the variables at run time. Guesswork should not be the approach here...
Last edited by ~s.o.s~; Aug 29th, 2008 at 12:04 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

Re: Linked List help

 
0
  #4
Aug 29th, 2008
Thanks for the advice, btw the program was solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC