working with pointers
-
I wrote this linked list code. Could someone confirm I`m doing it the way I`m supposed to.
class SomeNode
{
public:int data; SomeNode \* next;
};
SomeNode * BuildList()
{
SomeNode * Tail;
Tail = (SomeNode*)malloc(sizeof(SomeNode));
Tail->next = NULL;
Tail->data = 100;
SomeNode * Temp;
bool firstrun = true;
for(int i =0; i < 5; i++)
{
if(firstrun)
{
Temp = AddTo(&Tail,i);
firstrun = false;
}
else
{
Temp = AddTo(&Temp,i);} } return Temp;
}
SomeNode * AddTo(SomeNode ** Source, int somedata)
{
SomeNode * NewSN;
NewSN = (SomeNode*)malloc(sizeof(SomeNode));
NewSN->data = somedata;
NewSN->next = (*Source);return NewSN;
}
-
I wrote this linked list code. Could someone confirm I`m doing it the way I`m supposed to.
class SomeNode
{
public:int data; SomeNode \* next;
};
SomeNode * BuildList()
{
SomeNode * Tail;
Tail = (SomeNode*)malloc(sizeof(SomeNode));
Tail->next = NULL;
Tail->data = 100;
SomeNode * Temp;
bool firstrun = true;
for(int i =0; i < 5; i++)
{
if(firstrun)
{
Temp = AddTo(&Tail,i);
firstrun = false;
}
else
{
Temp = AddTo(&Temp,i);} } return Temp;
}
SomeNode * AddTo(SomeNode ** Source, int somedata)
{
SomeNode * NewSN;
NewSN = (SomeNode*)malloc(sizeof(SomeNode));
NewSN->data = somedata;
NewSN->next = (*Source);return NewSN;
}
1. Since your code is C++ then why do you use malloc rather than new? :confused: 2. How and where do you free your allocated in heap memory?
-
1. Since your code is C++ then why do you use malloc rather than new? :confused: 2. How and where do you free your allocated in heap memory?
Thanks for your feedback Victor. Since these are your only observations I take it that overall my code is within 'allowed' range. malloc: Using it doesn`t seem to break anything so why not. free: the code is just a display of how things would work in principle.
-
Thanks for your feedback Victor. Since these are your only observations I take it that overall my code is within 'allowed' range. malloc: Using it doesn`t seem to break anything so why not. free: the code is just a display of how things would work in principle.
One thing it is not good using malloc in C++ apps is because the class ctor is not called. I see you are trying then to "solve" this problem using this code:
SomeNode \* Tail; Tail = (SomeNode\*)malloc(sizeof(SomeNode)); Tail->next = NULL; Tail->data = 100;
But you wiil need to do it in every place you will create the class instance and, moreover, after some updates in the class members you will have to look for all the occurrences of such a malloc and update the class member initializations accordingly!
-
I wrote this linked list code. Could someone confirm I`m doing it the way I`m supposed to.
class SomeNode
{
public:int data; SomeNode \* next;
};
SomeNode * BuildList()
{
SomeNode * Tail;
Tail = (SomeNode*)malloc(sizeof(SomeNode));
Tail->next = NULL;
Tail->data = 100;
SomeNode * Temp;
bool firstrun = true;
for(int i =0; i < 5; i++)
{
if(firstrun)
{
Temp = AddTo(&Tail,i);
firstrun = false;
}
else
{
Temp = AddTo(&Temp,i);} } return Temp;
}
SomeNode * AddTo(SomeNode ** Source, int somedata)
{
SomeNode * NewSN;
NewSN = (SomeNode*)malloc(sizeof(SomeNode));
NewSN->data = somedata;
NewSN->next = (*Source);return NewSN;
}
Just a few more things on top of what Victor said: 1. Typically the chaining structure comes before the payload (the data part). That allows you to have different sizes of objects in your list without having to modify the list management code. 2. Again in the typical case, the function that creates or extends the list (
BuildList
function in your example) receives a structure containing the list head and tail. That way you can easily add new elements at either end and this is the most common operation. 3. In general you should be very, very, very sure that you need to use a linked list. Linked lists perform poorly because they don't make good use of cache. For a longer discussion about this see Bjarne Stroustrup: Why you should avoid Linked Lists - YouTube[^] and also Are lists evil? -- Bjarne Stroustrup : Standard C++[^].Mircea
-
One thing it is not good using malloc in C++ apps is because the class ctor is not called. I see you are trying then to "solve" this problem using this code:
SomeNode \* Tail; Tail = (SomeNode\*)malloc(sizeof(SomeNode)); Tail->next = NULL; Tail->data = 100;
But you wiil need to do it in every place you will create the class instance and, moreover, after some updates in the class members you will have to look for all the occurrences of such a malloc and update the class member initializations accordingly!
Quote:
One thing it is not good using malloc in C++ apps is because the class ctor is not called.
well I`m not really chasing the perfect solution, to be honest I haven`t even got to use the feature that much until now, my projects were simple enough to go about without using new/malloc Again huge thanks for helping me sort this out.
-
One thing it is not good using malloc in C++ apps is because the class ctor is not called. I see you are trying then to "solve" this problem using this code:
SomeNode \* Tail; Tail = (SomeNode\*)malloc(sizeof(SomeNode)); Tail->next = NULL; Tail->data = 100;
But you wiil need to do it in every place you will create the class instance and, moreover, after some updates in the class members you will have to look for all the occurrences of such a malloc and update the class member initializations accordingly!
I have an update. This is a function for inserting nodes in the middle of the list. Is this a good approach? I did a quick test and it looks like it`s doing what it should.
void InsertTo(SomeNode ** FrontTip, int NewNodeData, int InsertInFrontOfNodeCount, int TotalNodeCount)
{
SomeNode * Temp;
SomeNode * NewNode;
SomeNode * Previous;
NewNode = (SomeNode*)malloc(sizeof(SomeNode));
NewNode->data = NewNodeData;bool firstrun = true; for(int i =0; i < TotalNodeCount;i++) { if(i == InsertInFrontOfNodeCount) { NewNode->next = Temp; Previous->next = NewNode; } else { if(firstrun) { Temp = (\*FrontTip)->next; firstrun = false; } else { Previous = Temp; Temp = Temp->next; } } }
}
-
I wrote this linked list code. Could someone confirm I`m doing it the way I`m supposed to.
class SomeNode
{
public:int data; SomeNode \* next;
};
SomeNode * BuildList()
{
SomeNode * Tail;
Tail = (SomeNode*)malloc(sizeof(SomeNode));
Tail->next = NULL;
Tail->data = 100;
SomeNode * Temp;
bool firstrun = true;
for(int i =0; i < 5; i++)
{
if(firstrun)
{
Temp = AddTo(&Tail,i);
firstrun = false;
}
else
{
Temp = AddTo(&Temp,i);} } return Temp;
}
SomeNode * AddTo(SomeNode ** Source, int somedata)
{
SomeNode * NewSN;
NewSN = (SomeNode*)malloc(sizeof(SomeNode));
NewSN->data = somedata;
NewSN->next = (*Source);return NewSN;
}
CalinNegru wrote:
Could someone confirm I`m doing it the way I`m supposed to.
Maybe you meant to phrase it differently, but what is the "supposed to" way to which you refer? There are multiple ways to code a linked list, and only you know what goal(s) you are trying to meet.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
CalinNegru wrote:
Could someone confirm I`m doing it the way I`m supposed to.
Maybe you meant to phrase it differently, but what is the "supposed to" way to which you refer? There are multiple ways to code a linked list, and only you know what goal(s) you are trying to meet.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
It`s just a first time linked list implementation experience, I want to know if I`m not doing foolish things.
Quote:
There are multiple ways to code
I knew there is more than one approach when writing a linked list, but is there `that` many ways to do it?