944,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10496
  • C++ RSS
Dec 7th, 2005
0

recursive linked list

Expand Post »
I am having trouble trying to implement my recursive method for inserting into a linked list. I made the recursive method private because it needs acces to the head pointer. Not sure where to go from here . Any help would be appreciated. Here is the code.

The .h file:
C++ Syntax (Toggle Plain Text)
  1. // *********************************************************
  2. // Header file ListP.h for the ADT list.
  3. // Pointer-based implementation.
  4. // *********************************************************
  5. #include "ListIndexOutOfRangeException.h"
  6. typedef int ListItemType;
  7.  
  8. class List
  9. {
  10. public:
  11. // constructors and destructor:
  12. List(); // default constructor
  13. List(const List& aList); // copy constructor
  14. ~List(); // destructor
  15.  
  16. // list operations:
  17. bool isEmpty() const;
  18. int getLength() const;
  19. void insert(int index, ListItemType newItem)
  20. throw (ListIndexOutOfRangeException);
  21. void remove(int index)
  22. throw (ListIndexOutOfRangeException);
  23. void retrieve(int index, ListItemType& dataItem) const
  24. throw (ListIndexOutOfRangeException);
  25. void insertRec(ListItemType newItem);
  26. //void InsertFront(const ListItemType & newItem);
  27. //void linkedListInsert(ListItemType newItem);
  28.  
  29.  
  30.  
  31. private:
  32. struct ListNode // a node on the list
  33. {
  34. ListItemType item; // a data item on the list
  35. ListNode *next; // pointer to next node
  36. }; // end struct
  37.  
  38. int size; // number of items in list
  39. ListNode *head; // pointer to linked list of items
  40.  
  41.  
  42. ListNode *find(int index) const;
  43. void linkedListInsert(ListNode *&head, ListItemType newItem);
  44. //Returns a pointer to the index-th node
  45. //in the linked list.
  46. }; // end class
  47. // End of header file.

the out of range .h file:

C++ Syntax (Toggle Plain Text)
  1. #include <stdexcept>
  2. #include <string>
  3. using namespace std;
  4. class ListIndexOutOfRangeException: public out_of_range
  5. {
  6. public:
  7. ListIndexOutOfRangeException(const string & message = "")
  8. : out_of_range(message.c_str())
  9. { }
  10. }; // end ListIndexOutOfRangeException

