Need help debugging a line.

ListElement* const ptr = new ListElement (item, head->next);

Any ideas why this dumps me after execution? This line is in a Prepend circular linked-list class function.

Comments

  • [b][red]This message was edited by bryce at 2002-10-20 19:50:18[/red][/b][hr]
    : ListElement* const ptr = new ListElement (item, head->next);
    :
    : Any ideas why this dumps me after execution? This line is in a Prepend circular linked-list class function.
    :

    This should answer the question, and fly just fine. I have included
    the node helper class, and list class. I compile a cpp file seperately
    and just include the list class in the cpp file. Both the list class
    and node class are .h files.

    The great thing about these are they are not platform specific c++.
    Look at the list class, and see how we allocate memory with new, and
    use of pointers.

    If you have any other questions do not hesitate to ask.
    Bryce

    //Node Helper class

    #ifndef NODE_H
    #define NODE_H

    #include
    using std::ostream;
    using std::istream;

    // ---=== CLASS DECLARATION ===---

    template
    class node
    {
    private:
    data element;
    node * nextone;
    public:
    node ();
    node (data d);
    node (node * p);
    node (node & n);
    ~node ();

    node operator = (const node n);
    node operator = (const data d);
    node operator = (node * p);

    bool operator < (node n);
    bool operator < (const data d);
    bool operator == (node n);
    bool operator == (const data d);

    node * next();
    data extractinfo();

    friend ostream & operator << (ostream & out, const node<data> n);
    friend istream & operator >> (istream & in, const node & n);
    };

    // ---===DEFINITION===---

    template
    node :: node ()
    {
    nextone = NULL;
    }

    template
    node :: node (data d)
    {
    element = d;
    nextone = NULL;
    }

    template
    node :: node (node * p)
    {
    nextone = p;
    }

    template
    node :: node (node & n)
    {
    element = n.element;
    nextone = n.nextone;
    }

    template
    node :: ~node ()
    {
    nextone = NULL;
    }

    template
    node node :: operator = (const node n)
    {
    element = n.element;
    nextone = n.nextone;
    return n;
    }

    template
    node node :: operator = (const data d)
    {
    element = d;
    nextone = NULL;
    return *this;
    }

    template
    node node :: operator = (node * p)
    {
    nextone = p;
    return *this;
    }

    template
    bool node :: operator < (node n)
    {
    if(element < n.element)
    return true;
    else
    return false;
    }

    template
    bool node :: operator < (const data d)
    {
    if(element < d)
    return true;
    else
    return false;
    }

    template
    bool node :: operator == (node n)
    {
    if(element == n.element)
    return true;
    else
    return false;
    }

    template
    bool node :: operator == (const data d)
    {
    if(element == d)
    return true;
    else
    return false;
    }

    template
    node * node :: next ()
    {
    return nextone;
    }

    template
    data node :: extractinfo ()
    {
    return element;
    }

    template
    ostream & operator << (ostream & out, const node<data> n)
    {
    out << n.element;
    return out;
    }

    template <class data>
    istream & operator >> (istream & in, node & n)
    {
    in >> n.element;
    return in;
    }

    #endif

    /*
    Possibly Pending STL Copywrite

    */

    //Linked List class declarations.
    #ifndef LIST_H
    #define LIST_H

    #include "node.h"

    template

    // ---=== CLASS DECLARATION ===---
    class list
    {
    private:
    node * start;
    node * current;
    node * find_predecessor (node n);
    node * find_location (node n);
    public:
    list ();
    list (const list & l);
    list operator = (const list l);
    bool operator == (const list l);
    bool retrieve(data & d);
    ~list ();//Class destructor
    };

    //Class implementations

    template
    node * list::find_predecessor (node n)
    {
    node * location = NULL;
    node * ahead = start;

    while ((ahead != NULL) && (*ahead < n))
    {
    location = ahead;
    ahead = ahead -> next();
    }

    return location;
    }

    template
    node * list::find_location (node n)
    {
    node * present = start;

    while( !(present == NULL) && ( !(*present == n) ) )
    present = present -> next ();

    return present;
    }


    template
    list::list ()
    {
    start = NULL;
    current = NULL;
    }
    template
    list::list (const list & l)
    {
    node * templeft;
    node * tempright;

    if(l.start == NULL)
    {
    start = NULL;
    current = NULL;
    }
    else
    {
    start = new node;
    *start = *(l.start);

    if (l.current == l.start)
    current = start;

    templeft = start;
    tempright = l.start;
    tempright = tempright -> next ();

    while (tempright != NULL)
    {
    *templeft = new node;
    templeft = templeft -> next ();
    *templeft = *tempright;

    if (tempright == l.current)
    current = templeft;

    tempright = tempright -> next ();
    } // end of while

    } // end of else
    }

    template
    list list::operator = (const list l)
    {
    node * templeft;
    node * tempright;

    if(l.start == NULL)
    {
    start = NULL;
    current = NULL;
    }
    else
    {
    start = new node;
    *start = *(l.start);

    if (l.current == l.start)
    current = start;

    templeft = start;
    tempright = l.start;
    tempright = tempright -> next ();

    while (tempright != NULL)
    {
    *templeft = new node;
    templeft = templeft -> next ();
    *templeft = *tempright;

    if (tempright = l.current)
    current = templeft;

    tempright = tempright -> next ();
    }//End while
    }//End else

    return l;
    }

    template
    bool list::operator == (const list l)
    {
    bool result = true;
    node * templeft = start;
    node * tempright = l.start;

    while( (templeft != NULL) && (tempright != NULL) && (result == true) )
    {
    if( !(*templeft == *tempright) )
    result = false;

    templeft = templeft -> next ();
    tempright = tempright -> next ();
    }
    result = result && (templeft == NULL) && (tempright == NULL);

    return result;
    }

    template
    bool list::retrieve (data & d)
    {
    node n (d);
    node * location = find_location (n);
    bool success = !(location == NULL);

    if(success)
    {
    d = location -> extractinfo ();
    }

    return success;
    }

    template //Isn't this nice? All we need to do for the
    //class destructor.
    list::~list ()
    {
    delete (start);
    }

    #endif







Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion