Linked Lists: Find length and check for empty list

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

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

Linked Lists: Find length and check for empty list

 
0
  #1
Nov 11th, 2008
Hello..


This is what my program is suppose to do:

-find length of list
-check for empty list
-insert in back of list
-delete from front of list

I have everything done except finding the length of list and check for empty list.


I need help getting the length of my Linked list. I'm using a counter, but where do I place the counter in my program?

Also, I need help checking for empty list. There is no code in the book that shows me how to complete this task, so I don't even know how to check for it. Any help is appreciated...Thanks


I will post the 2 pieces of code that i'm working with and post the whole program below....



Here is my code:

  1. void createList(nodeType*& first, nodeType*& last);
  2. void printList(nodeType*& first);
  3. int listCount();
  4. bool emptyList();
  5. void insertBack(nodeType*& last);
  6. void deleteFront(nodeType*& first);
  7.  
  8. int main()
  9. {
  10.  
  11. nodeType *first, *last;
  12. int num;
  13. int emptyList;
  14. int count = 0;
  15.  
  16. createList(first,last);
  17. printList(first);
  18.  
  19. if (emptyList(first))
  20. cout<<"Empty list"<<endl;
  21. else
  22. cout<<"There are"<<listCount(first)<<" items in the linked list"<<endl;





  1. int listCount()
  2. {
  3.  
  4. return count;
  5.  
  6. }
  7.  
  8. bool emptyList()
  9. {
  10.  
  11.  
  12. }




THIS IS THE FULL CODE:



  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodeType
  5. {
  6. int info;
  7. nodeType *link;
  8. };
  9.  
  10. void createList(nodeType*& first, nodeType*& last);
  11. void printList(nodeType*& first);
  12. int listCount();
  13. bool emptyList();
  14. void insertBack(nodeType*& last);
  15. void deleteFront(nodeType*& first);
  16.  
  17. int main()
  18. {
  19.  
  20. nodeType *first, *last;
  21. int num;
  22. int emptyList;
  23. int count = 0;
  24.  
  25. createList(first,last);
  26. printList(first);
  27.  
  28. if (emptyList(first))
  29. cout<<"Empty list"<<endl;
  30. else
  31. cout<<"There are"<<listCount(first)<<" items in the linked list"<<endl;
  32.  
  33.  
  34. insertBack(last);
  35. printList(first);
  36.  
  37. deleteFront(first);
  38. printList(first);
  39.  
  40. system("PAUSE");
  41. return 0;
  42. }
  43.  
  44. void createList(nodeType*& first, nodeType*& last)
  45. {
  46. int number;
  47. nodeType *newNode;
  48.  
  49. first = NULL;
  50. last = NULL;
  51.  
  52. cout<<"Enter an integer (-999 to stop): ";
  53. cin>>number;
  54. cout<<endl;
  55.  
  56. while (number != -999)
  57. {
  58. newNode = new nodeType; // create new node
  59. newNode->info = number;
  60. newNode->link = NULL;
  61.  
  62. if (first == NULL)
  63. {
  64. first = newNode;
  65. last = newNode;
  66. }
  67. else
  68. {
  69. last->link = newNode;
  70. last = newNode;
  71. }
  72. cout<<"Enter an integer (-999 to stop): ";
  73. cin>>number;
  74. cout<<endl;
  75. }
  76.  
  77. }
  78.  
  79. void printList(nodeType*& first)
  80. {
  81.  
  82. cout<<"Inside printList...printing linked list...\n"<<endl;
  83. nodeType *current;
  84. current = new nodeType;
  85. current = first;
  86. while (current != NULL)
  87. {
  88. cout << current->info<<endl;
  89. current = current->link;
  90. count++;
  91. }
  92. }
  93.  
  94. void insertBack(nodeType*& last)
  95. {
  96.  
  97. int num;
  98. nodeType *first,*newNode;
  99.  
  100.  
  101. cout<<"There are 3 items in the linked list."<<endl;
  102. cout<<"Enter a number to add to the END of the list: "<<endl;
  103. cin>>num;
  104.  
  105.  
  106. newNode = new nodeType;
  107. newNode->info = num;
  108. newNode->link = NULL;
  109.  
  110. if (first == NULL)
  111. {
  112. first = newNode;
  113. last = newNode;
  114.  
  115. }
  116. else
  117. {
  118. last->link = newNode;
  119. last = newNode;
  120. }
  121.  
  122. }
  123.  
  124. void deleteFront(nodeType*& first)
  125. {
  126.  
  127. nodeType *last,*current,*trailcurrent;
  128. bool found;
  129.  
  130.  
  131. cout<<endl;
  132. cout<<"Inside deleteFront...removing item from front of list..."<<endl;
  133. cout<<endl;
  134.  
  135. if (first->info == 1)
  136. {
  137. current = first;
  138. first = first->link;
  139. }
  140. if (first == NULL)
  141. {
  142. last = NULL;
  143.  
  144. delete current;
  145. }
  146. else
  147. {
  148. found = false;
  149. trailcurrent = first;
  150.  
  151. current = first->link;
  152. }
  153.  
  154.  
  155.  
  156. }
  157.  
  158. int listCount()
  159. {
  160.  
  161. return count;
  162.  
  163. }
  164.  
  165. bool emptyList()
  166. {
  167.  
  168.  
  169. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Linked Lists: Find length and check for empty list

 
0
  #2
Nov 11th, 2008
Well, you obvioulsy need to increment counter each time you add a node, and decrement it each time you remove a node. It's really not hard, just put it in insert, delete, and create list.

For checking if list is empty... simply check if first==NULL.

But don't you need some wrapper class around everything? This way it's meaningless to write empty() function
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Linked Lists: Find length and check for empty list

 
0
  #3
Nov 11th, 2008
If unlucky user accidentally types a wrong char your program comes to loop forever because subsequent cin inputs do nothing until cin.clear() call.
Always check input result, for example:
  1. if (cin >> number) {
  2. // that's ok
  3. } else {
  4. // eof or not-a-number
  5. // Recovery? It's the other story...
  6. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
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