Checking user input in Link List (C++) [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a link list here to check for user input to see whether user has input the word before or not.
ListNode *cur = head;
while ( cur != NULL )
{
if ( guess == cur->item )
{
return true;
}
cur = cur->next;
}
return false;
My problem is that even though the list is empty, it'll still enter the while loop. What's my mistake?

Do you initialize the empty list with head=NULL;? Otherwise head will most likely point to some random memory, and it will be impossible to detect that the list is empty.

Related

This assignment is designed to explore linked lists so you will implement a singly linked-list to hold a collection of bids loaded from a CSV file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I need help please It keeps giving me the same number and not a different one.
Bid LinkedList::Search(string bidId) {
// FIXME (6): Implement search logic
// special case if matching node is the head
// make head point to the next node in the list
//decrease size count
//return
// start at the head of the list
// keep searching until end reached with while loop (next != nullptr
// if the current node matches, return it
// else current node is equal to next node
//return bid
Node* temp = head;
Node* holder = new Node;
holder->bid.bidId = "";
while (temp != nullptr) {
cout << temp->bid.bidId << endl;
if (temp->bid.bidId == bidId) {
return temp->bid;
}
temp = temp->next;
return holder->bid;
}
}
Just remove everything with a holder. And at the end throw an exception when nothing was found. Alternatively return std::optional<Bid>.

What does this cycle do?What is the point? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
int myDrawOrder = sprite->GetDrawOrder();
auto iter = mSprites.begin();
for(;iter != mSprites.end(); ++iter)
{
if(myDrawOrder < (*iter)->GetDrawOrder()) //what does this line mean?
{
break;
}
}
GetDrawOrder() returns the position of the sprite in the queue.
The loop iterates over the sprites, and breaks when it finds a spite that should be drawn before the sprite. You didn't share the rest of the code, but presumably something is then done with that sprite (e.g., it's drawn).
Since iter is not local to the loop, it's left after the loop pointing to the first element for which myDrawOrder < (*iter)->GetDrawOrder() is true (or mySprites.end() if there wasn't one).
It's a search operation.
It could also be written thusly:
const int myDrawOrder = sprite->GetDrawOrder();
auto iter = std::find_if(
std::begin(mSprites),
std::end(mSprites),
[&](const auto& sprite) {
return sprite.GetDrawOrder() >= myDrawOrder;
}
);

How to make recursive singly linked list (C++) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My book is asking me to make a recursive definition of a singly linked list. I have no idea at all how to do that. Can someone please help me out with a sample? Thanks
It's just like a normal linked list except the iteration is performed by recursion rather than loops.
First, a little light reading: What is recursion and when should I use it?
For example, a loop-based function to find the last node could be:
Node * getLast(Node * current)
{
while (current->next == null)
{ // loop until no more nodes
current = current.next;
}
return current; // return last node
}
While the recursive version only checks if the current node is the last and calls itself with the next node if there is a next node.
Node * getLast(Node * current)
{
if (current->next == null)
{ // found last node. return it
return current;
}
else
{ // see if next node is last node
return getLast(current->next);
}
}

Binary tree - Delete nodes whose level is greater or equal than given one [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
As title says, I need to write function that deletes all nodes from binary tree that have level greater or equal than given one in function as parameter, for example, I need to delete all nodes that are level 2 and higher.
But, I cannot add any attributes to nodes or use stack, queue, etc, or any other structure.
Any ideas on this? I'll appreciate it very much.
The implementation of Node and deleteNode is up to you:
void deleteLevel(Node *node, size_t level)
{
if(node->right)
{
if(level == 0)
{
deleteNode(node->right);
node->right = nullptr;
}
else deleteLevel(node->right, level - 1);
}
if(node->left)
{
if(level == 0)
{
deleteNode(node->left);
node->left = nullptr;
}
else deleteLevel(node->left, level - 1);
}
}
...
deleteLevel(aNode, levlOfANode);
The implementation of Node and deleteNode is up to you.

How to solve "Access Violation" when using iterators? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a class named CLine, which contains vector<CPoint>. I saved all the points along the line in the vector<CPoint> m_vecPoint. I was trying to traverse the points when the problem occurs.
My code:
for(vector<CLine>::iterator iter = m_vecLine.begin(); iter != m_vecLine.end(); iter++)
{
vector<CPoint>::iterator iter1 = iter->m_vecPoint.begin();
int temp = iter1->x;
}
When I debug this, it downs at int temp = iter1->x saying Access Violation.
What may be wrong?
It could be possible that begin == end
for(vector<CLine>::iterator iter = m_vecLine.begin(); iter != m_vecLine.end(); iter++)
{
vector<CPoint>::iterator iter1 = iter->m_vecPoint.begin();
if(iter1 != iter->m_vecPoint.end())
{ int temp = iter1->x;}
}