Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct lista {
- int chave;
- int tempo;
- struct lista *prox;
- struct lista *back;
- };
- int t =0;
- void insere_elem(struct lista **cab, int n, int j);
- void remove_elem(struct lista **cab, int n);
- void imprime_elem(struct lista **cab);
- int main (void) {
- char c;
- int n, j;
- struct lista *cab;
- cab = NULL;
- while(scanf("%c", &c) && c != 'f') {
- if (c == 'i') {
- scanf("%d %d", &n, &j);
- insere_elem(&cab, n, j);
- }
- else if (c == 'r') {
- scanf("%d", &n);
- //chama a função que remove elementos
- remove_elem(&cab, n);
- }
- }
- imprime_elem(&cab);
- return 0;
- }
- void insere_elem(struct lista **cab, int n, int j) {
- struct lista *aux, *temp;
- int i=1, h;
- temp = malloc(sizeof(struct lista));
- temp->chave = n;
- temp->tempo = t++;
- temp->prox = NULL;
- temp->back = NULL;
- if (*cab == NULL)
- *cab = temp;
- else {
- aux = *cab;
- while(aux->prox != NULL) {
- i++;
- aux = aux->prox;
- }
- aux->prox = temp;
- i++;
- if (j != 0) {
- aux = *cab;
- for(h = 1; h<i-j; h++)
- aux = aux->prox;
- temp->back = aux;
- }
- }
- }
- void remove_elem(struct lista **cab, int n) {
- struct lista *aux, *temp;
- aux = *cab;
- if(aux != NULL) {
- if(aux->chave == n) {
- *cab = temp = aux->prox;
- aux->prox = aux->back = NULL;
- while(temp != NULL) {
- if(temp->back == aux)
- temp->back = NULL;
- temp = temp->prox;
- }
- } else {
- while(aux->chave != n && aux->prox != NULL) {
- temp = aux;
- aux = aux->prox;
- }
- if(aux->chave == n) {
- temp->prox = aux->prox;
- aux->back = aux->prox = NULL;
- }
- temp = temp->prox;
- while(temp != NULL) {
- if(temp->back == aux)
- temp->back = NULL;
- temp = temp->prox;
- }
- }
- }
- t++;
- free(aux);
- }
- void imprime_elem(struct lista **cab) {
- int i=0;
- struct lista *aux, *temp;
- aux = *cab;
- if (aux == NULL)
- printf("-1");
- else {
- printf("[%d,%d] ", aux->chave, aux->tempo);
- while(aux->prox != NULL) {
- aux = aux->prox;
- printf("[%d,%d", aux->chave, aux->tempo);
- if (aux->back == NULL)
- printf("] ");
- else if (aux->back != NULL) {
- temp = *cab;
- while(aux->back != temp && temp->prox != NULL) {
- //contador para a posição
- i++;
- temp = temp->prox;
- }
- printf(",%d] ", i);
- }
- i = 0;
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement