943,654 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4104
  • C++ RSS
Nov 11th, 2008
0

Linked Lists: Find length and check for empty list

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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;





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




THIS IS THE FULL CODE:



C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 11th, 2008
0

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

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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 11th, 2008
0

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

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:
C++ Syntax (Toggle Plain Text)
  1. if (cin >> number) {
  2. // that's ok
  3. } else {
  4. // eof or not-a-number
  5. // Recovery? It's the other story...
  6. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Letters apparition problem
Next Thread in C++ Forum Timeline: Homework Function Undefined error





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


Follow us on Twitter


© 2011 DaniWeb® LLC