Jump to content
  • Advertisement
Sign in to follow this  
Moe

Linked list tutorial using New and Delete...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
You can just swich the malloc like this:

//Malloc
Node* pNode = reinterpret_cast<Node*>(malloc(sizeof(Node)));
free(pNode);

//new
Node* pNode = new Node();
delete pNode;


or wasn't that what you were looking for?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
a real simple solution is this:

struct SLink
{
int i;
SLink *next;
}

SLink *linkedlist = new SLink();
linkedlist->next = NULL; // 0x00000000

delete linkedlist;




is that what you were looking for?

guyaton

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!