help me with my code about the insertion in a list

Hello, This is for a project my school , it is that given a task that has time (the time it takes to do it ) and gain ( which is money tale wins to make it ) then it will be ordering it , so the task more gain first will do ( anger first) but if they have the same gain is now compared with the time, having more time it will be done first but if times are equal then go first which appeared before the list.

this is my lifted and some tests have already done, example :

aaa February 28
bbb March 20
ccc January 10
*
aaa
ccc
bbb

bbb ccc has more gain but that puts me second

on February 14
b February 30
c May 3
*
b
c
to

here also is not happening.
donut April 50
chocolate January 50
*
donut
chocolate

chocolate January 50
donut April 50
*
donut
chocolate

if well here as they have the same gain but as donut has more hours of first puts chocolate

if you can solve this problem thank you so much , I await your response.

imprimir , is Print my list ( if insertion is for prove is correct )

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
 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; 
					head->sig = n_nuevo;
				}else {
						n_nuevo->sig =head ->sig;
						head->sig = n_nuevo;
				}
			}else {
					n_nuevo->sig= head ->sig ;
					head->sig = n_nuevo;
			}
		}				
	}  
	
		void imprimir3() {							// esto es que me imprime toda la lista, para ver si los insertar funcionan correctamente 
		nodo *ptemp ;
		ptemp = new nodo ();
		ptemp= head ;
		if (head ==NULL)
		{
			cout<< "vacia"<<endl;
		}
		
		while (ptemp!=NULL){
			cout<<ptemp->name <<endl;
			ptemp=ptemp->sig;
		}
	}
Last edited on
Line 31. There is no reason to have that line.


I would implement operator:
bool node::operator< ( const nodo & rhs ) const;

Having that simplifies the code of insert3.
but, that not is the problem, the problem es the orden of the elements of the list.
There is a question: Are nodo a and nodo b in correct order?
The operator< can answer that question.

The insert should use the operator to determine the correct insertion position.

There are three distinct possibilities:
1. The list is empty.
2. Insertion to front.
3. Insertion elsewhere.

The first two you cover, but the third one requires a loop, because you don't know how many nodes the list has.

This keeps ascending order:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ( ! head ) { // #1
  head = tail = n_nuevo;
} else if ( *n_nuevo < * head ) { // #2
  n_nuevo -> sig = head;
  head = n_nuevo;
} else { // #3
  nodo * temp = head;
  while ( temp->sig && ! (*n_nuevo < *(temp->sig) ) ) {
    temp = temp->sig;
  }
  if ( temp->sig ) {
    // insert between temp and temp->sig
  } else {
    // insert at end of list
  }
}

The code above does not care whether the nodos have ganacias, horas or whatnot. The only thing that counts is that the operator< handles all that.


PS, the constructor of nodo should initialize the nodo::sig to nullptr, rather than the code in insert3.
so, if no care ganancia and horas , then, that is the solution to problem with example cases , because if no care horas and ganancia then how the program know where insert the elements ?
The nodo::operator< does care. It computes through the rules that you have and answers either true or false. Write the necessary logic into that function.
Topic archived. No new replies allowed.