Linked list tutorial using New and Delete...

Started by
10 comments, last by Moe 18 years, 8 months ago
Hey all, I have been wanting to code my own linked list for a little while now. I figured it would be a good way to get back into the swing of things. I have been looking around for a tutorial that uses New and Delete, rather than malloc() to allocate the memory for a new node in the list. Does anyone know of such a tutorial? Thanks.
Advertisement
You can just swich the malloc like this:
//MallocNode* pNode = reinterpret_cast<Node*>(malloc(sizeof(Node)));free(pNode);//newNode* pNode = new Node();delete pNode;

or wasn't that what you were looking for?
Well, I suppose that is what I was looking for. I was reading this article, and was pretty much wondering how to do it with New/Delete. I think I will have to do a bit more thinking about it, then sit down and code it. For the most part I understand the concepts, its just hard for me to get it into code.
Anyone?
a real simple solution is this:
struct SLink{    int i;    SLink *next;}SLink *linkedlist = new SLink();linkedlist->next = NULL; // 0x00000000delete linkedlist;


is that what you were looking for?

guyaton
~guyaton
I think I will just have to play around with it. I was doing a little thinking earlier today, and I think I have it figured out. I will just have to code it and see if it works.
struct Node{    int data;    Node* next;}class List{    private:    Node* head;    public:    /*... Member functions ...*/    void insert(int data);    void remove(int data);    // Etc. etc.}void List::insert(int data){    Node* nNode = new Node;    Node* curNode = List->GetHead(); // Hehe... no sexual pun intended    nNode->data = data;    nNode->next = NULL;    if(!curNode)        List->GetHead() = nNode;    while(curNode->next)        curNode = curNode->next;    curNode->next = nNode;}void List::remove(int data){    Node* curNode = List->GetHead();    Node* prevNode = List->GetHead();    if(!curNode)        return;    else if(curNode->data == data){        List->GetHead() = curNode->next;        delete curNode;    }    while(curNode && curNode->data != data){        prevNode = curNode;        curNode = curNode->next;    }    if(curNode){        prevNode->next = curNode->next;        delete curNode;    }}


Those are pretty much the two functions which will use new and delete. Of course insert can vary where you want to insert in sorted order or not. But this is just a basic implementation... What you do pretty much is replace malloc with new and free with delete. You can look at new as just a higher level malloc call where it does the malloc'ing for you.
Sweet! Thanks for the source. I am working my through it right now...

[in case anyone is wondering, SiCrane responded to a question that I posted asking if it was possible to use a class rather than a struct. I managed to get it to compile using a class, so I guess I answered my own question. Sorry SiCrane!]
In C++ a class and a struct are the same except that a struct defaults to public members and public inheritance and a class defaults to private members and private inheritance. (And you need to forward declare with the right keyword.)
Quote:Original post by SiCrane
(And you need to forward declare with the right keyword.)

Go ahead and call me inept, but how does one go about doing this? I have seen it done using structs, but not with classes...

This topic is closed to new replies.

Advertisement