Pascal/Turbo Pascal - insertar ordenado en una lista.

 
Vista:
sin imagen de perfil

insertar ordenado en una lista.

Publicado por Diego (98 intervenciones) el 18/10/2013 22:49:10
Buenas tardes, no veo claramente cual es el problema que tengo al crear dos listas ordenanas por un criterio. hice dos lista que insertan ordenadamente, pero parece que solo inserta si los criterios para la inserción van de mayor a menor, al intentar cargar datos en otro orden me da error.
Utilizo el Dev-Pascal
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
program ejercicio5;
type
  info_productos = record
    cod_pro : integer;
    nom_pro : string;
    stock_actual : integer;
    stock_minimo : integer;
    precio_unitario : real;
  end;
  lista_productos = ^nodo_pro;
  nodo_pro = record
    elemen_pro : info_productos;
    dirSig_pro : lista_productos;
  end;
  info_ventas = record
    num_venta : integer;
    cod_pro: integer;
    cant_vendida : integer;
  end;
  lista_ventas = ^nodo_vent;
  nodo_vent = record
    elemen_vent : info_ventas;
    dirSig_vent : lista_ventas;
  end;
  info_arbol = record
    cod_pro : integer;
    stock_a_reponer : integer;
  end;
  T_arbol = ^nodo_arbol;
  nodo_arbol = record
    hijo_der : T_arbol;
    elemen_arbol : info_arbol;
    hijo_izq : T_arbol;
  end;
procedure crear_lista_pro (var L : lista_productos);
  begin
    L:= nil;
  end;
procedure crear_lista_vent(var L : lista_ventas);
  begin
    L := nil;
  end;
procedure crear_arbol(var a: T_arbol);
  begin
    a:= nil;
  end;
procedure leer_info_pro(var reg_pro : info_productos);
  begin
    with (reg_pro) do
      begin
        write('Ingrese el código del producto: ');
        readln(cod_pro);
        if (cod_pro <> -1) then
          begin
            write('Ingrese el nombre del producto: ');
            readln(nom_pro);
            write('Ingrese el stock actual del producto: ');
            readln(stock_actual);
            write('Ingrese el stock minimo producto: ');
            readln(stock_minimo);
            write('Ingrese el precio unitario del producto: ');
            readln(precio_unitario);
          end;
      end;
  end;
procedure insertar_ord_pro(var list_pro : lista_productos; reg_pro: info_productos);
  var
    nue: lista_productos;
    ant, act : lista_productos;
  begin
    new(nue);
    nue^.elemen_pro := reg_pro;
    nue^.dirSig_pro := nil;
    if (reg_pro.cod_pro < list_pro^.elemen_pro.cod_pro) then
      begin
        nue^.dirSig_pro := list_pro;
        list_pro := nue;
      end
    else
      begin
        act := list_pro;
        ant := act;
        while ((act <> nil) and (reg_pro.cod_pro > act^.elemen_pro.cod_pro)) do
          begin
            ant := act;
            act := act^.dirSig_pro;
          end;
        if (reg_pro.cod_pro < act^.elemen_pro.cod_pro) then
            nue^.dirSig_pro := ant^.dirSig_pro;
        ant^.dirSig_pro := nue;
      end;
  end;
procedure cargar_lista_productos(var list_pro : lista_productos);
  var
    reg_pro : info_productos;
  begin
    leer_info_pro(reg_pro);
    if (reg_pro.cod_pro <> -1) then
      begin
        new(list_pro);
        list_pro^.elemen_pro := reg_pro;
        list_pro^.dirSig_pro := nil;
        leer_info_pro(reg_pro);
        while (reg_pro.cod_pro <> -1) do
        begin
          insertar_ord_pro(list_pro, reg_pro);
          leer_info_pro(reg_pro);
        end;
      end;
  end;
procedure leer_info_vent(var reg_vent: info_ventas);
  begin
    with (reg_vent) do
      begin
        write('Ingrese el código del producto: ');
        readln(cod_pro);
        if (cod_pro <> -1) then
          begin
            write('Ingrese el número de venta: ');
            readln(num_venta);
            write('Ingrese la cantidad vendida: ');
            readln(cant_vendida);
          end;
      end;
  end;
