Home > other >  This is code for my linkedlist in c . i have issue in deleteAtEnd method after deleting last node i
This is code for my linkedlist in c . i have issue in deleteAtEnd method after deleting last node i

Time:09-17

    #include <iostream>
using namespace std;

class Node{
    public:
        int data;
        Node* next;
        Node(int val){
            data=val;
            next=NULL;
        }
};
class LinkedList{
    public:
    Node *head;
    LinkedList(){
        head=NULL;
    }
        void insertAtEnd(int val){
            Node *newnode = new Node(val);
            if(head==NULL){
                head=newnode;
                return;
            }
            Node *temp=head;
            while(temp->next!=NULL){
                temp=temp->next;
            }
            temp->next=newnode;
        }
        void insertAtHead(int val){
            Node *newnode = new Node(val);
            newnode->next=head;
            head=newnode;
        }
        void deleteAtEnd(){
            if(head==NULL){
                cout<<"List Is Empty";
                return;
            }
            if(head->next==NULL){
                delete head;
                return;
            }
            
            Node *temp=head;
            while(temp->next->next!=NULL){
                temp=temp->next;
            }
            Node *todel=temp->next;
            delete todel;
            temp->next=NULL;
        }
        void print(){
            Node *temp=head;
            while(temp!=NULL){
                cout<<temp->data<<"->";
                temp=temp->next;
            }
            cout<<"NULL";
        }

};
int main()
{
    LinkedList ll;
    ll.insertAtEnd(10);
    ll.insertAtEnd(20);
    ll.insertAtEnd(30);
    ll.insertAtEnd(40);
    ll.insertAtEnd(50);
    ll.print();
    cout<<endl;
    ll.insertAtHead(100);
    ll.print();
    cout<<endl;
    ll.deleteAtEnd();
    ll.deleteAtEnd();
    ll.deleteAtEnd();
    ll.deleteAtEnd();
    ll.deleteAtEnd();
    ll.deleteAtEnd();
    
    
    ll.print();
    
    return 0;
}

This is code for my linkedlist in c . i have an issue in deleteAtEnd method, after deleting last node it shows some garbage value. I have added all checks in deleteAtEnd method that if head node is left then delete the head node.I can't find my mistake please help me with the solution. If you could provide me with code solution then it will be benificial for me. Such that what code should i change in my code so that cprogram runs perfectly.

CodePudding user response:

This part of the function deleteAtEnd

        if(head->next==NULL){
            delete head;
            return;
        }
        

is incorrect.

In the if statement you have to set the pointer head to nullptr (or NULL)

        if(head->next==NULL){
            delete head;
            head = nullptr;
            return;
        }

The function would have less branches if it would used a pointer to a pointer. For example

bool deleteAtEnd()
{
    bool success = head != nullptr;
 
    if ( success )
    {
        Node **current = &head;
        while ( ( *current )->next ) current = &( *current )->next;
        delete *current;
        *current = nullptr;
    }

    return success;
}

Pay attention to that the function should not issue any message. It is the user of the function that decides whether to issue a message or not based on the return value of the function.

Also the function print should be a constant member function because it does not change the object for which it is called.

    void print() const {
        const Node *temp=head;
        while(temp!=NULL){
            cout<<temp->data<<"->";
            temp=temp->next;
        }
        cout<<"NULL";
    }

CodePudding user response:

In the deleteAtEnd() function of your code, for the case where only one element is left in the linked list, the function triggers:

if(head->next==NULL){
    delete head;
    return;
}

Note that although it deletes the last node present in the linked list, but the pointer head is still pointing to that memory address, which now contains garbage value. So, make the head = NULL after deleting the last node.

Revised code:

if(head->next==NULL){
    delete head;
    head = NULL;
    return;
}
  • Related