Programación General > C/C++
Problemilla con Cola
(1/1)
Hiro88:
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
--- Fin de la cita ---
¿Alguna idea de cómo solucionarlo? Un Saludo.
--- Código: C ---#include <stdio.h>#include <string.h>#include <stdlib.h> 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);struct nodo *cabeza(struct cola *c); 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: %sn",c->ini->valores.ruta); printf("Fin: %sn",c->fin->valores.ruta); } while(cabeza(c)!=NULL){ printf("%s %sn",c->ini->valores.ruta,c->ini->valores.archivo); *c=pop(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) { c->ini=c->ini->sig; if(c->ini==NULL) { c=colaVacia(c); } return(*c); } struct nodo *cabeza(struct cola *c) { return(c->ini);}
Navegación
Ir a la versión completa