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

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);
}
}

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>.

I have created function to modify a node in Linkedlist in C++ but its not working: [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This Function is Created in LinkedList to modify a node at a given position. But, this function is not working properly and it is giving some random values.
void update_data(int old, int new_data) {//Function toupdate node
Node *curr=header;//Data members
int pos = 0;
while(curr->next!=NULL) {
if(curr->isbn == old)
{
curr->isbn = new_data;
cout<<old<<" Found at position "<<pos<<" Replaced with "<<new_data<<endl;;
}
curr = curr->next;
pos++;
}
}
For starters the variable pos is not used within the function.
Secondly the condition of the while loop
while(curr->next!=NULL) {
is incorrect and in general can invoke undefined behavior because the pointer header can be equal to nullptr. And moreover if the list contains only one node pointed to by the pointer header and its data member isbn is equal to the value of the variable old it will not be changed.
The function should not output any message.
The function can look the following way
void update_data( int old, int new_data )
{//Function toupdate node
for ( Node *curr = header; curr != nullptr; curr = curr->next )
{
if ( curr->isbn == old )
{
curr->isbn = new_data;
}
}
}

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.

Remove node at the TAIL Linked List C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
All, I'm creating a function that would remove the tail from a linked list. And My function works fine for just one iteration but it doesn't for subsequent ones.
Could anybody shed some light?
Thanks
int List::removeAtTail(){
if(head == NULL)
{
cout << "Node cannot be deleted from an empty linkedList" << endl;
}
if(curr->next= NULL)
{
curr->next=curr;
}
return 0;
}
And also, if I would like to return the element that I deleted, how would I go around at doing that?
There are many ways this can be done, following is one:
int List::removeAtTail(){
while(curr != NULL) {
if(curr->next == NULL) { // depending on your implementation you might use Tail
int temp = *curr;
delete curr;
return temp;
}
curr = curr->next;
}
return 0;
}
Notice how we iterate through the list until we find the last item. At that point we store it in a temporary variable before freeing the memory. Lastly we return the value stored in the temporary variable.

Checking user input in Link List (C++) [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 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.