Help with a insertion of list

Hi everyone, so my problem with my code is that the class nodo, when I compile my code show me that the nodo is the problem, so not understand why, because and use two class node (1 for the list simple that are the list 1, 2 and 3) and class nodito for the doubly linked list (list 4) and a friend tell me that with these 2 class node my program will be fine , but and don't know who is the problem.

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
 
//jose luis silva cedula:24900481 seccion: c3

#include <iostream>
#include <string>
using namespace std;

class nodito {
	public:
	int info;
	string nnombre;
	nodito *prev;
	nodito *prox;
	
	nodito (){}
	
	nodito ( int informacion , string nomb ) 
	{
		info = informacion;
		nnombre =nomb;
		prev = NULL;
		prox = NULL;
	}
	
	~nodito () {}
};

class nodo 
{
	public:
	int hora;
	string name;
	int ganancia;
	nodo *sig ;
	
	nodo (){}
	
	nodo (int hh, string nombrecito) 										//constructor 
	{
		hora = hh;
		name = nombrecito;
		sig=NULL;
	}
	
	nodo (int hh, string nname, int ga) 										//constructor 
	{
		hora = hh;
		name = nname;
		ganancia = ga;
		sig=NULL;
	}
	
	~nodo(){}
};

class list1				// clase que va a insertar al principio (pila) 
 {
	public:
	nodo *pfirst ;
	nodo *plast ;
	int aa;
	
	public:
	
	list1 () 
	{
		aa=-1;
		pfirst= NULL;
		plast = NULL;
	} 
	
	~list1 (){}
	
	void insert1 (int aa, string nn) 
	{
		nodo *nuevo;
		nuevo = new nodo (aa, nn);
		nuevo -> sig = NULL;
		if (pfirst == NULL) {
			pfirst= nuevo;
			plast= nuevo; 
		}else{
			nuevo->sig = pfirst;
			pfirst=nuevo;
		}
	}
	
	string pop (){
			nodo *temp;
			temp = new nodo ();
			temp = pfirst;
			string valor = pfirst->name;
			pfirst = pfirst ->sig;
			delete (temp); 
			return valor;	
		}
	
	void imprimir1 () {
		
			while (pfirst != NULL){
				cout<<pfirst->name<<endl;
				pfirst= pfirst->sig;
			}
			}
}; 

class list2 
{
	public:
	nodito *prim;
	nodito *ulti;
	int hhh,acum;
	
	list2 (){
		hhh = -1;
		acum=0;
		prim =NULL;
		ulti= NULL;
	}
	
	~list2 (){}
	void insert2 ( int hhh, string nombre_tar , int acum){
		
		nodito *n_new;
		n_new= new nodito (hhh, nombre_tar);
		n_new -> prox = NULL;
		n_new -> prev = NULL;
		if (( prim ==NULL) and (ulti ==NULL)){
			prim = ulti= n_new;
		}else if (acum % 2 != 0) {								// si es impar lo coloca a la derecha
			n_new -> prox = prim;
			n_new -> prev = prim -> prev;
			prim -> prev = n_new;
			prim =n_new;
		}else {														//si es par lo coloca a la derecha
			n_new ->prox =ulti ->prox;
			n_new -> prev = ulti;
			ulti -> prox= n_new;
			ulti= n_new;
		}	
	
	}
	
	void imprimir2 () {
		nodito *ayuda ;
		ayuda = new nodito ();
		ayuda= prim ;
		if (prim ==NULL)
		{
			cout<< "vacia"<<endl;
		}
		
		while (ayuda!=NULL){
			cout<<ayuda->nnombre<<endl;
			ayuda=ayuda->prox;
		}
	}
	
};

class list3 {
	public:
	nodo *head;
	nodo *tail;
	int tiempo, dinero;
	
	list3 () {
		tiempo = -1;
		dinero = -1;
		head = NULL;
		tail= NULL;
	}
	
	void insertar3 ( string tarea , int dinero , int tiempo) {
		nodo *n_nuevo;
		n_nuevo = new nodo (tarea, dinero, tiempo);
		n_nuevo ->sig = NULL;
		if (head == NULL){														// si es vacia
			head = tail = n_nuevo;
		}else {
			if ( n_nuevo -> ganancia > head->ganancia ){						// si la ganacia es mayor
				n_nuevo -> sig = head;
				head = n_nuevo;
			}else if (n_nuevo ->ganancia == head->ganancia){											// si la ganancia es igual
				if (n_nuevo->hora > head->hora) {  														// si la hora es mayor 
					n_nuevo->sig = head;
					head =n_nuevo;
				}else if (n_nuevo ->hora == head->hora ){										// si las horas son iguales ira despues que la que esta primera 
					n_nuevo->sig =head->sig; 
				}else {
					while (n_nuevo-> hora < head ->hora ) {
						n_nuevo =head ->sig;
						head->sig = n_nuevo;
					}
				}
			}else {
				while (n_nuevo->ganancia < head ->ganancia) {
					n_nuevo= head ->sig ;
					head->sig =n_nuevo;
				}
			}
		}				
	}  
};

