help me doubly linked list

Hi this is my code, and the program do insertion in both side of the list, and The First INSERTS INSERTS That is on the left and then on the Right and Now repeat the process again , for example if I enter ABC CBA should print it right? But if I type 1 , only B2 , C 3. AC prints , I've been watching and testing One Hour nodes in my notebook but do not know why you do that, help me please.

my class node , my action insert, and my int main()

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
 lass 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 list2 
{
	public:
	nodito *prim;
	nodito *ulti;
	int hhh,acum;
	
	list2 (){
		hhh = -1;
		acum=0;
		prim =NULL;
		ulti= NULL;
	}
	
	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) {
			n_new -> prox = prim;
			n_new -> prev = prim -> prev;
			prim -> prev = n_new;
			n_new = prim;
		}else {
			n_new ->prox =ulti ->prox;
			n_new -> prev = ulti;
			ulti -> prox= n_new;
			n_new =ulti;
		}	
	}
	}
	
	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;
		}
		
	}
	
};

int main ()
{
	list2 m;									// lista del hogar
	int hor, acumulador;
	string nnnn=" ";
	acumulador = 0;
	
	while ( nnnn != "*" ){
		cin>> nnnn;
		if ( nnnn == "*" ){
			m.imprimir2();}
		else{
			cin>> hor;
				acumulador= acumulador + 1; 
			m.insert2( hor, nnnn, acumulador);
		}
		
	}
return 0;
}
closed account (D80DSL3A)
1) I think a problem is on lines 43,44 with an extra { brace.
1
2
}else{    //<- this one
			if (acum % 2 == 0) {

You may want just 'else if' there
 
}else if (acum % 2 == 0) {

It was the extra } brace on line 56 which drew my attention to this.

2) You don't ++acum; anywhere so the insertion operations won't alternate. acum%2==0 always in your code.
Nevermind. I see it's done in main() and then passed to the insert().

3) Line 48 and 53 assignments are backwards.

4) lines 46 and 50. RHS should = NULL. If not for other errors they would though, so maybe not a problem.

5) Nit-picking here. Lines 39 and 40. These assignments aren't necessary as the modito constructor already assigns these values.

I speak only English but I'm amazed at language similarities sometimes.
eg. prim (primary?) and ulti (ultimate?) are close in meaning to 'first' and 'last', so they are sensible to me.

EDIT: A better modito constructor can make most of your code in the insert() go away. Ask how if you wish to know.
Last edited on
thank you un2code (1655)

so then I change the second else for else if, y eso podria arreglarme el problema, them acum , is for that the numbers "par" (0,2,4,6) insertion to right and number "impar" (1,3,5,7) insertion to right

in the lines 48 and 53 you tell that : 48 prim=n_new; and 53 ulti=n_new; ??

with that corrections the code, insert alternatively , example; A B AND C : print C B A ??

and yes, is funny how the english and the spanish have some similarities :) .

Topic archived. No new replies allowed.