View Single Post
Join Date: Mar 2008
Posts: 345
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Linked List: Inserting a new node

 
0
  #1
Nov 20th, 2008
Hello.

I am currently having problems. I am asking a user to input the position and number of where they would like to insert into the Linked List. After the user input the information, the number does not appear in the Linked List, but everything else does. Can someone help me fix this problem?

I am asking the user:

  1. cout<<"Enter a position and number to add to Link List"<<endl;
  2. cin>>index;
  3. cin>>number;

in the createList function.


Here is my full program: (Please ignore the functions that are commented out)


  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodeType
  5. {
  6. int info;
  7. nodeType *link;
  8. };
  9.  
  10.  
  11. void createList (nodeType*& first, nodeType*& last);
  12. void deleteFront (nodeType*& first,nodeType*& last);
  13. nodeType *GetNth (nodeType *first, int index);
  14. void insertBack (nodeType*& first, nodeType*& last, int info);
  15. void InsertNth (nodeType*& first, nodeType*& last,
  16. int index, int info);
  17. int modifyNum (nodeType*& first);
  18. void printList (nodeType*& first);
  19. int searchNum (nodeType*& first, int info);
  20.  
  21.  
  22. int main()
  23. {
  24.  
  25. nodeType *first, *last;
  26. int index;
  27. int info;
  28.  
  29. createList(first,last);
  30. printList(first);
  31.  
  32.  
  33. GetNth(first,index);
  34. InsertNth(first,last,index,info);
  35. // printList(first);
  36.  
  37. searchNum(first,info);
  38. modifyNum(first);
  39. // printList(first);
  40.  
  41. insertBack(first,last,info);
  42. // printList(first);
  43.  
  44. deleteFront(first,last);
  45. // printList(first);
  46.  
  47.  
  48.  
  49. system("PAUSE");
  50. return 0;
  51. }
  52.  
  53.  
  54.  
  55.  
  56. void createList(nodeType*& first, nodeType*& last)
  57. {
  58. int number;
  59. nodeType *newNode;
  60. int counter = 0;
  61. int index = 0;
  62.  
  63. first = NULL;
  64. last = NULL;
  65. cout<<"Enter an integer (-999 to stop): ";
  66. cin>>number;
  67. cout<<endl;
  68. while (number != -999)
  69. {
  70. InsertNth (first, last, counter++, number);
  71. if (counter == 1)
  72. {
  73. last = first;
  74. }
  75. cout<<"Enter an integer (-999 to stop): ";
  76. cin>>number;
  77. cout<<endl;
  78. }
  79. cout<<"Enter a position and number to add to Link List"<<endl;
  80. cin>>index;
  81. cin>>number;
  82. if (first == NULL)
  83. cout<<"Empty list"<<endl;
  84. else
  85. cout<<"There are "<<counter<<" items in the linked list"<<endl;
  86. }
  87.  
  88.  
  89.  
  90. void deleteFront (nodeType*& first,nodeType*& last)
  91. {
  92. //if (first != NULL)
  93. //{
  94. //nodeType* next = first;
  95. //first = first->link;
  96. //if (last == next)
  97. //{
  98. //last = NULL;
  99. //}
  100. //delete (next);
  101. //}
  102. }
  103.  
  104.  
  105. int modifyNum (nodeType*& first)
  106. {
  107.  
  108. int searchCount = 0;
  109. int num = 1;
  110. int Newnum = 4;
  111. bool found = false;
  112. nodeType *current;
  113.  
  114. current = first;
  115.  
  116. while (current != NULL)
  117. {
  118. if (current->info == num)
  119. {
  120. current->info = 5;
  121. cout<<endl;
  122. }
  123. current = current->link;
  124.  
  125. }
  126.  
  127. }
  128.  
  129. nodeType *GetNth(nodeType *first,int index)
  130. {
  131. nodeType *current = first;
  132. while ((current != NULL) && (index > 0))
  133. {
  134. current = current->link;
  135. index--;
  136. }
  137. return (current);
  138. }
  139.  
  140.  
  141.  
  142. void insertBack (nodeType*& first, nodeType*& last, int info)
  143. {
  144. //nodeType *newNode = new nodeType; // create new node
  145. //newNode->info = info;
  146. //newNode->link = NULL;
  147. //if (last == NULL)
  148. //{
  149. //first = last = newNode;
  150. //}
  151. //else
  152. //{
  153. //last->link = newNode;
  154. //}
  155. }
  156.  
  157.  
  158.  
  159. void InsertNth(nodeType*& first,nodeType*& last,int index, int info)
  160. {
  161. nodeType *newNode = new nodeType; // create new node
  162. newNode->info = info;
  163. newNode->link = NULL;
  164. int number;
  165.  
  166.  
  167. if (first == NULL)
  168. {
  169. first = newNode;
  170. }
  171. else
  172. {
  173. nodeType *next = first;
  174. while ((next->link != NULL) && (index > 0))
  175. {
  176. next = next->link;
  177. index--;
  178. }
  179. newNode->link = next->link;
  180. next->link = newNode;
  181. if (newNode->link == NULL)
  182. {
  183. last = newNode;
  184. }
  185. }
  186.  
  187. }
  188.  
  189.  
  190.  
  191. void printList (nodeType*& first)
  192. {
  193. nodeType *next = first;
  194. int index = 0;
  195. while (next != NULL)
  196. {
  197. cout<<endl;
  198. cout << "Position: " << index << ", Number = " << next->info << endl;
  199. index++;
  200. next = next->link;
  201. }
  202. cout << endl << endl;
  203. }
  204.  
  205.  
  206. int searchNum (nodeType*& first, int info)
  207. {
  208. int index = 0;
  209. nodeType *next = first;
  210. while ((next != NULL) && (next->info != info))
  211. {
  212. next = next->link;
  213. index++;
  214. }
  215. if (next->info != info)
  216. {
  217. index = -1;
  218. }
  219. return (index);
  220. }
Last edited by NinjaLink; Nov 20th, 2008 at 2:35 am.
Reply With Quote