class list4 					// clase que se insertar por como llegan las tareas (cola) 
{
	public :
	nodo *pprimero;
	nodo *pultimo;
	int xx;
	
	public:
	
	list4 ()
	{
			xx=-1;
			pprimero = NULL;
			pultimo =NULL;
	}
	
	~list4 (){}
	
	void insert4 ( int xx, string ff )
	{
		nodo *pnew;
		pnew = new nodo (xx, ff);
		pnew->sig= NULL;
		if (pprimero == NULL) {
			pprimero =pultimo= pnew;
	
		}else{
			
			pultimo->sig= pnew;
			pultimo =pnew;
		}
		
	}
	
	void eliminar4 (){
		
	}
	
	void imprimir4 ()
	{
		nodo *pa ;
		pa = new nodo ();
		pa= pprimero ;
		if (pprimero ==NULL)
		{
			cout<< "vacia"<<endl;
		}
		
		while (pa!=NULL){
			cout<<pa->name<<endl;
			pa=pa->sig;
		}
	}
	
};


the problem is the list 3, that list is that a new element in the list compare their gain (ganancia in spanish) and which has more gain will be done first, if the gain is equal , then Compare the time, if the time is greater then will be done first, and if the time is equal , then the task anger appreciation first on the list first.
Last edited on
Line 15: initialize prev and prox to NULL or they will contain garbage.
Line 36: initialize sig to NULL for the same reason.
Line 72: You are leaking the contents of the list. You need to walk through the list and delete the items.
Line 90: delete this line. You allocate a nodo and then overwrite the only pointer to it on the next line, so you're leaking the memory.
Line 93: You may need to update plast here too.
Lines 100-102. You're using the pfirst pointer to iterate through the list, so when the method exits, pfirst will be NULL. Use a separate local variable to walk the list. pfirst should always point to the first item.
Line 121: same comment as line 72
Line 133: You need to update prim->prev->prox before this line
Line 138: You need to update ulti->prox->prev before this line
Line 146: Delete (same comment as line 90)

Now for the question you're asking about. Look at line 192 and 193. The first time through the loop you set n_nuevo = head->sig, thus leaking the original value of n_nuevo. Line 193 never changes the value of head->sig. The loop probably just continues for ever after that. Lines 198 and 199 are the same.

In addition, you aren't adjusting the tail pointer.

To fix this, I think you should start by creating a less-than operator for nodo that expresses the way they compare to each other. Once that's done you can use a nice little trick to insert into a linked list. Rather than keeping a pointer to the previous node in the list, you keep a pointer to the pointer to the node. In other words, a pointer to either head, or the previous node's next pointer. Doing this, the whole inserting process gets really easy:
1
2
3
4
5
6
7
8
9
10
11
12
13
void insertar3 ( string tarea , int dinero , int tiempo) {
    nodo *n_nuevo = new nodo (tarea, dinero, tiempo);
    nodo **pp, *p;

    for (pp = &head; (p=*pp); pp = &p->next) {
	if (*n_nuevo > *p) break;
    }
    n_nuevo->sig = *pp;
    *pp = n_nuevo;
    if (tail == NULL || pp = &tail->sig) {
	tail = n_nuevo;
    }
}  

well, I don't very time but thnk you for the problems, the list now is:

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
void insert3 ( string tarea , int tiempo, int dinero) {
		nodo *n_nuevo;
		n_nuevo = new nodo (tarea, tiempo, dinero );
		n_nuevo ->sig = NULL;
		if (head == NULL){														// ve si es vacia
			head = tail = n_nuevo;
		}else {
			if ( n_nuevo -> ganancia > head->ganancia ){						// si la ganacia es mayor
				n_nuevo -> sig = head;
				head = n_nuevo;
			}else if (n_nuevo ->ganancia == head->ganancia){											// si la ganancia es igual
				if (n_nuevo->hora > head->hora) {  														// si la hora es mayor 
					n_nuevo->sig = head;
					head =n_nuevo;
				}else if (n_nuevo ->hora == head->hora ){										// si las horas son iguales ira despues que la que esta primera 
					n_nuevo->sig =head->sig; 
				}else {
						n_nuevo =head ->sig;
						head->sig = n_nuevo;
				}
			}else {
					n_nuevo= head ->sig ;
					head->sig =n_nuevo;
	
			}
		}				
	}  


and I prove with :
aaa 2 10
bbb 1 40
ccc 3 50
*
and my console:
ccc
bbb
aaa
that is right, because the gain of ccc is 50 is the big, but in other example: aaa 2 50
bbb 2 30
ccc 1 15
*
and print aaa , is how if only have the element with more gain, and no print other

Topic archived. No new replies allowed.