Hi all, I am still having troubles with distinguishing call by reference and call by value. Let me ask my question on an example.
Code:
struct Node{
int data;
Node *next;
Node *previous;
};
struct CircularDoublyLinkedList{
Node *final;
void create();
void empty();
void add(Node *newptr);
};
void CircularDoublyLinkedList::create(){
final=NULL;
}
int main(){
Node newnode={9,NULL,NULL};
CircularDoublyLinkedList cdll;
cdll.create();
cdll.add(&newnode);
for(int i=8;i>=0;i--){
newnode.data=i;
cdll.add(&newnode);
}
cdll.empty();
}
Here is my add method:
Code:
void CircularDoublyLinkedList::add(Node *newNode)
{
if(final==NULL)
{
newNode->next=newNode;
newNode->previous=newNode;
final = newNode;
return;
}
newNode->next=final->next;
final->next->previous = newNode;
final->next=newNode;
newNode->previous=final;
}
Now, after all these things, I realized that we were sending the address of a Node-type variable. And we didn't allocate the memory dynamically for it. My questions are:
1- As far as I understood, there is only 1 node, and we're changing its data continuously in main. We are not creating another node to add to the cdll, are we?
2- Since we didn't allocate the memory dynamically, how the heck can we deallocate it ?
Your helps are greatly appreciated here Thanks.