# Linked list tutorial using New and Delete...

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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?

##### Share on other sites
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?

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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!]

##### Share on other sites
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.)

##### Share on other sites
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...

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633705
• Total Posts
3013464
×