linked lists show next

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 16
Reputation: sirkraven is an unknown quantity at this point 
Solved Threads: 0
sirkraven sirkraven is offline Offline
Newbie Poster

linked lists show next

 
0
  #1
Apr 11th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 16
Reputation: sirkraven is an unknown quantity at this point 
Solved Threads: 0
sirkraven sirkraven is offline Offline
Newbie Poster

Re: linked lists show next

 
0
  #2
Apr 11th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: linked lists show next

 
1
  #3
Apr 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 16
Reputation: sirkraven is an unknown quantity at this point 
Solved Threads: 0
sirkraven sirkraven is offline Offline
Newbie Poster

Re: linked lists show next

 
0
  #4
Apr 11th, 2007
I know I’m going to sound like a fool now but how do you do that?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: linked lists show next

 
0
  #5
Apr 11th, 2007
  1.  
  2. for(Node *temp = head; temp != NULL; temp = temp->next)
  3. {
  4. cout << temp->data;
  5. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 16
Reputation: sirkraven is an unknown quantity at this point 
Solved Threads: 0
sirkraven sirkraven is offline Offline
Newbie Poster

Re: linked lists show next

 
0
  #6
Apr 11th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: linked lists show next

 
0
  #7
Apr 11th, 2007
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;
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 16
Reputation: sirkraven is an unknown quantity at this point 
Solved Threads: 0
sirkraven sirkraven is offline Offline
Newbie Poster

Re: linked lists show next

 
0
  #8
Apr 11th, 2007
Ok, I'm getting somwhere now!
How do I change this linked list to look more like what you’ve said?

  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. }
Reply With Quote Quick reply to this message  
Reply

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




Views: 920 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC