I'm starting learning C++ so I decided to try to implement a class able to store numeric values.  

And to add a little more challenge, I decided to make it **sorted**: The elements of the stack have to be sorted in ascending order from the top to the bottom.
I decided to use NaN to represent an empty stack.

So here's the hpp file:

```
#ifndef MYMSS2_RENAME_ME_HPP
#define MYMSS2_RENAME_ME_H
class mySortedStack{
    public:
        double info;
        mySortedStack * next;

        mySortedStack();
        mySortedStack(double a);
        mySortedStack(const mySortedStack& a);
        ~mySortedStack();

        double top();
        double pop();
        void push(double a);

        int len();

};
#endif

```

and here's the cpp file:

```
#include <iostream>
#include "myMSS2_RENAME_ME.hpp"
#include <math.h>

using namespace std;

mySortedStack::mySortedStack(){
    //cout<<"Inside empty constuctor"<<endl;
    info=NAN;
    next=NULL;
    //cout<<"Leaving empty constuctor"<<endl;
}

mySortedStack::mySortedStack(double a){
    //cout<<"Inside normal constructor"<<endl;
    info=a;
    next=NULL;
    //cout<<"Leaving normal constuctor"<<endl;
}

mySortedStack::mySortedStack(const mySortedStack& a){//this should be able to do deepcopies
    //cout<<"Inside copy constructor"<<endl;
    info=double(a.info);
    next=new mySortedStack;
    next=a.next;
    //cout<<"Leaving copy constuctor"<<endl;
}

mySortedStack::~mySortedStack(){
    //cout<<"Inside destructor"<<endl;
    delete this;
}

double mySortedStack::top(){
    return info;
}

void mySortedStack::pop(){
    double retVal;
    mySortedStack *tempStack = this->next;
    retVal = info;
    info = tempStack->info;
    next = tempStack->next;
    tempStack->next = NULL;
    delete tempStack;
    return retVal;
}

void mySortedStack::push(double a){
    //cout<<"Pushing!"<<endl;
    if (isnan(info)){//if stack is empty
        info=a;
    }
    else{
        mySortedStack *pushed=new mySortedStack();
        if(info>a){//if the pushed element will be placed at the top
            pushed->info=info;
            pushed->next=next;
            info=a;
            next=pushed;

        }else{//else
            mySortedStack *currentNode=this;
            while(currentNode->next->info<a){
                currentNode=currentNode->next;
            }
            mySortedStack *pushed=new mySortedStack();
            pushed->info=a;
            pushed->next=currentNode->next;
            currentNode->next=pushed;
        }

    }
}

int mySortedStack::len(){
    int i=1;
    mySortedStack currentNode=*this;
    if (isnan(info)){
        return 0;
    }
    while (currentNode.next!=NULL){
        i+=1;
        currentNode=*currentNode.next;
    }
    return i;
}

```

This compiles fine (I'm using Dev C++ on Windows 8).

So I tried to run a few tests by adding a `main` function in the cpp file:

```
int main(){
    mySortedStack mss0;
    mySortedStack mss1(7);
    mySortedStack mss2=mss1;
    mss1.info=6;
    cout<<mss1.info<<' '<<mss2.info<<endl;//copy constructor seems to work! output is '6 7'

    system("PAUSE");

    mss1.push(4);
    mss1.push(5);

    //the next piece of code is supposed to show the whole stack
    bool flag=true;
    mySortedStack inspected=mss1;
    while(flag){
        cout<<inspected.info<<endl;
        if(inspected.next==NULL){
            flag=false;
            cout<<"NULL found!"<<endl;
        }
        else{
            inspected=*inspected.next;
        }
    }
    //seems correct. Output is:
    //'4
    //5
    //6
    //NULL found!'
    system("PAUSE");
    return 0;
}

```

Aaaannd here it comes! Segmentation fault!(Exact error message: Program received signal SIGSEGV, segmentation fault) It seems to happen just before the `return 0`.  

After adding some `cout<<`, it seems that the problem resides in my destructor (infinite loop). Since I have no idea about which call of the destructor makes this, I neither have a clue if the problem is due to the destructor itself or the way I manipulate the pointers in a class method or anywhere else. I tried to draw on paper what happens inside of each of my methods and everything seems fine.  

What am I doing wrong?  

Thank you very much in advance.