473,396 Members | 1,766 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,396 software developers and data experts.

linked list search

I am attempting to search a list to check whether an item is already
present before inserting. I am having trouble with the pointers; I
understand that I need to set the pointer to the start of list but I
keep getting compilation errors. Can anyone help? Here is my code:
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
if (Item == listPtr->item)
return true;
else
{// Search for node in rest of list

while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;
}

</code>

the program crashes on execution

Apr 9 '07 #1
4 2326
* zf*****@mail.com:
I am attempting to search a list to check whether an item is already
present before inserting. I am having trouble with the pointers; I
understand that I need to set the pointer to the start of list but I
keep getting compilation errors. Can anyone help? Here is my code:
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
if (Item == listPtr->item)
return true;
else
{// Search for node in rest of list

while (loopPtr->next->item != Item)
loopPtr = loopPtr->next;
return true;
}
return false;
}

</code>

the program crashes on execution
What happens when loopPtr is 0?

Walk through the code's execution to find out.

What happens when loopPtr isn't 0, and the searched for item is in the
first node?

Walk through the code's execution to find out.

What happens when loopPtr isn't 0, and the searched for item is not in
the first node?

Walk through the code's execution to find out.

--
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
zf*****@mail.com wrote:
I am attempting to search a list to check whether an item is already
present before inserting. I am having trouble with the pointers; I
understand that I need to set the pointer to the start of list but I
keep getting compilation errors. Can anyone help? Here is my code:
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;
loopPtr = listPtr;
if (Item == listPtr->item)
return true;
else
{// Search for node in rest of list

while (loopPtr->next->item != Item)
This presumes the item is there. What if it isn't? How do you stop
at the end of the list?
loopPtr = loopPtr->next;
return true;
}
return false;
}
I'd probably rewrite it as

NodePtr loopPtr = listPtr;
while (loopPtr) {
if (loopPtr->item == Item)
return true;
loopPtr = loopPtr->next;
}
return false;
>
</code>

the program crashes on execution
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 9 '07 #3
I'm sorry guys, I promise this is the last question. Here is my code
in main:

<code>
infile >Item;

if(list1.IsThere(Item))
cout << "Item duplicate!" << endl;
else
{ list1.Insert(Item);
infile >Item;
while(!(list1.IsThere(Item)))
{ while(infile)
{
if(!(list1.IsThere(Item)))
{
list1.Insert(Item);
infile >Item;
}
infile >Item
}
}
</code>

and the search function
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;

loopPtr = listPtr;

while(loopPtr)
{

if (loopPtr->item == Item)
{
return true;
}
loopPtr = loopPtr-next;
}

return false;
}
</code>

Will this work?
Apr 9 '07 #4

<zf*****@mail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
I'm sorry guys, I promise this is the last question. Here is my code
in main:

<code>
infile >Item;

if(list1.IsThere(Item))
cout << "Item duplicate!" << endl;
else
{ list1.Insert(Item);
infile >Item;
while(!(list1.IsThere(Item)))
{ while(infile)
{
if(!(list1.IsThere(Item)))
{
list1.Insert(Item);
infile >Item;
}
infile >Item
}
This block of code is confusing. I'm not quite sure what you are trying to
accomplish, as you are reding input in 3 different places, inserting in 2,
checking for duplicates in 3, and only displaying "Item Duplicate!" once.

Are you tryign to read the items until the end and simply not entering the
item if it's a duplicate, but then reading the rest? Or are you trying to
stop on any duplicate?

Wouldn't something like this be better?

while ( infile >item )
if ( list1.IsThere(Item) )
{
cout << "Item Duplicate!" << endl;
// break
// Uncomment break if you want to halt on any duplicate
}
else
list1.Insert(Item);
>

}
</code>

and the search function
<code>
bool List::IsThere(ItemType Item) const
// Post: If item is in the list IsThere is
// True: False otherwise
{
NodePtr loopPtr;

loopPtr = listPtr;

while(loopPtr)
{

if (loopPtr->item == Item)
{
return true;
}
loopPtr = loopPtr-next;
}

return false;
}
</code>

Will this work?


Apr 9 '07 #5

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

Similar topics

5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
6
by: Rylios | last post by:
I am trying to make a very basic text editor using a linked list, fstream, sorting... This is what i have so far...
9
by: Daniel Vukadinovic | last post by:
I want to implement an index/counter to my linked list. Why? I wrote a search function which searches the list for elements based on their values (say I add an element and I assign the value 54...
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...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.