how to define and recognize NULL pointer?

in this link list i'm trying to make np pointer of the last node null so in insert function it can be recognized to place the correct algorithm. but it jumps from else if(cp->np==0) at the point!

by "p->np = 0" i try to define a NULL pointer. am i wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
person* create_node()
{
	person *p = new person;
	p->np = 0;
	return p;
}

 //sp=start pointer, cp= current pointer, pp=previews pointer, p= new node pointer 
person* insert(person *&sp, person *&cp, person *&pp, person *&p)
{
	if(cp==sp)
	{
		p->np=sp;
		sp=p;
	}
	else if(cp->np==cp)
	{
		cp->np=p;
	}
	else
	{
		p->np=cp;
		pp->np=p;
	}

	return sp;
}


my person struct is like this:
1
2
3
4
5
6
7
struct person
{
	string fname;
	string Lname;
	person *np;

};
Last edited on
closed account (zb0S216C)
babakslt wrote:
by "p->np = 0" i try to define a NULL pointer. am i wrong? (sic)

No, you're not wrong. Null is effectively 0. However, this is not the case in C++0x.

Here's a tip for you: If the list is linear, add a separate instantiation of person (pointer) to the root node.
This pointer will point to the last node. That way, you don't have to iterate through all the nodes before finding the last one.

Wazzak
Last edited on
Topic archived. No new replies allowed.