issues with Object pointers, null, 0

I know this has been asked many times and answered many times, but I am still very confused.
I have plenty of knowledge of Java and I am now trying to pick up the low level stuff that I missed out from C++ such as memory allocation, pointers ,etc.

Below, I am trying to create my own linked list from scratch. I am trying to create a list within a for loop. I would like to destory, set to null, set to 0, something with the object myNode. (If there is an easier way, I still would like to know how to do this)

I have tried creating a dynamic bit of memory for it with the keyword new then setting it to null, 0, or using delete or free().

I keep getting errors such as non-|value assignment, operator= not assigned.

Can anyone give me some advice or at least try and explain it to me?
Thank you in advanced. Your help is much appreciated.


#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
using namespace std;

class Node
{
public:
Node * pNext;
Node * pPrev;
int data;


Node * getNext(Node * thisNode)
{
thisNode = thisNode->pNext;
return thisNode;
}
Node * getPrev(Node * thisNode)
{
thisNode = thisNode->pPrev;
return thisNode;
}
void setData(Node * thisNode, int * myData)
{
thisNode->data = *myData;
}
};

int main()
{
int nodeNumber;
int thisData;


Node temp;
Node head;

// myNode = NULL;

cout<<"How many nodes?";
cin>>nodeNumber;


for(int a=0; a<nodeNumber; a++)
{
Node myNode;

cout<<"Enter data for " << a << ": ";
cin>>thisData;

myNode.data = thisData;

if(a==0)
temp.pNext = &myNode;



temp = myNode;
if(a==0)
head = myNode;

myNode = NULL;

}

for(int b=0; b<nodeNumber; b++)
{
cout << "Data for " << b << " is: " << head.data;
//&head = head.pNext;
}

return 0;

}
Just make these pointers
1
2
3
4
5
Node *temp;
Node *head;

Node *myNode; //also
// myNode = NULL; 

In C++ (unlike Java) NULL == 0, that's it. Because a pointer is pointing to a memory location, it contains an unsigned int. All memory locations are greater then 0 so pointing to memory at 0 means it's not pointing to valid memory. In Java, a Node would actually be a reference and so setting it to null means it's not referencing anything. In C++ a Node is an object, not a reference. That's why when you use the = operator you actually just call a method in the Node class that by default is (for the most part) equivalent to calling Object.clone() in Java.
I am still having a hard time understanding. I have set all my Node objects to just pointers. But now I have an error that says "cannot convert Node** to Node* at the bold statement below.

(*temp).pNext = &myNode;

That doesn't make sense to me. I am taking the data of temp and using its pointer of pNext and making it equal to the address of myNode. Shouldn't that work. Updated main is below.

int main()
{
int nodeNumber;
int thisData;

Node *myNode;
Node *temp;
Node *head;



cout<<"How many nodes?";
cin>>nodeNumber;


for(int a=0; a<nodeNumber; a++)
{


cout<<"Enter data for " << a << ": ";
cin>>thisData;

(*myNode).data = thisData;
if(a==0)
(*temp).pNext = &myNode;



if(a==0)
&head = &myNode;



}

for(int b=0; b<nodeNumber; b++)
{
cout << "Data for " << b << " is: " << head.data;
//&head = head.pNext;
}

return 0;

}
holy crap. Im an idiot. Thanks anyway.
Topic archived. No new replies allowed.