procedure insertar_ord_vent(var list_vent: lista_ventas; reg_vent: info_ventas);
  var
    nue: lista_ventas;
    ant, act : lista_ventas;
  begin
    new(nue);
    nue^.elemen_vent := reg_vent;
    nue^.dirSig_vent := nil;
    if (reg_vent.cod_pro < list_vent^.elemen_vent.cod_pro) then
      begin
        nue^.dirSig_vent := list_vent;
        list_vent := nue;
      end
    else
      begin
        act := list_vent;
        ant := act;
        while (act <> nil) and (reg_vent.cod_pro > act^.elemen_vent.cod_pro) do
          begin
            ant := act;
            act:= act^.dirSig_vent;
          end;
        if (reg_vent.cod_pro < act^.elemen_vent.cod_pro) then
          nue^.dirSig_vent := ant^.dirSig_vent;
        ant^.dirSig_vent := nue;
      end;
  end;
procedure cargar_lista_ventas(var list_vent: lista_ventas);
  var
    reg_vent : info_ventas;
  begin
    leer_info_vent(reg_vent);
    if (reg_vent.cod_pro <> -1) then
      begin
        new(list_vent);
        list_vent^.elemen_vent := reg_vent;
        list_vent^.dirSig_vent := nil;
        leer_info_vent(reg_vent);
        while (reg_vent.cod_pro <> -1) do
          begin
            insertar_ord_vent(list_vent, reg_vent);
            leer_info_vent(reg_vent);
          end;
      end;
  end;
procedure imprimir_lista_productos(L : lista_productos);
  begin
    while (L <> nil) do
      begin
        writeln('Código del producto: ', L^.elemen_pro.cod_pro);
        writeln('Nombre del producto: ', L^.elemen_pro.nom_pro);
        writeln('Stock actual del producto: ', L^.elemen_pro.stock_actual);
        writeln('Stock minimo del producto: ', L^.elemen_pro.stock_minimo);
        writeln('Precio unitario del producto: ', L^.elemen_pro.precio_unitario);
        writeln();
        L := L^.dirSig_pro;
      end;
  end;
procedure imprimir_lista_ventas(L : lista_ventas);
  begin
    while (L <> nil) do
      begin
        writeln('Código del producto: ', L^.elemen_vent.cod_pro);
        writeln('Número de venta del producto: ', L^.elemen_vent.num_venta);
        writeln('Cantidad vendida del producto: ', L^.elemen_vent.cant_vendida);
        writeln();
        L := L^.dirSig_vent;
      end;
  end;
var
  list_pro : lista_productos;
  list_vent : lista_ventas;
begin
  crear_lista_pro(list_pro);
  crear_lista_vent(list_vent);
  writeln('---------------Lista de Productos--------------');
  cargar_lista_productos(list_pro);
  writeln('------------------------X----------------------');
  writeln('---------------Lista de Ventas--------------');
  cargar_lista_ventas(list_vent);
  writeln('------------------------X----------------------');
  writeln('---------------Lista de Productos--------------');
  imprimir_lista_productos(list_pro);
  writeln('------------------------X----------------------');
  writeln('---------------Lista de Ventas--------------');
  imprimir_lista_ventas(list_vent);
  writeln('------------------------X----------------------');
  readln();
  readln();
end.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

insertar ordenado en una lista.

Publicado por Diego (98 intervenciones) el 19/10/2013 21:54:52
Muchas gracias a los que se interesaron, ya me salió y me di cuenta cual era mi error, no tuve en cuanta que para realizar la comparacion de codigos, la direccion tiene que ser distinta de nil, sino hará la comparación de un numero con una direccion de memoria.

Lo solucioné haciendo este modulo:
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
procedure insertar_ord_vent(var pri_nodo: lista_ventas; reg: info_ventas);
  var
    nue, aux, ant : lista_ventas;
  begin
    new(nue);
    nue^.elemen_vent := reg;
    nue^.dirSig_vent := nil;
    if (reg.cod_pro < pri_nodo^.elemen_vent.cod_pro) then
      begin
        nue^.dirSig_vent := pri_nodo;
        pri_nodo := nue;
      end
    else
      begin
         aux:= pri_nodo;
         ant := aux;
         while ((aux <> nil) and (reg.cod_pro >= aux^.elemen_vent.cod_pro)) do
           begin
             ant := aux;
             aux := aux^.dirSig_vent;
           end;
         ant^.dirSig_vent := nue;
         if not(aux = nil) then
           nue^.dirSig_vent := aux;
      end;
  end;

y uno igual para los productos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

insertar ordenado en una lista.

