Linklists! >.<

Hello!

I've just started revisiting C++ after about 1.5 years of stagnancy and was just getting into implementing linked lists in C++.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>

using namespace std;

class node
{
	private:
		int variable;
		node *ptr;
	public:
		node(int k);
		~node();
		int getvariable();
		void setvariable(int i);
		node *getpointer();
		void setpointer(node *add);
};

node::node(int k)
{
	variable = k;
}

node::~node()
{
	delete ptr;
}

int node::getvariable()
{
	return variable;
}

void node::setvariable(int i)
{
	variable = i;
}

node *node::getpointer()
{
	return ptr;
}

void node::setpointer(node *add)
{
	ptr = add;
}

node *linklist()
{
	int n, o;
	node *head = 0;
	node *temp = 0;
	
	cout << "Input number of things: " ;
	cin >> n;
	cout << endl;
	
	for (int i=1; i<n+1; i++)
	{
		cout << "Object Value:";
		cin >> o;
		
		if (i==1)
		{
			head = new node(o);
			temp = head;
		}
		else
		{
			temp = temp -> getpointer();
			temp = new node(o);			
		}		
	}
	return head;
}

int total(node *llist)
{
	int i = 0;
	node *temp= 0;
	node *next = 0;
	temp = llist;
	
	while(temp != 0)
	{
		temp = temp -> getpointer();
		i++;
	}
	return i;
}

int main()
{
	node *list = linklist();
	
	cout << endl;
	cout << total(list) << endl;
	
	return 0;
}


The code above has a class that defines the nodes in the very basic integer based linked list, a function to create the linked list using multiple instances of the nodes, and a function to traverse through the entire list, counting the total number of nodes in the list. I do know about the memory leaks that can arise due to unresolved pointer variables.

The trouble comes in when I run the total function. It gives me back a value of 1, and not anything more. I suspect it's because the function that creates the linked list cannot return multiple instances of the node at a time.

The Question: Is there a way to correct this problem without me having to modify the node class? Thanks for answering! :D
I suspect it's because the function that creates the linked list cannot return multiple instances of the node at a time.
I don't know what you mean by this, but the result is correct. There's just one element in the list.

The member variable ptr is not modified anywhere. Normally your program should crash since ptr remains uninitialized.
Sorry for the long wait for my reply, I thought they would send me any emails about any new activity...

I did implement the ptr variable in line 9, didn't I? Unless you mean pointing it to a temporary address or 0...

Okay, let me rephrase the question: If there exists a pointer 'A' that points to another pointer 'B', which in turn points to a unrelated object 'C', can I use the return function such that the line "return A" will return the whole group? Or is there another way to do such a thing...or none at all? :o

Last edited on
Sorry for the long wait for my reply, I thought they would send me any emails about any new activity...

See the "Tracking options for this topic:" at the bottom of the thread.


I did implement the ptr variable in line 9, didn't I? Unless you mean pointing it to a temporary address or 0...

What he said was: ptr is not modified anywhere in your program... and that's what he meant. It is never set to point to a valid memory address, but you treat it as if it were. Search your code for "setpointer", since that is the only way to modify ptr. Where do you see it called?
Last edited on
Oh wow my bad, thanks for pointing that out. :0
Topic archived. No new replies allowed.