Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// cola dinamica.h
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIN_MEM -1
- #define MIN(x,y) ((x)<(y)?(x):(y))
- typedef struct sNodo
- {
- void *dato;
- unsigned tam;
- struct sNodo *sig;
- }tNodo;
- typedef struct
- {
- tNodo *pri,
- *ult;
- }tCola;
- void crearCola(tCola *c);
- int colaVacia(const tCola *c);
- int colaLlena(const tCola *c,unsigned tam);
- void vaciarCola(tCola *c);
- int ponerEnCola(tCola *c, unsigned tam, const void *dato);
- int sacarDeCola(tCola *c, unsigned tam, void *dato);
- int verPrimero(const tCola *c,unsigned tam, void *dato);
- ///.c
- #include "cDinamica.h"
- void crearCola(tCola *c)
- {
- c->pri=NULL;
- c->ult=NULL;
- }
- int colaVacia(const tCola *c)
- {
- return c->pri==NULL;
- }
- int colaLlena(const tCola *c,unsigned tam)
- {
- tNodo *nue=malloc(sizeof(tNodo));
- if(!nue || !(nue->dato=malloc(tam)))
- {
- free(nue);
- return 1;
- }
- free(nue->dato);
- free(nue);
- return 0;
- }
- int ponerEnCola(tCola *c, unsigned tam, const void *dato)
- {
- tNodo *nue;
- if(!(nue=malloc(sizeof(tNodo))))
- return SIN_MEM;
- if(!(nue->dato=malloc(tam)))
- {
- free(nue);
- return SIN_MEM;
- }
- memcpy(nue->dato,dato,tam);
- nue->tam=tam;
- nue->sig=NULL;
- if(!c->pri)
- c->pri=nue;
- else
- c->ult->sig=nue;
- c->ult=nue;
- return 1;
- }
- int sacarDeCola(tCola *c, unsigned tam, void *dato)
- {
- tNodo *elim=c->pri;
- if(elim)
- return -1;
- memcpy(dato,elim->dato,MIN(elim->tam,tam));
- c->pri=elim->sig;
- if(!c->pri)
- c->ult=NULL;
- free(elim->dato);
- free(elim);
- return 1;
- }
- void vaciarCola(tCola *c)
- {
- tNodo *elim;
- while(c->pri)
- {
- elim=c->pri;
- c->pri=elim->sig;
- free(elim->dato);
- free(elim);
- }
- c->ult=NULL;
- }
- int verPrimero(const tCola *c,unsigned tam, void *dato)
- {
- if(!c->pri)
- return -1;
- memcpy(dato,c->pri->dato,MIN(c->pri->tam,tam));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement