473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

searching a linked list

I have this function that searches a link list before main performs
the insert function to insert an item. My problem is the pointer stuff
and the program keeps crashing.
<code>
List::List()
{
listPtr = NULL;

}

bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
while(loopPtr != NULL) // but listPtr is NULL- is this the
problem
{
if(loopPtr->item == Item)
return true;
else
{
loopPtr = loopPtr->next;

}
return false;
}
}
// this is the declaration in the spec file
private:
NodeType* listPtr;
int length;

</code>

Can anyone help?

Apr 9 '07 #1
8 1839
* zf*****@mail.com:
I have this function that searches a link list before main performs
the insert function to insert an item. My problem is the pointer stuff
and the program keeps crashing.
<code>
List::List()
{
listPtr = NULL;

}

bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
while(loopPtr != NULL) // but listPtr is NULL- is this the
problem
{
if(loopPtr->item == Item)
return true;
else
{
loopPtr = loopPtr->next;

}
return false;
}
}
// this is the declaration in the spec file
private:
NodeType* listPtr;
int length;

</code>

Can anyone help?
The error's on line 162. In addition you forgot to specify a return
value in your IsThere function, for the case of listPtr == 0.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 9 '07 #2
On Apr 9, 2:56 am, "Alf P. Steinbach" <a...@start.nowrote:
* zfar...@mail.com:


I have this function that searches a link list before main performs
the insert function to insert an item. My problem is the pointer stuff
and the program keeps crashing.
<code>
List::List()
{
listPtr = NULL;
}
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
while(loopPtr != NULL) // but listPtr is NULL- is this the
problem
{
if(loopPtr->item == Item)
return true;
else
{
loopPtr = loopPtr->next;
}
return false;
}
}
// this is the declaration in the spec file
private:
NodeType* listPtr;
int length;
</code>
Can anyone help?

The error's on line 162. In addition you forgot to specify a return
value in your IsThere function, for the case of listPtr == 0.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -
Pardon me but could you clarify about line 162. When I put in the
return value false for the case =0; the insert function skips.
Here is the other code
<code>
infile >Item;
if(list1.IsThere(Item))
{
while(infile)
{
if(list1.IsThere(Item))
{
list1.Insert(Item);
infile >Item;
}
}
}
else
cout << "Item duplicate!" << endl;
</code>

Thank you.

Apr 9 '07 #3
* zf*****@mail.com:
On Apr 9, 2:56 am, "Alf P. Steinbach" <a...@start.nowrote:
>* zfar...@mail.com:
>>>
I have this function that searches a link list before main performs
the insert function to insert an item. My problem is the pointer stuff
and the program keeps crashing.
<code>
List::List()
{
listPtr = NULL;
}
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
while(loopPtr != NULL) // but listPtr is NULL- is this the
problem
{
if(loopPtr->item == Item)
return true;
else
{
loopPtr = loopPtr->next;
}
return false;
}
}
// this is the declaration in the spec file
private:
NodeType* listPtr;
int length;
</code>
Can anyone help?
The error's on line 162. In addition you forgot to specify a return
value in your IsThere function, for the case of listPtr == 0.

Pardon me but could you clarify about line 162. When I put in the
return value false for the case =0; the insert function skips.
Here is the other code
<code>
infile >Item;
if(list1.IsThere(Item))
{
while(infile)
{
if(list1.IsThere(Item))
{
list1.Insert(Item);
infile >Item;
}
}
}
else
cout << "Item duplicate!" << endl;
</code>
Please don't quote signatures -- corrected.

Line 162 is part of the code you didn't post.

Now, regarding the missing return value specification in IsThere, I
listed that as one error because the other error in IsThere, the early
return after checking the first node, is really the same error. To find
that error, execute the code on paper (play at being the computer).
What happens when loopPtr is 0 (execute the code on paper to see)? What
happens when the item to be found is in the second or third node (ditto)?

Btw., for the code you posted this time, is it really your intention to
only insert items that already are in the list?

Or did you forget a couple of logical "not"s?

Thank you.
You're welcome. Just don't quote signatures.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 9 '07 #4
On Apr 9, 2:56 pm, "Alf P. Steinbach" <a...@start.nowrote:
* zfar...@mail.com:


I have this function that searches a link list before main performs
the insert function to insert an item. My problem is the pointer stuff
and the program keeps crashing.
<code>
List::List()
{
listPtr = NULL;
}
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
while(loopPtr != NULL) // but listPtr is NULL- is this the
problem
{
if(loopPtr->item == Item)
return true;
else
{
loopPtr = loopPtr->next;
}
return false;
}
}
// this is the declaration in the spec file
private:
NodeType* listPtr;
int length;
</code>
Can anyone help?

The error's on line 162. In addition you forgot to specify a return
value in your IsThere function, for the case of listPtr == 0.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -
Hello Steinbach,
Yeah£¬ Pardon me too:) how did you know the error was in line 162.

Apr 9 '07 #5
Hi
Can you take a look at this code and check my error? I understand the
logic in what Steinbach is saying but why the errors?
<code>

bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
if (Item == listPtr->item)
return true;
else
// Search for node in rest of list
{ loopPtr = listPtr;
while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;
}
</code>
the program just crashes

Apr 9 '07 #6
zf*****@mail.com wrote:
Hi
Can you take a look at this code and check my error? I understand the
logic in what Steinbach is saying but why the errors?
<code>

bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
if (Item == listPtr->item)
return true;
else
// Search for node in rest of list
{ loopPtr = listPtr;
while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
This line always gets executed.
}
return false;
}
</code>
the program just crashes
Apr 9 '07 #7
I'm sorry; are you referring to the return true line? I am still lost,
would you suggest the correct code to perform this search? if the item
is found, return true else return false in order to continue inserting
an item in the list. This is the only part holding me up; I've even
printed the entire list.

Apr 9 '07 #8
On Apr 9, 8:38 pm, zfar...@mail.com wrote:
Can you take a look at this code and check my error? I understand the
logic in what Steinbach is saying but why the errors?
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;

if (Item == listPtr->item)
return true;
else
// Search for node in rest of list
{ loopPtr = listPtr;
while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;}
</code>
the program just crashes
You missed one important point in what he said: execute the code
in your head. Play at being the machine.

And while you're at it, consider whether you really need any
special cases, or those premature returns. This is, after all,
a linear search, and the standard algorithm for a linear search
is just an empty while loop, followed by a return.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
6
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
8
by: Gordon Knote | last post by:
Hi can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++). The search should be as fast as possible and it would...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.