From: alelvb@inwind.it   
      
   Hello posters,   
   I'm starting to study C++ after a brief panoramic of the C language.   
   The first problem I have to face is to avoid memory leaks in a testing   
   singly linked list. I'm not sure neither mine is the best way to   
   implemnt one [and I'd like to read your suggestions and advices].   
      
   My destructor prints a message to show it has been called, but when the   
   list goes out of scope at the end of main no message gets printed.   
      
   I'll read your posts,   
   thank you   
      
    **** cut here ****   
      
   #include    
      
   using std::cout;   
   using std::endl;   
      
   // the node class containing data   
      
   class Node   
   {   
    public:   
    Node(int);   
    ~Node();   
      
    Node *next;   
      
    int getValue() const { return itsDatum; }   
      
    private:   
    int itsDatum;   
      
   };   
      
   // constructor implementation   
      
   Node::Node(int itsDatum):   
   next(0),   
   itsDatum(itsDatum) {}   
      
   // destructor implementation   
      
   Node::~Node()   
   {   
    delete next;   
    cout << "node deleted." << endl;   
   }   
      
      
   void print(Node *);   
   void push(Node **, int);   
   void pushBack(Node **, int);   
      
   int main()   
   {   
      
    Node *head = 0; // head is the head of the empty list   
      
    push(&head, 1);   
    push(&head, 2);   
    push(&head, 3);   
    push(&head, 4);   
      
    print(head);   
      
    return 0;   
   }   
      
   // insert an element to the top of the list   
      
   void push(Node **phead, int value)   
   {   
    Node *n = new Node(value);   
      
    if(n != 0)   
    {   
      
    if(*phead == 0)   
    {   
    *phead = n;   
    }   
    else   
    {   
    n->next = *phead;   
    *phead = n;   
    }   
    }   
    else   
    cout << "\nmemory allocation error." << endl;   
   }   
      
   // pushes an element to the end of the list   
      
   void pushBack(Node **phead, int value)   
   {   
    Node *temp = new Node(value);   
      
    if(temp != 0)   
    {   
    if(*phead == 0)   
    {   
    *phead = temp;   
    }   
    else   
    {   
    Node *index = *phead;   
      
    while(index->next != 0)   
    {   
    index = index->next;   
    }   
    index->next = temp;   
    }   
    }   
    else   
    {   
    cout << "memory allocation error.";   
    }   
   }   
      
   // prints the list checking wheter it is an empy list   
      
   void print(Node *index)   
   {   
    if(index != 0)   
    {   
    while(index != 0)   
    {   
    cout << index->getValue() << " ";   
    index = index->next;   
    }   
    cout << endl;   
    }   
    else   
    {   
    cout << "\athe list is empty." << endl;   
    }   
   }   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|