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();
};
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.
[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.
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.