#include <iostream>
usingnamespace std;
constshortunsignedint NUMVALS = 10;
class linklist
{
private:
struct ListNode
{
int value;
ListNode* next;
staticvoid recursiveDelete(ListNode *node);
};
ListNode *head;
public:
linklist(void) {head = NULL;}
~linklist() {
if( head != NULL )
ListNode::recursiveDelete(head);
}
void getHead() {return ListNode* head;}
bool insertNode(int num);
void displayList(void);
bool apendinglist (int num);
bool deleteNode(int num);
int indexOf (int value);
};
int main(void)
{
linklist mylist;
int dumVals[NUMVALS];
for (int i = 0; i<NUMVALS; i++)
{
cout<<"please enter your "<<i+1<<" number in the list data ";
cin>>dumVals[i];
}
shortunsignedint i;
for(i=0; i<NUMVALS; i++) { // insert the array values into the linked list
if(mylist.insertNode(dumVals[i])) {
cout << "Error in function insertNode!\n";
cout << "Program will halt!\n";
exit(1);
}
}
mylist.displayList();
int dummy2;
cout<<"please enter the value that you wish to search";
cin>>dummy2;
mylist.indexOf (dummy2);
}
bool linklist::insertNode(int num)
// This function inserts a node into our linked list.
{
ListNode* newNode;
ListNode* nodePtr = head;
ListNode* prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member!\n";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) { // bad programming practice but i don't care =D
cout<<"your list is ";
head = newNode;
}
else {
while(nodePtr != NULL && nodePtr->value<num) {
prevNodePtr = nodePtr; // advance prevNodePtr up to nodePtr
nodePtr = nodePtr->next; // advance nodePtr by 1 node
}
if(prevNodePtr == NULL) {// at beginning of list
newNode->next = head;
head = newNode;
}
else {
prevNodePtr->next = newNode;
newNode->next = nodePtr;
}
}
return 0;
}
void linklist::displayList(void)
// This function displays our linked list!
{
ListNode* nodePtr = head;
if(head == NULL) {
cout << "List is currently empty!\n";
}
else {
while (nodePtr != NULL) {
cout << nodePtr->value <<"\t";
nodePtr = nodePtr->next;
}
}
}
bool linklist::deleteNode(int num)
// This function deletes a node, if it is in face there.
{
ListNode* nodePtr = head;
ListNode* prevNodePtr = NULL;
if(head==NULL) { // list is currently empty
cout << "The list is currently empty - ";
cout << "There are no nodes to delete!\n";
}
elseif(nodePtr->value == num) { // node is first one
head = nodePtr->next;
delete nodePtr;
}
else {
while(nodePtr != NULL && nodePtr->value != num) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr == NULL) {
cout << "Your value (i.e., " << num << ")";
cout << " was not in the list!\n";
}
else {
prevNodePtr->next = nodePtr->next;
delete nodePtr;
}
}
return 0;
}
bool linklist::apendinglist(int num)
// This function inserts a node into our linked list.
{
ListNode* newNode;
ListNode* nodePtr = head;
ListNode* prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member!\n";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) { // check if list is currently empty
cout << "List is currently empty - " << newNode->value;
cout << " is part of the list's first node!\n";
head = newNode;
}
else
{
while(nodePtr->next != NULL)
nodePtr=nodePtr->next;
nodePtr->next = newNode;
}
return 0;
}
void linklist::ListNode::recursiveDelete(ListNode *node)
{
if( node->next != NULL )
recursiveDelete(node->next);
delete node;
}
int linklist::indexOf (int value)
{
ListNode* itr = mylist.getHead(); // you will need to create this function.
for (int i = 1; itr != NULL; i++, itr = itr->next) {
if (itr->value == value)
return i; // index of the value you're searching for.
cout<<i;
}
return -1; // not found.
}
As you see i am letting the user type 10 ints and store it in a link list, i then display the linklist in ascending order (and so far my program works). NOw i want to modify my program to let the user promt an int, and if that int belongs to that link list then it will display the position of that number, starting head = 0.
E.g user input 1 2 3 4 5 6 7 8 9 10 as the initial ten value, then it will ask the user to "please enter the value that you wish to search" the user type in 2, the it will display 2 is in position 2(position 1 for comptuer code but i will add 1 to it).
Now i got help from other posts and here's my problems
on line 25, my getHead function is invalid
line 188 mylist is invalid (although i declare it in main).
Your getHead function returns void; that is, no return. Yet you then try to return something.
Note that when you return an object, it is unusual to declare its type as well.
That is,
1 2 3 4 5
return i; // good
returnint i; // a bit odd
return head; //good
return ListNode* head; //a bit odd
line 188; you are calling a member function of your mylist object. It can see the getHead function because that is another member function within that object.
Try
ListNode* itr = getHead(); // you will need to create this function.
Hi, your suggestions
return i; // good
return int i; // a bit odd
return head; //good
return ListNode* head; //a bit odd
i tried it before, and it doesn't work, there's a red line under it, is it because i said it's void?
note that i am talking about line 25,
and what is int i? i don't think i declare it.
In short what is the correct getHead declaration? for line 25
returnint i was a simple example to show you something that you shouldn't do. return ListNode* head; is a slightly more complicated example to show you something you shouldn't do.
You should replace the void on line 25 with the kind of object you are returning. Look at line 25, work out what kind of object you are returning (i.e. what kind of object head is), and put that where void is.
haha, that is what i'm asking i know it is not "void" i tried int, float, double, bla bla bla but the problem is the return statment, no matter what i return it'still underlind with red.
ListNode* getHead() {return head;} = Bingo!
For line 188 try ListNode* itr = getHead();
Or try ListNode* itr = head; because private class members are accessible in the class member functions (ie. you didn't need getHead() after all).
HOLD ON A SEC something is really worng here, fun2code you did solve my problem but my code is not doing what i want, for the data i type 1 2 3 4 5 6 7 8 9 0
well so the code will display 0 1 2 3 4 5 6 7 8 9
then the code will ask you "please enter the value you wish to search" then i type 4
then it display 1234 )'=
Yes, the list will display "backwards". That's how it's supposed to work.
Remember, the 1st value entered becomes the last value in the list while the last value entered becomes the head (or 1st) value in the list.
then it display 1234
That's what line 192 will do.
If you want to see only the index where the value was found then simply display the functions return value. Replace line 54 with: cout << mylist.indexOf(dummy2); and remove line 192.