943,545 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 891
  • Java RSS
Aug 27th, 2008
0

Linked List help

Expand Post »
The program compiles but is not run right can somebody help plz? There are two parts a class and a main.

java Syntax (Toggle Plain Text)
  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----------------------------------
java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Aug 27th, 2008
0

Re: Linked List help

its the delete function, something is not right, line 44...
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Aug 29th, 2008
0

Re: Linked List help

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Aug 29th, 2008
0

Re: Linked List help

Thanks for the advice, btw the program was solved.
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I need Extract Email from html or doc code
Next Thread in Java Forum Timeline: Need Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC