Problem creating circular linked list; possible problem with call-by-ref -- help please

So I'm trying to create a linked list consisting of a class. I'm not using a lot of the functions which I defined but I'm going to post my code in it's entirety anyway. In fact you should ignore the functions "insert", "disp", and "reverse" since they're not being used.

header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

class Node
{
public:
	Node(short number = 0, Node* link = NULL);
	
	void set_link(Node* nodePtr);
	

	short get_number();
	Node* get_link();

private:
	short number;
	Node* link;
};
void reverse(Node*& head);
void insert(Node* after_me, short the_number);
void donut_insert(Node*& head, short the_number);
void disp(Node* head);


function implementation:
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstddef>
#include <string>
#include "test4.h"

using namespace std;

void reverse(Node*& head)
{
	Node* bottomPtr;
	Node* topPtr;
	Node* target;
	Node* new_head;
	target = head;
	if(target != NULL)
	{
		while(target->get_link() != NULL)
		{
			target = target->get_link();
		}
		//target is the last node in the list
		new_head = target;
	}
	else
	{
		return;
	}

	bottomPtr = head;
	while(bottomPtr != target)
	{
		while(bottomPtr != target)
		{
			bottomPtr = bottomPtr->get_link();
		}
		topPtr = head;
		while(topPtr->get_link() != bottomPtr)
		{
			topPtr = topPtr->get_link();
		}
		bottomPtr->set_link(topPtr);
		target = topPtr;
		bottomPtr = head;
	}
	bottomPtr->set_link(NULL);
	head = new_head;
	return;
}

Node::Node(short number, Node* link): number(number), link(link)
{

}

void Node::set_link(Node* nodePtr)
{
	link = nodePtr;
}

void insert(Node* head, short i)
{
	if(head == NULL)
	{
		head = new Node(i);
	}
	else
	{
		Node* back;
		back = head;
		while(back->get_link() != NULL)
		{
			back = back->get_link();
		}
		Node* new_back = new Node(i);
		back->set_link(new_back);
		new_back->set_link(NULL);
	}
}

short Node::get_number()
{
	return number;
}

Node* Node::get_link()
{
	return link;
}

void donut_insert(Node*& head, short the_number)
{
	if(head->get_link() == NULL)
	{
		head = new Node();
		head->set_link(head);
	}
	else
	{
		Node* newNodePtr = new Node(the_number);//points to NULL
		Node* last = head;
		cout << "last address: " << last << "   number: " << last->get_number() << endl;
		while(last->get_link() != head)
		{
			last = last->get_link();
			cout << "last address: " << last << "   number: " << last->get_number() << endl;
		}
		cout << endl;
		last->set_link(newNodePtr);
		newNodePtr->set_link(head);
	}
}

void disp(Node* head)
{
	while(head != NULL)
	{
		cout << "This node: ";
		cout << head->get_number();
		head->set_link(head->get_link());
		string a;
		getline(cin, a);
	}
}


application:
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
#include <iostream>
#include "test4.h"
using namespace std;

int main()
{

	Node* head;
	head = new Node();
	head->set_link(head);
	if(head->get_link() == head)
	{
		cout << "head is linked to head\n";
		cout << "address: " << head << endl;
		cout << "link: " << head->get_link() << endl << endl;
	}
	//disp(head);
	
	for(short i = 1; i < 11; i++)
	{
		donut_insert(head, i);
	}


	short i = 0;
	Node* dispPtr;
	dispPtr = head;
	while(dispPtr != NULL && i<8 && dispPtr->get_link() != head)
	{
		cout << "number: " << dispPtr->get_number() << "    link: " << dispPtr->get_link() << endl;
		dispPtr->set_link(dispPtr->get_link());
		i++;
	}

	cout << "Enter key to exit: ";
	char a;
	cin >> a;

	return 0;
}


The code compiles but the loop on line 28 causes only the first node to be displayed. It's as if though my function "donut_insert" doesn't pass the argument "head" as a reference argument but I think it should because I included &, unless that's not how you do it with pointers. What's wrong with my code?
1
2
3
4
5
6
7
	while(dispPtr != NULL && i<8 && dispPtr->get_link() != head)
	{
		cout << "number: " << dispPtr->get_number() << "    link: " << dispPtr->get_link() << endl;
		//dispPtr->set_link(dispPtr->get_link()); //here you are breaking your structure
		dispPtr = dispPtr->link; //stupid getters and setters that make your members public
		i++;
	}

You weren't updating dispPtr.

You ought to encapsulate the behaviour in a class circular_list. Also, check out for memory leaks


"Enter key to exit: " ¿how many times do you think that I pressed 'Enter'? xP
Last edited on
ne555,

You are absolutely right. Thank you! This is a bit embarrassing to admit but I've spent a whole day on this code (this is my second or third attempt)! what a humbling experience this was. What doesn't kill you makes you stronger I suppose. Thank you for taking the time to sift through all that.
Topic archived. No new replies allowed.