the .cpp file:
C++ Syntax (Toggle Plain Text)
  1. // *********************************************************
  2. // Implementation file ListP.cpp for the ADT list.
  3. // Pointer-based implementation.
  4. // *********************************************************
  5. #include <iostream>
  6. using namespace std;
  7. #include "RListP.h" // header file
  8. #include <cstddef> // for NULL
  9. #include <cassert> // for assert()
  10.  
  11.  
  12.  
  13.  
  14. List::List(): size(0), head(NULL)
  15. {
  16. } // end default constructor
  17.  
  18. List::List(const List& aList): size(aList.size)
  19. {
  20. if (aList.head == NULL)
  21. head = NULL; // original list is empty
  22.  
  23. else
  24. { // copy first node
  25. head = new ListNode;
  26. head->item = aList.head->item;
  27.  
  28. // copy rest of list
  29. ListNode *newPtr = head; // new list pointer
  30. // newPtr points to last node in new list
  31. // origPtr points to nodes in original list
  32. for (ListNode *origPtr = aList.head->next;
  33. origPtr != NULL;
  34. origPtr = origPtr->next)
  35. { newPtr->next = new ListNode;
  36. newPtr = newPtr->next;
  37. newPtr->item = origPtr->item;
  38. } // end for
  39.  
  40. newPtr->next = NULL;
  41. } // end if
  42. } // end copy constructor
  43.  
  44. List::~List()
  45. {
  46. while (!isEmpty())
  47. remove(1);
  48. } // end destructor
  49.  
  50. bool List::isEmpty() const
  51. {
  52. return size == 0;
  53.  
  54. } // end isEmpty
  55.  
  56. int List::getLength() const
  57. {
  58. return size;
  59. } // end getLength
  60.  
  61. List::ListNode *List::find(int index) const
  62. // --------------------------------------------------
  63. // Locates a specified node in a linked list.
  64. // Precondition: index is the number of the
  65. // desired node.
  66. // Postcondition: Returns a pointer to the desired
  67. // node. If index < 1 or index > the number of
  68. // nodes in the list, returns NULL.
  69. // --------------------------------------------------
  70. {
  71. if ( (index < 1) || (index > getLength()) )
  72. return NULL;
  73.  
  74. else // count from the beginning of the list
  75. { ListNode *cur = head;
  76. for (int skip = 1; skip < index; ++skip)
  77. cur = cur->next;
  78. return cur;
  79. } // end if
  80. } // end find
  81.  
  82. void List::retrieve(int index,
  83. ListItemType& dataItem) const throw(ListIndexOutOfRangeException)
  84. {
  85. if ((index < 1) || (index > getLength()))
  86. throw ListIndexOutOfRangeException(
  87. "ListIndexOutOfRangeException: retrieve index out of range");
  88. else
  89. { // get pointer to node, then data in node
  90. ListNode *cur = find(index);
  91. dataItem = cur->item;
  92. cout<<dataItem<<endl;
  93. } // end if
  94. } // end retrieve
  95.  
  96. void List::insert(int index, ListItemType newItem) throw(ListIndexOutOfRangeException)
  97. {
  98. int newLength = getLength() + 1;
  99.  
  100. if ((index < 1) || (index > newLength))
  101. throw ListIndexOutOfRangeException(
  102. "ListIndexOutOfRangeException: insert index out of range");
  103.  
  104. else
  105. { // create new node and place newItem in it
  106. ListNode *newPtr = new ListNode;
  107. size = newLength;
  108. newPtr->item = newItem;
  109.  
  110. // attach new node to list
  111. if (index == 1)
  112. { // insert new node at beginning of list
  113. newPtr->next = head;
  114. head = newPtr;
  115. }
  116. else
  117. { ListNode *prev = find(index-1);
  118. // insert new node after node
  119. // to which prev points
  120. newPtr->next = prev->next;
  121. prev->next = newPtr;
  122. } // end if
  123. } // end if
  124. } // end insert
  125.  
  126. void List::remove(int index) throw(ListIndexOutOfRangeException)
  127. {
  128. ListNode *cur;
  129.  
  130. if ((index < 1) || (index > getLength()))
  131. throw ListIndexOutOfRangeException(
  132. "ListIndexOutOfRangeException: remove index out of range");
  133. else
  134. {
  135. --size;
  136. if (index == 1)
  137. { // delete the first node from the list
  138. cur = head; // save pointer to node
  139. head = head->next;
  140. }
  141.  
  142. else
  143. { ListNode *prev = find(index-1);
  144. // delete the node after the
  145. // node to which prev points
  146. cur = prev->next; // save pointer to node
  147. prev->next = cur->next;
  148. } // end if
  149.  
  150. // return node to system
  151. cur->next = NULL;
  152. delete cur;
  153. cur = NULL;
  154. } // end if
  155. } // end remove
  156.  
  157. void linkedListInsert(ListNode *&head, ListItemType newItem);
  158. {
  159. if ((head == NULL) || (newItem < head->item))
  160. { // base case: insert newItem at beginning
  161. // of the linked list to which headPtr points
  162. ListNode *newPtr = new ListNode;
  163. newPtr->item = newItem;
  164. newPtr->next = head;
  165. head = newPtr;
  166. }
  167. else
  168. linkedListInsert(newItem);
  169. } // end linkedListInsert
  170.  
  171.  
  172. int main()
  173. {
  174. List aList;
  175. aList.linkedListInsert(5);
  176. aList.linkedListInsert(4);
  177. int a = 0;
  178. //a = aList.getLength();
  179. //cout<<a<<endl;
  180.  
  181. }
Reputation Points: 10
Solved Threads: 0
Unverified User
skeet123 is offline Offline
20 posts
since Nov 2004

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: MS Visual Studio/Getting jobs??
Next Thread in C++ Forum Timeline: 2d address calculation





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


Follow us on Twitter


© 2011 DaniWeb® LLC