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

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.

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

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

Why wouldn't the head change - linkedlist implementation [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 8 years ago.
Improve this question
What I'm trying to do is to insert a node with a value to the ith location of a linked list. It has to return true if the insertion was successful, false otherwise. What I'm not able to understand is how to add to the start of the list. What I tries did not insert it at all. Where is the problem?
bool insertat(struct Node*head, int value,int loc){
Node *q = new Node;
q->data=value;
if(loc>list_length(head)){
insertlast(head,value);
return true;
}
else if(loc<=0){
cout<<"invalid location";
return false;
}
else if(loc==1){
q->next=head;
head=q;
return true;
}
else{
int i=1;
Node*p =head;
while( i<loc-1){
p=p->next;
i++;
}
q->next=p->next;
p->next=q;
return true;
}
}
This is because you are passing pointer by value. So, changes done to head in this function are lost.
Rather change your function to:-
bool insertat(struct Node**head, int value,int loc)
and pass the address of head.
OR
bool insertat(struct Node*&head, int value,int loc)

Linked List delete function exception [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have some study to do for an upcoming C++ assessment and I have an issue with my deleteNode function. I know the problem lies somewhere within the function as I copied a previous deleteNode function and it worked fine.All of my other pushback/pushfront functions work fine, I'd rather know the problem than just ignore it but I can't spot it for the life of me.
So the linked list is storing Employees, with a name (string) and a salary (double). My deleteNode matches the name of the employee and the string parameter passed in. If anyone can spot my mistake it'll be a great help to my learning!
bool EmployeeList::deleteNode(std::string n){
EmployeeNode *leadptr = head, *trailptr = nullptr;
if (head != nullptr){
if (head->emp.name == n){
head = head->next;
delete leadptr;
return true;
}
else{
while (leadptr != nullptr && leadptr->emp.name != n){
trailptr = leadptr;
leadptr = leadptr->next;
}
if (leadptr = nullptr){
return false;
}
else{
trailptr->next = leadptr->next; //access violation here, leadptr may be null
delete leadptr;
return true;
}
}
}
}
This is an assignment
if (leadptr = nullptr){
Correction would be
if (leadptr == nullptr){