Publicado por ramon (2158 intervenciones) el 20/10/2013 13:27:50
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
{Mira Por si Acaso te sirve}
 
 uses
      crt;
 
   type
   info_productos = record
           cod_pro : integer;
           nom_pro : string;
      stock_actual : integer;
      stock_minimo : integer;
   precio_unitario : real;
      end;
 
  lista_productos = ^nodo_pro;
         nodo_pro = record
         elemen_pro : info_productos;
         dirSig_pro : lista_productos;
      end;
 
  var
    prime, anter, ultimo, actual : lista_productos;
    producto : info_productos;
 
 
 
    procedure leer_info_pro(var reg_pro : info_productos);
    begin
      clrscr;
      writeln('*** Entrada Datos Producto Termina Entradas [-1] ***');
      writeln;
      with (reg_pro) do
      begin
          write('   Ingrese el codigo del producto          : ');
        readln(cod_pro);
        if (cod_pro <> -1) then
        begin
          write('   Ingrese el nombre del producto          : ');
          readln(nom_pro);
          write('   Ingrese el stock actual del producto    : ');
          readln(stock_actual);
          write('   Ingrese el stock minimo producto        : ');
          readln(stock_minimo);
          write('   Ingrese el precio unitario del producto : ');
          readln(precio_unitario);
       end;
     end;
  end;
 
   procedure insertaprimero(reg_pro : info_productos);
   var
      pt : lista_productos;
    begin
        new(pt);
        pt^.elemen_pro := reg_pro;
        pt^.dirSig_pro := prime;
        prime := pt;
    end;
 
    procedure insertafinal(reg_pro : info_productos);
    var
      pt : lista_productos;
    begin
        new(pt);
        pt^.elemen_pro := reg_pro;
        actual^.dirSig_pro := pt;
        pt^.dirSig_pro := nil;
        actual := pt;
    end;
 
    procedure insertamosporordendebalor(reg_pro : info_productos);
    var
      rr, pp, pt : lista_productos;
      nofin, salir : boolean;
    begin
       if reg_pro.cod_pro <> -1 then
       begin
       if prime = nil then
       begin
         new(actual);
         actual^.elemen_pro := reg_pro;
         prime := actual;
         actual^.dirSig_pro := nil;
       end
     else
       begin
       new(pt);
       pt^.elemen_pro := reg_pro;
       pp := prime;
       rr := prime;
       if pp^.elemen_pro.cod_pro > reg_pro.cod_pro then
       begin
          insertaprimero(reg_pro);
       end
     else
         begin
         salir := false;
         nofin := false;
     repeat
         rr := pp;
         pp := pp^.dirSig_pro;
         if pp^.elemen_pro.cod_pro > reg_pro.cod_pro then
         begin
            salir := true;
            nofin := true;
         end;
     until (pp^.dirSig_pro = nil) or (salir = true);
      if (salir = false) and (nofin = false) then
      begin
         insertafinal(reg_pro);
      end
    else
       begin
          pt^.dirSig_pro := pp;
          rr^.dirSig_pro := pt;
        end;
      end;
    end;
   end;
 end;
 
  procedure listardatos;
    var
      ver : lista_productos;
    begin
       clrscr;
       writeln('  Cidigo   Nombre          Actual   Minimo   Precio');
       ver := prime;
       while ver <> nil do
       begin
          with ver^.elemen_pro do
 writeln('   ',cod_pro,'       ',nom_pro,'             ',stock_actual,'       ',
        stock_minimo,'       ',precio_unitario:0:2);
          ver := ver^.dirSig_pro;
        end;
        writeln;
        writeln('   Pulse Una Tecla');
     end;
 
   procedure insertar_ord_pro(var list_pro : lista_productos;
                                 reg_pro : info_productos);
  var
     nue : lista_productos;
     ant, act : lista_productos;
    begin
      new(nue);
      nue^.elemen_pro := reg_pro;
      nue^.dirSig_pro := nil;
    if (reg_pro.cod_pro < list_pro^.elemen_pro.cod_pro) then
    begin
        nue^.dirSig_pro := list_pro;
        list_pro := nue;
     end
  else
     begin
        act := list_pro;
        ant := act;
     while ((act <> nil) and (reg_pro.cod_pro > act^.elemen_pro.cod_pro)) do
     begin
       ant := act;
       act := act^.dirSig_pro;
     end;
    if (reg_pro.cod_pro < act^.elemen_pro.cod_pro) then
     nue^.dirSig_pro := ant^.dirSig_pro;
     ant^.dirSig_pro := nue;
    end;
  end;
 
 
  begin
      prime := nil;
     while producto.cod_pro <> -1 do
     begin
      leer_info_pro(producto);
      insertamosporordendebalor(producto);
     end;
      listardatos;
      readkey;
  end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

insertar ordenado en una lista.

Publicado por Diego (98 intervenciones) el 21/10/2013 00:55:48
Gracias, ya lo resolví.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar