I changed up the code for the retrieve function , and it works like a charm !!!!
OH boy this was a challenging question. 2 more to go.
the new code is
Code:
void List::retrieve(node *pPointer, int XNumber) //performs retrieve on a sorted list
{
node *tmp = pPointer;
int position=0;
while (tmp != NULL && tmp->number != XNumber)
{
tmp=tmp->next;
position++;
}
if (tmp == NULL )
{
cout << XNumber << " is not in the list " << endl;
}
else if (tmp->number = XNumber )
{
cout << XNumber << " is located in position " << position << endl;
}
else
{
cout << " this is 0 " << endl;
}
For the arguments of pPointer. My assignment has me bring in a node and a number as a parameter. Im not understanding how to bring in TheHead directly into the insert and retrieve functions. If i try , it says that i cannot and
illegal reference to non-static member 'List::TheHead displays.
i got changed up the parameters in the main.
but i am not sure what u mean by , destroying the list, do u mean using the delete command?
here is the full code after the mods i did. pPointer doesnt like me changing it at all.
Code:
#include <iostream>
using namespace std;
struct node
{
int number;
node *next;
};
class List
{
public:
List();
~List();
node *Head();
node *createnode(int);
void insert(node *, int);
void traverse(node *);
void retrieve(node *, int);
private:
node *TheHead;
};
List::List()
{
TheHead = NULL;
}
node *List::Head()
{
return TheHead;
}
List::~List() {} //Destructor
node *List::createnode(int NumberToInsert)
{
node *NewOne = new node;
NewOne ->number = NumberToInsert;
NewOne ->next = NULL;
return NewOne;
}
void List::insert(node *pPointer, int ANumber)
{
node *tmp = new node;
tmp = createnode(ANumber);
tmp -> next = pPointer;
TheHead = tmp;
}
void List ::traverse(node *CurPtr)
{
node *current = CurPtr;
while (current !=NULL)
{
cout << current ->number << " ";
current = current ->next;
}
cout << "\n";
}
void List::retrieve(node *pPointer, int XNumber) //performs retrieve on a sorted list
{
node *tmp = pPointer;
int position=0;
while (tmp != NULL && tmp->number != XNumber)
{
tmp=tmp->next;
position++;
}
if (tmp == NULL )
{
cout << XNumber << " is not in the list " << endl;
}
else if (tmp->number = XNumber )
{
cout << XNumber << " is located in position " << position << endl;
}
else
{
cout << " this is 0 " << endl;
}
}
int main()
{
List s;
s.Head();
int TheNumber=-1;
int RetrievedNumber = -1;
while (TheNumber != 0)
{
cout << "Please enter in a number: enter 0 to quit: " ;
cin >> TheNumber;
s.insert(s.Head() , TheNumber);
}
s.traverse(s.Head());
while (RetrievedNumber !=0)
{
cout << "Please enter the number to be retrieved , 0 to quit: " ;
cin >> RetrievedNumber ;
s.retrieve(s.Head(), RetrievedNumber);
}
system("pause");
return 0;
}