Using find method in LinkedList

I'm new to c++. I was studying data structures and came to know Linked Lists Lately.
We had a problem of finding an item stored inside a linked list and here's the code I wrote using Visual Studio to store and find an item. This code gives me a run time error.

1. "Link.h" file

1
2
3
4
5
6
7
8
9
10
11
#pragma once
 
class Link
{
public:
	double num;
	char nam[20];
	Link* next;
	Link(double pnum,char pnam[]);
	void displayLink();
};



2. "Link.cpp" file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include "StdAfx.h"
#include "Link.h"
#include<iostream>
#include<cstring>
using namespace std;
 
Link::Link(double pnum,char pnam[])
{
	next = NULL;
	num = pnum;
	strcpy(nam,pnam);
}
 
void Link::displayLink()
{
	cout<<num<<"\t"<<nam<<endl;
}


3. "LinkList.h" file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#pragma once
#include"Link.h"
 
class LinkList
{
private:
	Link *first;
 
public:
	LinkList();
	void insertFirst(double n,char nm[]);
	Link* find(double n);
};


4. "Linklist.h" file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include "StdAfx.h"
#include "LinkList.h"
#include"Link.h"
#include<iostream>
using namespace std;
 
LinkList::LinkList()
{
	first = NULL;
}
 
void LinkList::insertFirst(double n,char nm[])
{
	Link *temp = new Link(n,nm);
	temp->next = first;
	first = temp;
	cout<<"DATA ENTERED"<<endl;
}
 
Link* LinkList::find(double n)
{
	Link *cur = first;
	while(cur!=NULL)
	{
		if(cur->num == n)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
 
}


5."LinkList_5.cpp" (main program)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include "stdafx.h"
#include<iostream>
#include"Link.h"
#include"LinkList.h"
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
	LinkList *ne = new LinkList();
	ne->insertFirst(12.22,"Charith");
	ne->insertFirst(13.22,"Lalitha");
	cout<<ne->find(12.22)->num;
 
	system("PAUSE");
	return 0;
}


I've doubt about the way I accessed items that're returned by 'find' method. Can somebody please tell me a way of getting out values returned in that particular method inside the LinkList_5.cpp.
Can somebody please tell me a way of getting out values returned in that particular method inside the LinkList_5.cpp

But i guess you already have the value . as
1
2
if( ne->find(12.22) == NULL)
double number = ne->find(12.22)->num;
do you mean:

[code]
cout<<ne->find(12.22)->num;
cout<<ne->find(12.22)->nam;
[/code

or

[code]
Link * p = ne->find(12.22);
if (p)
{
cout<<p->num;
cout<<p->nam;
}
[/code

The 2nd block of code is better since it does not call find twice - we call find once- store it's result in pointer p and then print out the elements of p by dereferencing it.

Is this what you wanted to know?
What is the run time error? What have you done to debug the program? If you are using visual studio it should be trivial to step through the code with the debugger. Set a breakpoint and then use the F5 key to run in debug mode.

You should check the return value to see if it is NULL since that is a possibility. It seems like it should work, except that you are using a double. They don't always compare equal even when you think they should. Debug and check whether NULL is being returned.

Take a look at this.
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17
Last edited on
Topic archived. No new replies allowed.