Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8635495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:59:15+00:00 2026-06-12T09:59:15+00:00

The delete from last functionality does not work properly. It shows that the node

  • 0

The delete from last functionality does not work properly. It shows that the node has been deleted but when I display it, it gets into an infinite loop and displays junk. Couldn’t figure what’s wrong!

Here’s the code:

using namespace std;
class List
{
    struct NODE
    {
        int item;
        NODE *next;
    };
    NODE *Head,*Tail;
    public:
    List()
    {
        Head=NULL;
        Tail=NULL;
    }
    ~List()
    {
        while(Head->next!=NULL)
        {
            Delete_At_Head();
        }
        Delete_At_Head();
    }
    void Add_At_First(int);
    void Add_At_Last(int);
    void Delete_At_Head();
    void Delete_At_Tail();
    int Is_Empty();
    void display();
};
void List::Add_At_First(int data)
{
    NODE *temp;
    temp=new NODE;
    if(Head==NULL)
    {
        temp->item=data;
        temp->next=NULL;
        Head=temp;
        Tail=Head;
    }
    else
    {
        temp->item=data;
        temp->next=Head;
        Head=temp;
    }
    cout<<"Node added at first!\n";
}
void List::Add_At_Last(int data)
{
    NODE *temp;
    temp=new NODE;
    temp->item=data;
    temp->next=NULL;
    if(Head==NULL)
    {
        Head=temp;
        Tail=temp;
    }
    else
    {
        Tail->next=temp;
        Tail=temp;
    }
    cout<<"Node added at last!\n";
}
void List::Delete_At_Head()
{
    NODE *temp;
    temp=new NODE;
    temp->item=Head->item;
    temp->next=Head->next;
    delete Head;
    Head=temp->next;
    delete temp;
    cout<<"Node deleted from head!\n";
}
void List::Delete_At_Tail()//Problematic part
{
    NODE *temp,*prev;
    temp=new NODE;
    prev=new NODE;
    temp=Head;
    while(temp->next!=NULL)
    {
        prev=temp;
        temp=temp->next;
    }
    prev->next=NULL;
    delete temp;
    delete Tail;
    Tail=prev;
    delete prev;
    cout<<"Node deleted from tail!\n";
}
int List::Is_Empty()
{
    if(Head==NULL)
        return 1;
    else
    return 0;
}
void List::display()//does not display after delete from tail
{
    NODE *temp;
    temp=new NODE;
    temp->item=Head->item;
    temp->next=Head->next;
    do
    {
        cout<<temp->item<<"-->";
        temp=temp->next;
    }while(temp->next!=NULL);
    cout<<temp->item;
}
int main()
{
    List obj;
    int ch,data;
    do
    {
        cout<<"\n1.Display\n2.Add at first\n3.Add at last\n4.Delete at 
head\n5.Delete at tail\n6.Exit\nEnter your choice: ";
        cin>>ch;
        switch(ch)
        {
            case 1:
            {
                if(obj.Is_Empty())
                    cout<<"List is Empty!\n";
                else
                    obj.display();
                break;
            }
            case 2:
            {
                cout<<"Enter data: ";
                cin>>data;
                obj.Add_At_First(data);
                break;
            }
            case 3:
            {
                cout<<"Enter data: ";
                cin>>data;
                obj.Add_At_Last(data);
                break;
            }
            case 4:
            {
                if(obj.Is_Empty())
                    cout<<"List is Empty!\n";
                else
                    obj.Delete_At_Head();
                break;
            }
            case 5:
            {
                if(obj.Is_Empty())
                    cout<<"List is Empty!\n";
                else
                    obj.Delete_At_Tail();
                break;
            }
            case 6:
            {
                break;
            }
        }
    }while(ch!=6);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:59:16+00:00Added an answer on June 12, 2026 at 9:59 am

    The following should correctly delete the last item in the list:

    void List::Delete_At_Tail()
    {
        if(Head == NULL) return;
    
        // If only one item in the list, delete it and empty the list...
        if(Head->next == NULL) {
            delete Head;
            Head = NULL;
            Tail = NULL;
            return;
        }
    
        // Find the last item in the list
        NODE *temp = Head;
        while(temp->next!=Tail)
        {
            temp=temp->next;
        }
    
        delete Tail;
        temp->next=NULL;
        Tail=temp;
        cout<<"Node deleted from tail!\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete all data from a table that has a date prior
Normaly i would do a delete * from XXX but on this table thats
I would like to return the ids which have been deleted by a DELETE
I'm using this code to delete a row in a table, but it's not
How can I delete the last member from a set? For example: set<int> setInt;
This shows me all the first names and last names that have exactly two
I was searching from last 1-2 hours about this question but didn't get any
I'm trying to delete the last added entry of a table: DELETE FROM notes
I am Trying to connect mysql to vc++ from last 7 days. But nobody
delete from a A where a.ID = 132. The table A contains around 5000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.