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 6685681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:05:06+00:00 2026-05-26T05:05:06+00:00

The following code stops working, shows an empty screen, shows a number, and then

  • 0

The following code stops working, shows an empty screen, shows a number, and then prints an error message that code stopped working.

#include <iostream>
using namespace std;
class Link{
public:
  long ddata;
  Link *next;
  Link(long d){
    ddata=d;
  }
  void displaylin(){
    cout<<ddata<<"  "<<endl;
  }
};

class firstlast{
private :
  Link *first;
  Link * last;
public:
  firstlast(){
    first=NULL;
    last=NULL;
  }
  bool empthy(){
    return (first==NULL);
  }
  void insertfirst(long dd){
    Link *newlink=new Link(dd);
    if (empthy())
      last=newlink;
    newlink->next=first;
    first=newlink;
  }
  void insertlast(long dd){
    Link *newlink=new Link(dd);
    if ((empthy()))
      first=newlink;
    else
      last->next=newlink;
    newlink=last;
  }
  long deletefirst(){    //delete first link
    long temp=first->ddata;
    if (first->next==NULL)//only one item
      last=NULL;
    first=first->next;
    return temp;
  }
  void displayList(){
    cout<<"first -> last   "<<endl;
    Link *current=first;
    while(current!=NULL){
      current->displaylin();
      current=current->next;
    }
  }
};

int main(){
  firstlast *fl=new firstlast();
  fl->insertfirst(22);
  fl->insertfirst(44);
  fl->insertfirst(66);
  fl->insertlast(33);
  fl->insertlast(88);
  fl->insertlast(100);
  fl->deletefirst();
  fl->displayList();
  return 0;
}

I think somewhere is is overflow, but I can’t find where. IDE One shows the output of my program.

  • 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-05-26T05:05:07+00:00Added an answer on May 26, 2026 at 5:05 am

    The problem is this line in insertlast(long):

    newlink=last;
    

    It should be:

    last=newlink;
    

    With that change, the output becomes:

    first -> last   
    44  
    22  
    33  
    88  
    100  
    

    EDIT: As @Jared pointed out, your code suffers from memory leaks. This happens whenever you allocate heap memory with new or malloc but do not free it with the corresponding “free” function.

    It is important that each pointer returned by new be freed exactly once by delete. If you fail to call delete, then your program leaks memory. If you call delete more than once to “double free” a region, then your program invokes Undefined Behavior.

    In this code:

      long deletefirst(){    //delete first link
        long temp=first->ddata;
        if (first->next==NULL)//only one item
          last=NULL;
        first=first->next;
        return temp;
      }
    

    notice that first=first->next; makes you lose the pointer to a piece of dynamically-allocated memory. Remember that first was set to the result of new, so you need to delete first before overwriting first with a pointer to a different region of dynamically-allocated memory.

    Also, you need to add to your class:

    1. A destructor to free all memory that wasn’t freed through calls to deletefirst().
    2. A copy constructor to make a deep copy of firstlast instances.
    3. An overload of the assignment operator, operator=(const firstlast& fl), that also makes a deep copy.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in the header of a website that I'm working
The xmlns attribute in the following code stops me getting the value I need.
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
Following code iterates through many data-rows, calcs some score per row and then sorts
Following is the final code I was working on. I can sleep and show
I have following javascript code. Its a timer code. Timer stops if quiz is
I have the following code that, weirdly, works for a couple of seconds and
I have been working with the slider and am using the following code: $(function(){
I followed an example from the following url that shows how to use the
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666

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.