| | |
Linked List Help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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:
My header file:

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)
#include<iostream> #include "linkedList.h" //so all functions can use LinkedList class LinkedList list; void enterNumbers(); void displayList(); void searchList(double z); void deleteNode(double z); // start main int main() { // declare variables int statusChoice = 0; int menuChoice = 0; double searchValue = 0.0; // output user information cout << "This program uses a linked list. The user has the options to \n"; cout << "add data to the list, delete data from the list, or display \n"; cout << "the contents of the list.\n\n\n"; // User input to start or stop the program cout << "Linked list operations.\n"; cout << "Enter 1 to start or 2 to stop.\n" << endl; cin >> statusChoice; while (statusChoice == 1) { //User needs to choose option of what to do cout << "Choose an option below" << endl; cout << "\t1) Add Data" << endl; cout << "\t2) Delete Data" << endl; cout << "\t3) Display Data" << endl; cout << "\t4) Search" << endl; cout << "\t5) Exit" << endl; cin >> menuChoice; switch(menuChoice) { case 1: enterNumbers(); break; case 2: deleteNode(searchValue); break; case 3: displayList(); break; case 4: searchList(searchValue); break; case 5: statusChoice = 2; } } // output termination message cout << "\n\n\nProgram has terminated successfully!\n\n\n\n"; return 0; } void enterNumbers() { double number = 0.0; // Get input from user and add them to list cout << "\nYou now need to enter numbers into the list."; // enter numbers into the list - entering -1 will escape out of loop do { cout << "\nEnter a numerical value <Enter -1 to stop>\n"; cin >> number; list.add(number); }while(number != -1); } void displayList() { // Print the list cout << "\n\nThe numbers you entered into the list will be displayed.\n"; cout << "\nFollowing are the numbers entered: " << endl; list.print(); cout << endl; } void searchList(double z) { cout << "Enter number to find in the list" << endl; cin >> z; list.isMember(z); } void deleteNode(double z) { cout << "Enter number to delete from the list" << endl; cin >> z; list.deleteNode(z); }
My header file:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; // declare class ListNode class ListNode { public: ListNode(double v, ListNode *p) { value = v; next = p; } private: double value; ListNode *next; friend class LinkedList; // LinkedList has friend status }; // declare class LinkedList class LinkedList { public: void add(double x); bool isMember(double x); void deleteNode(double x); LinkedList( ) { head = NULL;} void print(); private: ListNode * head; static void rPrint(ListNode *pList); }; // define member print. void LinkedList::print() { ListNode *p = head; // Use to walk through list while (p != NULL) { cout << p->value << " "; p = p->next; } } // define member add void LinkedList::add(double x) { // do not accept sentinel as data to be added to list // if sentinel is passed to function member - does not add to list // and escapes loop in listPrint.cpp if (x != -1) { head = new ListNode(x, head); } else return; } // define membber isMember bool LinkedList::isMember(double x) { int position = 0; ListNode *p = head; // Use p to walk through list while (p) { if (p->value == x) { cout << "Value was found in the list in position " << position << ".\n\n"; return true; } else p = p->next; position ++; } // List is exhausted without finding x return false; } void LinkedList::deleteNode(double z) { ListNode *nodePtr, *previousNodePtr; // If the list is empty, do nothing if (!head) return; // Determine if the first node is the one to delete if (head->value == z) { nodePtr = head; head = head->next; delete nodePtr; } else { //Initialize nodePtr to head of list nodePtr = head; //skip all nodes whose value member is not equal to z while (nodePtr != NULL && nodePtr->value != z) { previousNodePtr = nodePtr; nodePtr = nodePtr->next; } //link the previous node to the node after nodePtr, then delete nodePtr if (nodePtr) { previousNodePtr->next = nodePtr->next; delete nodePtr; } } }
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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.
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.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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:
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.
(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)
// define member add void LinkedList::add(double x) { // do not accept sentinel as data to be added to list // if sentinel is passed to function member - does not add to list // and escapes loop in listPrint.cpp if (x != -1) { if (head == NULL) { head = new ListNode(x, head); } else { ListNode *nodePtr = head; while (nodePtr->next != NULL) { nodePtr = nodePtr->next; } nodePtr->next = new ListNode(x, head); } } else return; }
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.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Input Validation for an array
- Next Thread: Need help with soduku code!
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





