#include #include struct broj { unsigned int *n; struct broj *sled,*preth; }; struct lista { struct broj *pocetak, *kraj; }; void dodaj(struct lista *,unsigned int); void ispisi(struct lista *); void obrisi(struct lista *); void obrisibroj(struct broj *); void obrisiizliste(struct lista *, struct broj *); main() { unsigned int i; struct lista *l; l = (struct lista*) malloc(sizeof(struct lista)); l->pocetak = l->kraj = 0; for(i=0;i<20;i++) { dodaj(l,i); } while(l->kraj) { ispisi(l); putchar('\n'); obrisiizliste(l,l->kraj); } obrisi(l); return 0; } void dodaj(struct lista *l,unsigned int i) { struct broj *b = (struct broj*) malloc(sizeof(struct broj)); b->n = (unsigned int*) malloc(sizeof(unsigned int)); *(b->n) = i; b->sled = 0; if(l->kraj) { l->kraj->sled = b; l->kraj->sled->preth = l->kraj; l->kraj = l->kraj->sled; } else { l->pocetak = l->kraj = b; l->pocetak->preth = 0; } } void ispisi(struct lista *l) { struct broj *p = l->pocetak; while( p ) { printf("%d ",*(p->n)); p = p->sled; } } void obrisi(struct lista *l) { struct broj *t, *p = l->pocetak; while(p) { t = p; p = p->sled; free( t->n ); free( t ); } free(l); } void obrisibroj(struct broj *b) { free(b->n); free(b); } void obrisiizliste(struct lista * l, struct broj * b) { if(!b) return; if(b==l->pocetak && b==l->kraj) { obrisibroj( b ); l->pocetak = l->kraj = 0; return; } if(b==l->pocetak) { l->pocetak = l->pocetak->sled; l->pocetak->preth = 0; } if(b==l->kraj) { l->kraj = l->kraj->preth; l->kraj->sled = 0; } if(b->preth) b->preth->sled = b->sled; if(b->sled) b->sled->preth = b->preth; obrisibroj( b ); }