#include #define PRIO_MAX 0 #define PRIO_MIN 100 struct registro{ int prio; char nome[30]; struct registro *prox; struct registro *ant; }; struct registro *inicio = NULL; struct registro *fim = NULL; void insere( int p, char *n) { struct registro *x, *novo; if( (novo = (struct registro *) malloc( sizeof(struct registro))) == NULL ) { printf("Erro na alocacao\n"); exit(1); } novo->prio = p; strcpy( novo->nome, n); if( inicio == NULL ) // sera o unico { novo->prox = NULL; novo->ant = NULL; inicio = novo; fim = novo; return; } x = inicio; while( x != NULL && p >= x->prio ) x = x->prox; if( x == NULL ) // sera o ultimo { novo->prox = NULL; novo->ant = fim; fim = novo; novo->ant->prox = novo; } else if( inicio == x ) // sera o primeiro { novo->prox = inicio; novo->ant = NULL; inicio = novo; novo->prox->ant = novo; } else // fica no meio { novo->prox = x; novo->ant = x->ant; x->ant = novo; novo->ant->prox = novo; } } int retira( char *n) { struct registro *x; x = inicio; while( x != NULL && (strcmp( n, x->nome) != 0) ) x = x->prox; if( x == NULL ) return -1; if( x->ant == NULL ) inicio = x->prox; else x->ant->prox = x->prox; if( x->prox == NULL ) fim = x->ant; else x->prox->ant = x->ant; free(x); return 0; } void lista( void) { struct registro *x; printf("DIRETO\n"); x = inicio; while( x != NULL ) { printf("prio <%d> nome <%s>\n", x->prio, x->nome); x = x->prox; } printf("INVERSO\n"); x = fim; while( x != NULL ) { printf("prio <%d> nome <%s>\n", x->prio, x->nome); x = x->ant; } } int main( int argc, char *argv[]) { insere( 5, "555555555"); insere( 8, "888888888"); insere( 7, "777777777"); insere( 3, "333333333"); insere( 9, "999999999"); lista(); if( retira("555555555") == -1 ) printf("Nao encontrou!\n"); else lista(); if( retira("zz") == -1 ) printf("Nao encontrou zz!\n"); else lista(); }