# Thread: Why can't my link list find integers 7 and 8??

1. ## Why can't my link list find integers 7 and 8??

Code:
```#include <iostream>
#include <vector>
//#include <list>
using namespace std;

struct Node
{
int data;

Node *next;

Node(int d, Node *n) : data(d), next(n)
{}
};

class List
{
public:

List()
{
head = new Node(int(), NULL);
//cout << "int() is: " << int() << endl;

}

void Add(int dataX, Node &n1)
{

Node *it = head;

/*if(it->next != NULL)
{
head = new Node(dataX, it->next);

head->data = dataX;
//cout << "head->data is: " << head->data << endl;
head->next = NULL;

tail = new Node(dataX, it->next);

tail->data = dataX;
//cout << "tail->data is: " << tail->data << endl;
tail->next = NULL;
return;
}*/

if(it->next == 0)
{
// can't find 7 or 8 due to a problem in here.
n1.data = it->data;
//n1.next = it->next;
cout << "yes, this is definately working" << endl;
cout << n1.data << endl;
}

while(it->next != 0)// && it->next->data < dataX)
{
int ggg = 0;
//cout << ++ggg << "this number" << endl;

n1.data = it->data;
n1.next = it->next;
//	cout << n1 << " and " << n1.data << endl;
it = it->next;
//cout << "hello world" << endl;
cout << it->data << endl;

//it->next = new Node(dataX, it->next);

}

//cout << "it->data is: " << it->data << endl;

//if(it->next == NULL)
//n1->data = dataX;
//n1 = it->next;
it->next = new Node(dataX, it->next);

//head = it;

}

bool Count(int y)
{
//cout << vTail[-1] << endl;
return 1;

}

void RemoveAll(int z)
{
Node *it = head;
//cout << "head->next is right now: " << head->data << endl;
//cout << "it->next is right now: " << it->data << endl;

//while(it->next == NULL) // && it->next->data < z)
//{
//cout << "hola" << endl;
it = it->next;

//	cout << "hola " << it->next << endl;
//}
//if(head->next != 0)
//{
//Node *it = head->next;
//	Node *hold = it->next;
//	it->next = hold->next;

//	delete hold;

//	while(it != 0)
//	{
//cout << "now it's: " << it->data << "->" << endl;

//		it = it->next;
//	}

//}

if(it->next != NULL)
{
cout << "yes this works" << endl;
Node *hold = it->next;
it->next = hold->next;
//cout << "hold->next is: don't expect to print->: " << hold->next << endl;

delete hold;

//cout << "hold->next is now: " << hold->data << endl;

//cout << "while remove, it->data is: " << it->data << endl;
}

//cout << "after remove, it->data is: " << it->data << endl;

}

int Contains(int b)
{
return 1;
}

void display()
{
//cout << "head->next in walk is: " << head->data << endl;
if(head->next != 0)
{
Node *it = head->next;

while(it != 0)
{
cout << it->data << "->";

it = it->next;
//cout << it->data << "->";
}

}

}

private:
Node *head;
Node *tail;

//List *pNext;

vector<int> vHead;
vector<int> vTail;

};

Node *find_entry(Node *lptr, int match)
{
while(lptr != 0)
if(lptr->data == match)
{
cout << "match is: " << match << endl;
return(lptr);
}
else
//cout << "lptr->data is: " << lptr->data << endl;
lptr = lptr->next;

return (0);

}

int main()
{
List list;

Node *find_entry(Node *lptr, int match);
//Node n1, n2, n3;
Node *lptr, *list_start;
//Node n1(NULL, NULL) = &list_start;
int i, search;

//cout << list.Count(9) << endl; //this should print 0

list.Add(7, *list_start);
list.Add(8, *list_start);
list.Add(9, *list_start);
list.Add(10, *list_start);
list.Add(11, *list_start);

cout << "list start is: " << list_start->data << endl;
list.display();

cout << "Enter a value to locate: ";
cin >> search;

lptr = find_entry(list_start, search);
//cout << "this is important: " << lptr->data << endl;

if(lptr != 0)
cout << "found " << lptr->data << "." << endl;
else
cout << "Not found" << endl;

list.RemoveAll(7);
list.RemoveAll(10);
list.RemoveAll(9);
list.display();

//list.Add(7)->head;

//lptr = list.FindEntry(list_start, match);

/*if(lptr != 0)
cout << "Found: " << lptr->data << endl;
else
cout << "Not found!" << endl;*/

//cout << list.Add() << endl;

/*if(list.Contains(12))
cout << "Found 12" << endl; //this should print
else
cout << "Error" << endl;

if(!list.Contains(15))
cout << "Did not find 15" << endl; //this should print
else
cout << "Error" << endl;*/

//for(int dataX = 0; dataX < 10; dataX++)
//	list.Add(dataX);

//cout << list.Count(7) << endl; //should print 2, since there are 2 7's in the list

list.RemoveAll(0);

//cout << list.Count(7) << endl; //should print 0

cout << endl;

return 0;
}```
in my link list, I'm inserting the integers 7, 8, 9, 10, and 11.

I have a search function: "find_entry" under class list.

This function can find the 9, 10, and 11 that I inserted into my list, but it can't find 7 or 8.

The problem lies within class List, member function Add, somewhere inside the first if statement.

I don't know why n1.data isn't getting assigned to it->data correctly.

Could someone please point me in the right direction?

Thanks

2. Code:
```Node *lptr, *list_start;

list.Add(7, *list_start);```
You have a pointer to head already stored in the object list, so why is list_start?

You can use memberfunction Add() just like this:

list.Add (7);

Since the object itself stores head and tail, you don't need to do that in main().

BTW, list_start is a pointer to Node, it is not an object of type Node. So you cannot store data in it. To use it, you would first have to allocate memory for it.

Popular pages Recent additions