开发者

double ended list

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 f开发者_如何学编程irst 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.


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.


First off, your insertLast method results in a memory leak, as you never actually connect the next pointers of the newly-created Links to anything. You simply reassign the local pointer newlink, which goes out of scope immediately afterward. Subsequent insertLast calls leave memory out in space. This might also be causing the error to be printed.

Second, your deleteFirst method doesn't actually delete anything, but leaves a Link allocated with new out in memory with nothing pointing to it anymore. Another memory leak.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