El código de la cola:
Código:
El código del programa de prueba: typedef struct { char *ruta; char *archivo; } datos; struct cola { struct nodo *ini; struct nodo *fin; }; struct nodo { datos valores; struct nodo *sig; }; struct cola *colaVacia(struct cola *c); int esVacia(struct cola *c); struct cola *push(struct cola *c,char *r, char *a); struct cola pop(struct cola *c/*,char **r, char **a*/); struct nodo *cabeza(struct cola *c); struct cola *colaVacia(struct cola *c) { c->ini=NULL; c->fin=NULL; return c; } int esVacia(struct cola *c) { return(c->ini==NULL); } struct cola *push(struct cola *c,char *r, char *a) { struct nodo *nuevo=(struct nodo *)malloc(sizeof(struct nodo)); nuevo->valores.ruta=r; nuevo->valores.archivo=a; nuevo->sig=NULL; if(esVacia(c)) { c->ini=(struct nodo *) malloc(sizeof(struct nodo)); c->ini=nuevo; c->fin=nuevo; } else { c->fin->sig=(struct nodo *) malloc(sizeof(struct nodo)); c->fin->sig=nuevo; c->fin=nuevo; } return(c); } struct cola pop(struct cola *c/*,char **r, char **a*/) { c->ini=c->ini->sig; if(c->ini==NULL) { c=colaVacia(c); } return(*c); } struct nodo *cabeza(struct cola *c) { return(c->ini); }
Código:
La salida que da es esta:#include <stdio.h> #include <string.h> #include <stdlib.h> #include "cola.h" int main(){ struct cola *c=(struct cola *) malloc(sizeof(struct cola)); struct nodo *q; c=colaVacia(c); char *r; char *a; char *r1; char *a1; int i; r=(char *) malloc(4 * sizeof(char)); a=(char *) malloc(4 * sizeof(char)); r1=(char *) malloc(4 * sizeof(char)); a1=(char *) malloc(4 * sizeof(char)); for(i=0;i<3;i++){ if(i==0){ strcpy(r,"111"); strcpy(a,"111"); } else if(i==1){ strcpy(r,"222"); strcpy(a,"222"); } else { strcpy(r,"333"); strcpy(a,"333"); } c=push(c,r,a); printf("Ini: %s\n",c->ini->valores.ruta); printf("Fin: %s\n",c->fin->valores.ruta); } %s\n",c->ini->valores.ruta,c->ini->valores.archivo); while(cabeza(c)!=NULL){ printf("%s %s\n",c->ini->valores.ruta,c->ini->valores.archivo); *c=pop(c); } }
Código:
Un saludo y gracias. PD: compilo en el compilador de GNU (gcc) Ini: 111 Fin: 111 Ini: 222 Fin: 222 Ini: 333 Fin: 333 333 333 333 333 333 333