• Jueves 2 de Mayo de 2024, 21:16

Autor Tema:  Problemilla con Cola  (Leído 954 veces)

Hiro88

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Problemilla con Cola
« en: Lunes 21 de Diciembre de 2009, 19:24 »
0
Estoy haciendo una cola con punteros para el compilador gcc pero no sé muy bien cómo hacer que el puntero del primer elemento y el del último no se muevan a la par cuando hago nuevas inserciones. Por ejemplo, en el programa de prueba que pongo a continuación la salida es la siguiente:
Citar
Ini: 111
Fin: 111
Ini: 222
Fin: 222
Ini: 333
Fin: 333
333 333
333 333
333 333
¿Alguna idea de cómo solucionarlo? Un Saludo.

Código: C
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct {
  6.     char *ruta;
  7.     char *archivo;
  8. } datos;
  9.  
  10. struct cola {
  11.     struct nodo *ini;
  12.     struct nodo *fin;
  13. };
  14.  
  15. struct nodo {
  16.     datos valores;
  17.     struct nodo *sig;
  18. };
  19.  
  20. struct cola *colaVacia(struct cola *c);
  21. int esVacia(struct cola *c);
  22. struct cola *push(struct cola *c,char *r, char *a);
  23. struct cola pop(struct cola *c);
  24. struct nodo *cabeza(struct cola *c);
  25.  
  26. int main(){
  27.     struct cola *c=(struct cola *) malloc(sizeof(struct cola));
  28.     struct nodo *q;
  29.     c=colaVacia(c);
  30.     char *r;
  31.     char *a;
  32.     char *r1;
  33.     char *a1;
  34.     int i;
  35.     r=(char *) malloc(4 * sizeof(char));
  36.     a=(char *) malloc(4 * sizeof(char));
  37.     r1=(char *) malloc(4 * sizeof(char));
  38.     a1=(char *) malloc(4 * sizeof(char));
  39.    
  40.     for(i=0;i<3;i++){
  41.         if(i==0){
  42.             strcpy(r,"111");
  43.             strcpy(a,"111");
  44.         } else if(i==1){
  45.             strcpy(r,"222");
  46.             strcpy(a,"222");
  47.         } else {
  48.             strcpy(r,"333");
  49.             strcpy(a,"333");
  50.         }
  51.         c=push(c,r,a);
  52.         printf("Ini: %sn",c->ini->valores.ruta);
  53.         printf("Fin: %sn",c->fin->valores.ruta);
  54.     }
  55.     while(cabeza(c)!=NULL){
  56.         printf("%s %sn",c->ini->valores.ruta,c->ini->valores.archivo);
  57.         *c=pop(c);
  58.     }
  59. }
  60.  
  61. struct cola *colaVacia(struct cola *c) {
  62.     c->ini==NULL;
  63.     c->fin==NULL;
  64.     return c;
  65. }
  66.  
  67. int esVacia(struct cola *c) {
  68.     return(c->ini==NULL);
  69. }
  70.  
  71. struct cola *push(struct cola *c,char *r, char *a) {
  72.  
  73.     struct nodo *nuevo=(struct nodo *)malloc(sizeof(struct nodo));
  74.    
  75.     nuevo->valores.ruta=r;
  76.     nuevo->valores.archivo=a;
  77.     nuevo->sig=NULL;
  78.     if(esVacia(c)) {
  79.         c->ini=(struct nodo *) malloc(sizeof(struct nodo));
  80.         c->ini=nuevo;
  81.         c->fin=nuevo;
  82.     } else {
  83.         c->fin->sig=(struct nodo *) malloc(sizeof(struct nodo));
  84.         c->fin->sig=nuevo;
  85.         c->fin=nuevo;
  86.        
  87.     }
  88.    return(c);
  89. }
  90.  
  91. struct cola pop(struct cola *c) {
  92.    
  93.     c->ini=c->ini->sig;
  94.     if(c->ini==NULL) {
  95.         c=colaVacia(c);
  96.     }
  97.    
  98.     return(*c);
  99.    
  100. }
  101.  
  102. struct nodo *cabeza(struct cola *c) {
  103.     return(c->ini);
  104. }
  105.