943,740 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 843
  • C++ RSS
Dec 7th, 2008
0

Linked List Help

Expand Post »
To any and everyone
As stated in my title, this is a homework assignment, so I appreciate any help or suggestions that are given. Also, per the website rules, I have put effort to this, and I'm not asking anyone to just do my work.

(The program compiles and works as-is, but the output for the search function is not correct.)

OK...here is the problem. The isMember member function is working, but is not returning the correct location. For instance, when initially running the program and entering the numbers 10, 20, and 30 (in that order) into the list, and then doing the search on the number 30 -- it will return that it found the number in location 0.

Can someone help me get this fixed, so it would give location 3 for the search results? Thanks in advance. Jason

p.s. I apologize if my program does not follow "best practices." In fact, I'm very open to "real world" constructive criticism as to where I can improve. With that being said, I do know that this program is lacking in error checking.

My source file:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2.  
  3. #include "linkedList.h"
  4.  
  5. //so all functions can use LinkedList class
  6. LinkedList list;
  7.  
  8. void enterNumbers();
  9. void displayList();
  10. void searchList(double z);
  11. void deleteNode(double z);
  12.  
  13. // start main
  14. int main()
  15. {
  16. // declare variables
  17. int statusChoice = 0;
  18. int menuChoice = 0;
  19. double searchValue = 0.0;
  20.  
  21.  
  22. // output user information
  23. cout << "This program uses a linked list. The user has the options to \n";
  24. cout << "add data to the list, delete data from the list, or display \n";
  25. cout << "the contents of the list.\n\n\n";
  26.  
  27. // User input to start or stop the program
  28. cout << "Linked list operations.\n";
  29. cout << "Enter 1 to start or 2 to stop.\n" << endl;
  30. cin >> statusChoice;
  31. while (statusChoice == 1)
  32. {
  33. //User needs to choose option of what to do
  34. cout << "Choose an option below" << endl;
  35. cout << "\t1) Add Data" << endl;
  36. cout << "\t2) Delete Data" << endl;
  37. cout << "\t3) Display Data" << endl;
  38. cout << "\t4) Search" << endl;
  39. cout << "\t5) Exit" << endl;
  40. cin >> menuChoice;
  41.  
  42. switch(menuChoice)
  43. {
  44. case 1: enterNumbers();
  45. break;
  46. case 2: deleteNode(searchValue);
  47. break;
  48. case 3: displayList();
  49. break;
  50. case 4: searchList(searchValue);
  51. break;
  52. case 5: statusChoice = 2;
  53. }
  54. }
  55.  
  56. // output termination message
  57. cout << "\n\n\nProgram has terminated successfully!\n\n\n\n";
  58. return 0;
  59. }
  60.  
  61. void enterNumbers()
  62. {
  63. double number = 0.0;
  64.  
  65. // Get input from user and add them to list
  66. cout << "\nYou now need to enter numbers into the list.";
  67.  
  68. // enter numbers into the list - entering -1 will escape out of loop
  69. do
  70. {
  71. cout << "\nEnter a numerical value <Enter -1 to stop>\n";
  72. cin >> number;
  73. list.add(number);
  74. }while(number != -1);
  75.  
  76. }
  77.  
  78. void displayList()
  79. {
  80. // Print the list
  81. cout << "\n\nThe numbers you entered into the list will be displayed.\n";
  82. cout << "\nFollowing are the numbers entered: " << endl;
  83.  
  84. list.print();
  85. cout << endl;
  86. }
  87.  
  88. void searchList(double z)
  89. {
  90. cout << "Enter number to find in the list" << endl;
  91. cin >> z;
  92. list.isMember(z);
  93. }
  94.  
  95. void deleteNode(double z)
  96. {
  97. cout << "Enter number to delete from the list" << endl;
  98. cin >> z;
  99. list.deleteNode(z);
  100. }

My header file:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // declare class ListNode
  5. class ListNode
  6. {
  7. public:
  8. ListNode(double v, ListNode *p)
  9. {
  10. value = v; next = p;
  11. }
  12. private:
  13. double value;
  14. ListNode *next;
  15. friend class LinkedList; // LinkedList has friend status
  16. };
  17.  
  18. // declare class LinkedList
  19. class LinkedList
  20. {
  21. public:
  22. void add(double x);
  23. bool isMember(double x);
  24. void deleteNode(double x);
  25. LinkedList( ) { head = NULL;}
  26. void print();
  27. private:
  28. ListNode * head;
  29. static void rPrint(ListNode *pList);
  30. };
  31.  
  32. // define member print.
  33. void LinkedList::print()
  34. {
  35. ListNode *p = head; // Use to walk through list
  36. while (p != NULL)
  37. {
  38. cout << p->value << " ";
  39. p = p->next;
  40. }
  41. }
  42.  
  43. // define member add
  44. void LinkedList::add(double x)
  45. {
  46. // do not accept sentinel as data to be added to list
  47. // if sentinel is passed to function member - does not add to list
  48. // and escapes loop in listPrint.cpp
  49. if (x != -1)
  50. {
  51. head = new ListNode(x, head);
  52.  
  53. }
  54. else
  55. return;
  56. }
  57.  
  58.  
  59.  
  60.  
  61. // define membber isMember
  62. bool LinkedList::isMember(double x)
  63. {
  64. int position = 0;
  65. ListNode *p = head; // Use p to walk through list
  66. while (p)
  67. {
  68. if (p->value == x)
  69. {
  70. cout << "Value was found in the list in position " << position << ".\n\n";
  71. return true;
  72. }
  73. else
  74. p = p->next;
  75. position ++;
  76. }
  77. // List is exhausted without finding x
  78. return false;
  79. }
  80.  
  81. void LinkedList::deleteNode(double z)
  82. {
  83. ListNode *nodePtr, *previousNodePtr;
  84.  
  85. // If the list is empty, do nothing
  86. if (!head)
  87. return;
  88. // Determine if the first node is the one to delete
  89. if (head->value == z)
  90. {
  91. nodePtr = head;
  92. head = head->next;
  93. delete nodePtr;
  94. }
  95.  
  96. else
  97.  
  98. {
  99. //Initialize nodePtr to head of list
  100. nodePtr = head;
  101.  
  102. //skip all nodes whose value member is not equal to z
  103. while (nodePtr != NULL && nodePtr->value != z)
  104. {
  105. previousNodePtr = nodePtr;
  106. nodePtr = nodePtr->next;
  107. }
  108.  
  109. //link the previous node to the node after nodePtr, then delete nodePtr
  110. if (nodePtr)
  111. {
  112. previousNodePtr->next = nodePtr->next;
  113. delete nodePtr;
  114. }
  115. }
  116. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JLopeman is offline Offline
19 posts
since Oct 2008
Dec 8th, 2008
0

Re: Linked List Help

Since you are adding nodes to the head (start) of the list, the last node you add will be in position 0. The output is correct for the example you gave. I'm assuming the list isn't supposed to be sorted - if so then you need to change your add function.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 8th, 2008
0

Re: Linked List Help

mahler...

No, the list does not need to be sorted.

I was looking at the STL-- "list.reverse" -- but I'm not for sure I understand the syntax on how to use that. I'll see what google tells me.

I was also thinking of a recursive function to swap the nodes around, but I think I've been staring at this for too long today.

My apologies if I posted too much code. I'm a nOOb both here and in the C++ world.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JLopeman is offline Offline
19 posts
since Oct 2008
Dec 8th, 2008
0

Re: Linked List Help

AH....Just re -read your message Mahler...

Do you think appending each value into the list would be a better, albeit dirty, way to fix this issue?

Thanks

Jason
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JLopeman is offline Offline
19 posts
since Oct 2008
Dec 8th, 2008
0

Re: Linked List Help

What exactly are the requirements? Are you supposed to be adding new nodes to the beginning of the list or the end of the list? Are you sure you are not supposed to get the result you got?

In the case that you are supposed to append to the END of the list instead of the beginning as you have done, then you will need to loop through the entire list and put the new node at the end when adding a node. This is clearly inefficient, but if the requirements state that you should be adding to the end of the list then that's what you have to do.

If the requirements say that you should be getting '2' as the output for your example, but specifies that you should be adding new nodes to the start of the list, then that's messed up.... The only real way to fix that case is if you keep track of the length of the list as you insert/remove nodes and then subtract the actual found location from the length of the list (and then -1 since it is 0 indexed). So in your example you would do 3 - 0 - 1 = 2.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 8th, 2008
0

Re: Linked List Help

I didn't say this wasn't messed up (Don't get me started on my prof.)

The example output that was shown has the numbers 10, 20, and 30 entered into the list, and when doing the search on the number 30 it is to return that the number was found in location number 3.

I tried this:
C++ Syntax (Toggle Plain Text)
  1. // define member add
  2. void LinkedList::add(double x)
  3. {
  4. // do not accept sentinel as data to be added to list
  5. // if sentinel is passed to function member - does not add to list
  6. // and escapes loop in listPrint.cpp
  7. if (x != -1)
  8. {
  9. if (head == NULL)
  10. {
  11. head = new ListNode(x, head);
  12. }
  13. else
  14. {
  15. ListNode *nodePtr = head;
  16. while (nodePtr->next != NULL)
  17. {
  18. nodePtr = nodePtr->next;
  19. }
  20. nodePtr->next = new ListNode(x, head);
  21. }
  22. }
  23. else
  24. return;
  25. }

It works for the first two numbers you enter into the list, but crashes when trying to enter in the 3rd number. I tried it with entering just two numbers -- 10 and 20 (in that order)-- and did a search on 20 and returned location 1, which would be correct. However, when trying to display the numbers, it infinitely fills the screen with 10's and 20's. grrrrrr

I've attached the example output we are supposed to be following.
Attached Files
File Type: doc Linked_List_output.doc (79.0 KB, 21 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JLopeman is offline Offline
19 posts
since Oct 2008
Dec 8th, 2008
0

Re: Linked List Help

When adding the node at the end (as you are now doing), you should have the new node point to NULL instead of head. The reason you get it infinitely repeating is because when your end node points to the head (the start) it is basically a giant loop.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 8th, 2008
0

Re: Linked List Help

Awesome!

Thanks for the help!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JLopeman is offline Offline
19 posts
since Oct 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: Input Validation for an array
Next Thread in C++ Forum Timeline: Need help with soduku code!





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


Follow us on Twitter


© 2011 DaniWeb® LLC