943,614 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1044
  • C++ RSS
Apr 11th, 2007
0

linked lists show next

Expand Post »
Hey,
I have a singly linked list that has a show all function that looks like this

virtual void Show()
   {
       myObject->Show(); myNext->Show();
   } 

Where myObject is a class ordered by an int. This function prints out all information in the list.

I was wondering how I could change this at the start print out just the first object then on user input move on to the next object?

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirkraven is offline Offline
16 posts
since May 2006
Apr 11th, 2007
0

Re: linked lists show next

Sorry. I’ve got the print out the 1st object only bit to work! Just had to take out the myNext->Show() bit from the code above. Anyone have any idea about the showing the next object in the list?
Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirkraven is offline Offline
16 posts
since May 2006
Apr 11th, 2007
1

Re: linked lists show next

Loop through the linked list. Start at the head, and show it, then move to the next node and show it, and so on until the current node's next pointer is null.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 11th, 2007
0

Re: linked lists show next

I know I’m going to sound like a fool now but how do you do that?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirkraven is offline Offline
16 posts
since May 2006
Apr 11th, 2007
0

Re: linked lists show next

C++ Syntax (Toggle Plain Text)
  1.  
  2. for(Node *temp = head; temp != NULL; temp = temp->next)
  3. {
  4. cout << temp->data;
  5. }
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 11th, 2007
0

Re: linked lists show next

Thanks! But the way I’ve set up my linked list I can’t convert a node to a head node and then change it back to a node!

I don’t suppose you know somewhere where that has an example of a good linked list to save me going all the way through google? Or even a tutorial that shows how to use the STL linked lists?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirkraven is offline Offline
16 posts
since May 2006
Apr 11th, 2007
0

Re: linked lists show next

When you create a new list, you should make a Node *head. This will serve as the beginning of the list. No matter what you do with your list, always make sure the head pointer is pointing to the first index of the linked list.

Such as...

[code]
struct Node
{
int element;
Node *next;
}
Node *head;

// some where else in code

Node *head = new Node;
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 11th, 2007
0

Re: linked lists show next

Ok, I'm getting somwhere now!
How do I change this linked list to look more like what you’ve said?

C++ Syntax (Toggle Plain Text)
  1. template <class T>
  2. class Node
  3. {
  4. public:
  5. Node(){}
  6. virtual ~Node(){}
  7. virtual Node * Insert(T * theObject) =0;
  8. virtual void Show() = 0;
  9. virtual void ShowNext() = 0;
  10. private:
  11. };
  12.  
  13.  
  14. template <class T>
  15. class InternalNode: public Node<T>
  16. {
  17. public:
  18. InternalNode(T * theObject, Node<T> * next);
  19. ~InternalNode(){delete myNext; delete myObject;}
  20. virtual Node<T> * Insert(T *theObject);
  21. virtual void Show()
  22. {
  23. myObject->Show();system("PAUSE"); myNext->Show();
  24. }
  25.  
  26. virtual void ShowNext()
  27. {
  28. // myObject->Show(); show first
  29. }
  30. private:
  31. T * myObject; //the data
  32. Node<T> * myNext; // the next bit of data in the list
  33. };
  34.  
  35. //constructor
  36. template <class T>
  37. InternalNode<T>::InternalNode(T* theObject, Node<T> * next):
  38. myObject(theObject), myNext(next)
  39. {
  40. }
  41.  
  42.  
  43.  
  44.  
  45. //the main part of the list
  46. // when u put new object in to the list
  47. //it is passed to the node that figures out
  48. //where it goes
  49. template <class T>
  50. Node<T> * InternalNode<T>::Insert(T * theObject)
  51. {
  52. //is the new data bigger or smaller
  53. int result = myObject->Compare(*theObject);
  54.  
  55. switch(result)
  56. {
  57. // by convention if new data is same as old it goes 1st
  58. case KIsSame: //fallthrough
  59. case KIsLarger: //new data comes b4 old
  60. {
  61. InternalNode<T> * ObjectNode =
  62. new InternalNode<T>(theObject, this);
  63. return ObjectNode;
  64. }
  65. // if it is bigger than the old pass it on
  66. //to the next node and let it handle it
  67. case KIsSmaller:
  68. myNext = myNext->Insert(theObject);
  69. return this;
  70. }
  71. return this; //appease the compiler
  72. }
  73.  
  74.  
  75. //Tail node is just a sentinel
  76. template <class T>
  77. class TailNode : public Node<T>
  78. {
  79. public:
  80. TailNode(){}
  81. virtual ~TailNode(){}
  82. virtual Node<T> * Insert(T * theObject);
  83. virtual void Show(){}
  84. virtual void ShowNext(){}
  85. private:
  86. };
  87.  
  88.  
  89.  
  90. //if data comes to here it must be b4
  91. //because this is the tail and nothing comes after it
  92. template <class T>
  93. Node<T> * TailNode<T>::Insert(T * theObject)
  94. {
  95. InternalNode<T> * ObjectNode = new InternalNode<T>(theObject, this);
  96. return ObjectNode;
  97. }
  98.  
  99.  
  100. //Head node has no data, it just points to the
  101. // begining of the list
  102. template <class T>
  103. class HeadNode : public Node<T>
  104. {
  105. public:
  106. HeadNode();
  107. virtual ~HeadNode(){delete myNext;}
  108. virtual Node<T> * Insert(T * theObject);
  109. virtual void Show(){myNext->Show();}
  110. virtual void ShowNext(){myNext->ShowNext();}
  111. //private:
  112. Node<T> * myNext;
  113. };
  114.  
  115.  
  116. //as soon as head is
  117. //created so is the tail
  118. template <class T>
  119. HeadNode<T>::HeadNode()
  120. {
  121. myNext = new TailNode<T>;
  122. }
  123.  
  124.  
  125. //Nothing comes b4 the head so just
  126. // pass the data to the next node
  127. template <class T>
  128. Node<T> * HeadNode<T>::Insert(T * theObject)
  129. {
  130. myNext = myNext->Insert(theObject);
  131. return this;
  132. }
  133. //the linked list
  134. template <class T>
  135. class LinkedList
  136. {
  137. public:
  138. LinkedList();
  139. ~LinkedList(){delete myHead;}
  140. void Insert(T * theObject);
  141. void ShowAll(){myHead->Show();}
  142. //print out 1st node
  143. void ShowFirst(){myHead->ShowNext();}
  144. /* void ShowNext()
  145.   {
  146.   for(HeadNode<T> *temp = myHead; temp != NULL; temp = temp->myNext)
  147.   {
  148.   cout << temp->data;
  149.   }
  150.   }*/
  151. private:
  152. HeadNode<T> * myHead;
  153. Node<T> * myNext;
  154. };
  155.  
  156.  
  157.  
  158. //create the head node
  159. //which creates the tail node
  160. //so an empty list points to the head which
  161. //points to the empty tail
  162. template <class T>
  163. LinkedList<T>::LinkedList()
  164. {
  165. myHead = new HeadNode<T>;
  166. }
  167. //delegation
  168. template <class T>
  169. void LinkedList<T>::Insert(T * pObject)
  170. {
  171. myHead->Insert(pObject);
  172. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirkraven is offline Offline
16 posts
since May 2006

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 C++ Forum Timeline: Multiple definition error
Next Thread in C++ Forum Timeline: cin skip ?





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


Follow us on Twitter


© 2011 DaniWeb® LLC