Linked List Problem

Ok so I know how to do this with pointer to pointer..
AddNode(Node **ppNode) and what not but I cant figure out for the life of me how to get it working with just a single pointer.

The code is small but a mess and I have no clue how to get this working so the arguments are just AddNode( Node *pNode ) etc.

Thanks to anyone who can fix where I'm going 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <windows.h>
#include <stdio.h>

struct Node
{
	int value;
	Node *next;
};

void AddNode(Node *pNode)
{
	Node *temp = new Node;
	Node *temp2 = new Node;

	printf("Enter a value to store: ");
	scanf_s("%d", &temp->value );
	temp->next = NULL;

	if( pNode == NULL )
	{
		pNode = &(*temp);
	}
	else
	{
		temp2 = &(*pNode);

		while( temp2->next != NULL )
		{
			temp2 = temp2->next;
		}
		temp2->next = temp;
	}
}

void DisplayNodes(Node *pNode)
{
	Node *temp;
	temp = new Node;

    temp = &(*pNode);

    printf("\n");
    if (temp == NULL)
	{
		printf("The List is empty\n");
	}
    else
    {
		while (temp != NULL)
	    {
			printf("Nodes Value -> %d\n", temp->value);
			temp = temp->next;
	    }
		printf("END OF LIST!\n");
    }
}

int main()
{
	Node *myNode = NULL;
	bool isRunning = true;
	do
	{
		
		AddNode(myNode);
		DisplayNodes(myNode);

	}while(isRunning);
}
I tried using you code and just changing node pointer to a node pointer reference for addNode, and it got stuck in a loop printing a number. But then I just decided to clean up whenever you dereferenced and then referenced something, and it worked for some reason.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <windows.h>
#include <stdio.h>

struct Node
{
	int value;
	Node *next;
};

void AddNode(Node*& pNode)
{
	Node *temp = new Node;
	Node *temp2 = new Node;

	printf("Enter a value to store: ");
	scanf_s("%d", &temp->value );
	temp->next = NULL;

	if( pNode == NULL )
	{
		pNode = temp;
	}
	else
	{
		temp2 = pNode;

		while( temp2->next != NULL )
		{
			temp2 = temp2->next;
		}
		temp2->next = temp;
	}
}

void DisplayNodes(Node *pNode)
{
	Node *temp;
	temp = new Node;

    temp = &(*pNode);

    printf("\n");
    if (temp == NULL)
	{
		printf("The List is empty\n");
	}
    else
    {
		while (temp != NULL)
	    {
			printf("Nodes Value -> %d\n", temp->value);
			temp = temp->next;
	    }
		printf("END OF LIST!\n");
    }
}

int main()
{
	Node *myNode = NULL;
	bool isRunning = true;
	do
	{
		
		AddNode(myNode);
		DisplayNodes(myNode);

	}while(isRunning);
}

I hope someone can tell my why using something like pNode = &(*temp) is different from pNode = temp;. I'm very curious as to why it didn't work.
Thanks a heap for that. It works a charm. But whats the main point in pointer to reference. As In "Node *&pNode" is this basically just allowing the pointer to be edited?
if you write it like
Node *&pNode

then it simply sends the pointer by reference that is you can modify the pointer to point anywhere you want to (of course not beyond the memory you have been given for your program else it may lead to a segmentation fault) whereas
Node *pNodewill simply create a copy of the pointer and pass the copy so the original pointer cannot be modified
Awesome thanks, that clears up a lot :)

Thanks,
Myth.
Topic archived. No new replies allowed.