Major Problem with Doubly linked list program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 3
Reputation: 123qwerty123 is an unknown quantity at this point 
Solved Threads: 0
123qwerty123 123qwerty123 is offline Offline
Newbie Poster

Major Problem with Doubly linked list program

 
0
  #1
Apr 19th, 2009
For the most parts I believe my program is correct, when compiled it doesn't show any errors. The problem that I do have is that I cannot the information on my linked list to print. When the print out comes out it's suppose to show a list of names instead it shows the number "1".

header files (3)
  1. #ifndef NODE_H
  2. #define NODE_H
  3. # include <string>
  4. using std::string;
  5.  
  6. class Node
  7. {public:
  8. Node();
  9. private:
  10. string data;
  11. Node* previous;
  12. Node* next;
  13. friend class List;
  14. friend class Iterator;};
  15. #endif;
  16.  
  17.  
  18. #ifndef ITERATOR_H
  19. #define ITERATOR_H
  20. # include <string>
  21. #include "list.h"
  22. #include "node.h"
  23. using std::string;
  24.  
  25. class Iterator
  26. { public:
  27. Iterator();
  28. string get() const;
  29. void next();
  30. void previous();
  31. bool equals() const;
  32.  
  33. private:
  34. class Node* position;
  35. class Node* last;
  36. friend class List;};
  37. #endif;
  38.  
  39. #ifndef LIST_H
  40. #define LIST_H
  41. # include <string>
  42. # include "node.h"
  43. #include"iterator.h"
  44. #include<iostream>
  45. using std::string;
  46.  
  47. class List
  48. { public:
  49. List();
  50. void push_back();
  51. Iterator begin();
  52. Iterator end();
  53. void display();
  54.  
  55. private:
  56. Node* first;
  57. Node* last;
  58. friend class Node;};
  59. #endif;

the other files (4)

  1. # include "node.h"
  2. #include "list.h"
  3. using std::string;
  4.  
  5. Node::Node()
  6. {Node* t1 = new Node();
  7. t1->previous = NULL;
  8. t1->data = "Allen";
  9. t1->next =NULL;
  10.  
  11. Node *t2 = new Node();
  12. t2->previous = t1;
  13. t2->data = "Tom";
  14. t2->next = NULL;
  15. t1->next = t2;
  16.  
  17. Node *t3 = new Node();
  18. t3->previous = t2;
  19. t3->data = "John";
  20. t3->next = NULL;
  21. t2->next = t3;
  22.  
  23. Node *t4 = new Node();
  24. t4->previous = t3;
  25. t4->data = "Jerry";
  26. t4->next = NULL;
  27. t3->next = t4;
  28.  
  29. Node *t5 = new Node();
  30. t5->previous = t4;
  31. t5->data = "Fiona";
  32. t5->next = NULL;
  33. t4->next = t5;
  34.  
  35. Node *t6 = new Node();
  36. t6->previous = t5;
  37. t6->data = "Chris";
  38. t6->next = NULL;
  39. t5->next = t6;
  40.  
  41. Node *t7 = new Node();
  42. t7->previous = t6;
  43. t7->data = "Wolf";
  44. t7->next = NULL;
  45. t6->next = t7;
  46.  
  47. Node *t8 = new Node();
  48. t8->previous = t7;
  49. t8->data = "Lion";
  50. t8->next = NULL;
  51. t7->next = t8;
  52.  
  53. Node *t9 = new Node();
  54. t9->previous = t8;
  55. t9->data = "Tiger";
  56. t9->next = NULL;
  57. t8->next = t9;
  58.  
  59. Node *t10 = new Node();
  60. t10->previous = t9;
  61. t10->data = "Dog";
  62. t10->next = NULL;
  63. t9->next = t10;
  64. }
  65.  
  66. # include "iterator.h"
  67. # include "node.h"
  68. # include "assert.h"
  69. #include<iostream>
  70. using std::string;
  71.  
  72. Iterator::Iterator()
  73. {
  74. position = NULL;
  75. last = NULL;
  76. }
  77.  
  78. string Iterator::get() const
  79. {
  80. assert(position != NULL);
  81. return position->data;
  82. }
  83.  
  84. void Iterator::next()
  85. {
  86. assert(position != NULL);
  87. position = position->next;
  88. }
  89.  
  90. void Iterator::previous()
  91. {
  92. if (position == NULL)
  93. position = last;
  94. else position = position->previous;
  95. assert(position != NULL);
  96. }
  97.  
  98.  
  99. #include<iostream>
  100. # include "node.h"
  101. # include "list.h"
  102. using namespace std;
  103.  
  104.  
  105. List::List()
  106. {
  107. first = NULL;
  108. last = NULL;
  109. }
  110.  
  111. void List::push_back()
  112. {
  113. Node* newnode = new Node;
  114. if (last == NULL)
  115. {
  116. first = newnode;
  117. last = newnode;
  118. }
  119. else
  120. {
  121. newnode->previous = last;
  122. last->next = newnode;
  123. last = newnode;
  124. }
  125. }
  126.  
  127. Iterator List::begin()
  128. {
  129. Iterator iter;
  130. iter.position = first;
  131. iter.last = last;
  132. return iter;
  133. }
  134.  
  135. Iterator List::end()
  136. {
  137. Iterator iter;
  138. iter.position = NULL;
  139. iter.last = last;
  140. return iter;
  141. }
  142. void List::display()
  143. {
  144. Node* n;
  145. for(n = first; n != NULL; n = n->next)
  146. {cout<<" "<<n->data;}
  147.  
  148. }
  149.  
  150.  
  151. #include<iostream>
  152. #include"node.h"
  153. #include"list.h"
  154. #include"Iterator.h"
  155. #include<string>
  156. using namespace std;
  157.  
  158. int main()
  159.  
  160. {
  161. List mylist;
  162.  
  163. cout<<" I have used linked list to manage my soldiers."<<endl;
  164. cout<<"Currently on the list are: "<<&List::display<<endl;
  165.  
  166. return 0;
  167.  
  168. }


Thank in advance everyone. I really need your help
Last edited by 123qwerty123; Apr 19th, 2009 at 6:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: Major Problem with Doubly linked list program

 
0
  #2
Apr 19th, 2009
The correct syntax for calling a method of an instance of a class is:
  1. objectName.method(args)

In your case:
  1. cout << "Currently on the list are: " << mylist.display() << endl;

That may or may not work, depending on whether there are other errors may be in the code.

By the way, that list constructor is ... astonishing is the nicest word I can think of. It makes very little sense to write a list class that is hard-coded to produce only a specific list of pre-defined items. I can honestly say I've never seen anything quite like that. A more normal approach is to have a constructor that produces an empty list containing 0 or 1 node, and another method that adds nodes (and their data contents) one at a time on demand with values that are determined at that time based on user input, file input, or data that is generated as a result of the program's processing.
Last edited by r.stiltskin; Apr 19th, 2009 at 10:35 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC