problem about infinite loop

the following codes make up the program. when the program run, it happens an infinite loop. this loop is within the operator + in dblinked classe(i have point out it with a comment). I insert a getch() function to avoid program crash.
dbLinked.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include<conio.h>
using namespace std;
template<class T>
class DbLinked
{
	//dato: è la variabile che contiene l'informazione di un certo record
	struct Elemento
	{
			T dato;
			struct Elemento* next;
			struct Elemento* prev;
			Elemento()
			{
				next=0;
				prev=0;
			}
			Elemento(T& elem)
			{
				dato=elem;
				next=0;
				prev=0;
			}
	};
	struct Elemento* start; 
	public: 
	DbLinked()
	{
		this->start=0;
	}
	DbLinked(T& elem)
	{
		this->start=new struct Elemento();
		(this->start)->next=0;
		(this->start)->prev=0;
		(this->start)->dato=elem;
	}	
	~DbLinked()
	{
		struct Elemento* iterator=start;
		struct Elemento* punt;
		while(iterator!=0)
		{
			punt=iterator->next;
			delete(iterator);
			iterator=punt;
		}
		cout<<"\nlista eliminata";
	}
		bool remove(T& elem)
	{
	struct Elemento* iterator;
	if((this->start)->dato==elem)
		{
		iterator=this->start;
		this->start=(this->start)->next;
		if(this->start!=0)
		(this->start)->prev=0;
		delete(iterator);
		return true;
		}
	iterator=start->next;
	while(iterator!=0)
		{			//se il successivo al nodo puntato è uguale ad element
	if(iterator->dato==elem)
			{
			(iterator->prev)->next=iterator->next;
			if(iterator->next!=0)
			(iterator->next)->prev=iterator->prev;
			delete(iterator);
			return true;
			}
	iterator=iterator->next;
		}
	
	return false;
	}
	bool add(T& elem )
	{
	struct Elemento* iterator;
	if(this->start==0) 
		{
		this->start=new struct Elemento();
		(this->start)->dato=elem;
		(this->start)->prev=0;
		(this->start)->next=0;
		return true;
		}	
	iterator=this->start;
	while(iterator!=0)
		{	
	if(iterator->dato==elem) return false;	//non ammessi duplicati
	if(iterator->next==0)
			{
			iterator->next=new struct Elemento(elem);
			(iterator->next)->prev=iterator;
			return true;
			}
	iterator=iterator->next;
		}
return false;
	}
	DbLinked& operator =(const DbLinked& altro) 
	{
		struct Elemento* iterator;
		struct Elemento* iterator2;
		iterator2=altro.start;
		if(this->start==0)
		{
		if(altro.start==0) return *this;
		 this->start=new Elemento(altro.start->dato);
		 iterator2=iterator2->next;
		}
		else if(altro.start!=0){
		 this->start->dato=altro.start->dato;
		 iterator2=iterator2->next;
		 }
		iterator=this->start;
		while(iterator2!=0)
		{
			if(iterator->next!=0) iterator->next->dato=iterator2->dato;
			else{
			iterator->next=new Elemento(iterator2->dato);
			iterator->next->prev=iterator;
			}
		iterator=iterator->next;
		iterator2=iterator2->next;
		}
		while(iterator->next!=0)
		{
			iterator2=iterator->next->next;
			delete(iterator->next);
			iterator->next=0;
			iterator=iterator2;
		}
		return *this;
	}
		DbLinked operator+(const DbLinked& altro)
	{
		DbLinked lista_temp=*this;
		struct Elemento* iterator;
		struct Elemento* punt;
		iterator=altro.start;
		if(lista_temp.start==0)
		{							
		if(altro.start==0) return *this;
		lista_temp.start=new Elemento(altro.start->dato);
		iterator=iterator->next;
		}
		punt=lista_temp.start;
		// infinite loop
		while(iterator!=0)
		{
			getch();
			if(punt->next==0)
			{
			punt->next=new Elemento(iterator->dato);
			punt->next->prev=punt;
			iterator=iterator->next;
			}
			punt=punt->next;
		}
		return lista_temp;	
	}
	DbLinked operator|(const DbLinked& destra)
	{
		DbLinked lista_temp;
		struct Elemento* iterator;
		iterator=this->start;
			while(iterator!=0)
		{
			lista_temp.add(iterator->dato);
			iterator=iterator->next;
		}
		iterator=destra.start;
		while(iterator!=0)
		{
			lista_temp.add(iterator->dato);
			iterator=iterator->next;
		}
		return lista_temp;
	}
	 friend ostream& operator<<(ostream& output,const DbLinked& lista)
	{
		int i=0;
		struct Elemento* iterator;
		iterator=lista.start;
		
	while(iterator!=0)
		{	
	output<<(i+1)<<". "<< iterator->dato<<"\n";	
	i++;
	iterator=iterator->next;
		}
	if(i==0) output << "non ci sono elementi nella lista";
	}
	
	
};


Persona.h
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<cstring>
#include<iostream>
#include "Contatto.h"
using namespace std;
class Persona
{
	char* nome;
	char* cognome;
	char* indirizzo;
 DbLinked<Contatto> contatti;
	
	public:
		Persona()
		{
			nome=0;
			cognome=0;
			indirizzo=0;
		}
		Persona(char* n,char* c,char* ind)
		{
			this->nome=new char[strlen(n)+1];
			this->cognome=new char[strlen(c)+1];
			this->indirizzo=new char[strlen(ind)+1];
			strcpy(this->nome,n);
			strcpy(this->cognome,c);
			strcpy(this->indirizzo,ind);
			
		}
		~Persona()
		{
			delete[](nome);
			delete[](cognome);
			delete[](indirizzo);
			cout<<"\npersona eliminata";
			
		}
		void aggiungicontatto(tipo_contatto t,char* num)
		{
			Contatto nuovo_contatto(t,num);
			 this->contatti.add(nuovo_contatto);
		}		
		Persona& operator = (const Persona& altro)
		{
		if(this==&altro) return *this;
		delete[](this->nome);
		delete[](this->cognome);
		delete[](this->indirizzo);
		this->nome=new char[strlen(altro.nome)+1];
		this->cognome=new char[strlen(altro.cognome)+1];
		this->indirizzo=new char[strlen(altro.indirizzo)+1];
		this->contatti=altro.contatti;
		strcpy(this->nome,altro.nome);
		strcpy(this->cognome,altro.cognome);
		strcpy(this->indirizzo,altro.indirizzo);
		return *this;
		
		}
		 bool operator == (const Persona& altro)
		{
			return (!strcmp(this->nome,altro.nome)&&!strcmp(this->cognome,altro.cognome)&&!strcmp(this->indirizzo,altro.indirizzo));
		}
		friend ostream& operator <<(ostream& output,const Persona& persona) 
		{
			output<<"Nome: "<< persona.nome<<"\nCognome: "<< persona.cognome<<"\nIndirizzo: "<<persona.indirizzo << "\nContatti:"<<"\n"<< persona.contatti;
			return output;
		}
		
		
};


Contatto.h
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
#include<cstring>
#include<iostream>
enum tipo_contatto{CELLULARE,CASA,UFFICIO,FAX,EMAIL};
class Contatto
 {
		tipo_contatto tipo;
		char* numero;
		public:
		Contatto()
		{
			tipo=CELLULARE;
			numero=0;
		}
		Contatto(tipo_contatto t,char* num)
		{
		tipo=t;
		numero=new char[strlen(num)+1];
		strcpy(numero,num); 
		}
		~Contatto()
		{
			if(this->numero!=0)
			delete[](numero);
		}
		Contatto& operator =( const Contatto& altro)
		{
			if(this==&altro) return *this;
			this->tipo=altro.tipo;
			if(this->numero!=0)
			delete[](this->numero);
			this->numero=new char[strlen(altro.numero)+1];
			strcpy(this->numero,altro.numero);
			return *this;
		}
	 friend ostream& operator <<(ostream& output,const Contatto& stampare)
		{
			switch(stampare.tipo)
			{
				case CASA: output <<"CASA:";
				break;
				case CELLULARE: output<<"CELLULARE:";
				break;
				case UFFICIO: output<<"UFFICIO:";
				break;
				case EMAIL: output<<"EMAIL:";
				break;
				default: output<<"unidefined:";
			}
		output<< stampare.numero;
			return output;
		
		}
		bool operator == (const Contatto& altro)
		{
			return (strcmp(this->numero,altro.numero)==0);
		}
		
};


main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Persona.h"
#include<conio.h>
using namespace std;
int main()
{
	Persona a("Simone","Clementi","padre");
	a.aggiungicontatto(CASA,"355533");
	a.aggiungicontatto(CELLULARE,"3493392928");
	a.aggiungicontatto(EMAIL,"simone@clementi.it");
	Persona b("Gianluca","Maraschio","matteotti");
	Persona c("Greta","Affricani","delmare");
	DbLinked<Persona>lista(a);
	DbLinked<Persona>lista2(b);
	DbLinked<Persona>lista3(c);
	lista.add(b);
	cout <<"\n lista di persone:\n" << lista;
	cout<< "\n funziona l'operatore =\n";
	lista=lista+lista;
	cout << "\n non funziona l'operatore +\n"<<lista;
	getch();
	return 0;
}

Last edited on
The problem is in your dblinked.h file.

Line 155 to 159 is within an if statement and that is also where you are moving the pointer which the outer while loop depends on. I don't know exactly what you plan to do with that block of code, but you should review it and determine that what it is doing is what you want it to be doing
Topic archived. No new replies allowed.