Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct lista {
  5.        int chave;
  6.        int tempo;
  7.        struct lista *prox;
  8.        struct lista *back;
  9. };
  10.  
  11. int t =0;
  12.  
  13. void insere_elem(struct lista **cab, int n, int j);
  14. void remove_elem(struct lista **cab, int n);
  15. void imprime_elem(struct lista **cab);
  16.  
  17.  
  18. int main (void) {
  19.    char c;
  20.    int n, j;
  21.    struct lista *cab;
  22.  
  23.    cab = NULL;
  24.    while(scanf("%c", &c) && c != 'f') {
  25.  
  26.        if (c == 'i') {
  27.            scanf("%d %d", &n, &j);
  28.            insere_elem(&cab, n, j);
  29.        }
  30.        else if (c == 'r') {
  31.            scanf("%d", &n);
  32.            //chama a função que remove elementos
  33.            remove_elem(&cab, n);
  34.        }      
  35.    }
  36.    
  37.    imprime_elem(&cab);    
  38.  
  39. return 0;
  40. }
  41.  
  42.  
  43. void insere_elem(struct lista **cab, int n, int j) {
  44.     struct lista *aux, *temp;
  45.  
  46.     int i=1, h;
  47.  
  48.     temp = malloc(sizeof(struct lista));
  49.     temp->chave = n;
  50.     temp->tempo = t++;
  51.     temp->prox = NULL;
  52.     temp->back = NULL;
  53.  
  54.     if (*cab == NULL)
  55.         *cab = temp;
  56.     else {
  57.         aux = *cab;
  58.         while(aux->prox != NULL) {
  59.             i++;
  60.             aux = aux->prox;
  61.         }
  62.  
  63.         aux->prox = temp;
  64.         i++;
  65.  
  66.         if (j != 0) {
  67.             aux = *cab;
  68.             for(h = 1; h<i-j; h++)
  69.                 aux = aux->prox;
  70.             temp->back = aux;
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76. void remove_elem(struct lista **cab, int n) {
  77.  
  78.     struct lista *aux, *temp;
  79.     aux = *cab;
  80.  
  81.     if(aux != NULL) {
  82.         if(aux->chave == n) {
  83.             *cab = temp = aux->prox;
  84.             aux->prox = aux->back = NULL;
  85.             while(temp != NULL) {
  86.                 if(temp->back == aux)
  87.                     temp->back = NULL;
  88.                 temp = temp->prox;
  89.             }
  90.         } else {
  91.             while(aux->chave != n && aux->prox != NULL) {
  92.                 temp = aux;
  93.                 aux = aux->prox;
  94.             }
  95.             if(aux->chave == n) {
  96.                 temp->prox = aux->prox;
  97.                 aux->back =  aux->prox = NULL;
  98.             }    
  99.             temp = temp->prox;
  100.             while(temp != NULL) {
  101.                 if(temp->back == aux)
  102.                     temp->back = NULL;
  103.                 temp = temp->prox;
  104.             }
  105.  
  106.         }
  107.     }
  108.     t++;
  109.     free(aux);
  110. }
  111.  
  112. void imprime_elem(struct lista **cab) {
  113.  
  114.     int i=0;
  115.     struct lista *aux, *temp;
  116.  
  117.     aux = *cab;
  118.     if (aux == NULL)
  119.         printf("-1");
  120.     else {
  121.         printf("[%d,%d] ", aux->chave, aux->tempo);
  122.         while(aux->prox != NULL) {
  123.             aux = aux->prox;
  124.             printf("[%d,%d", aux->chave, aux->tempo);
  125.             if (aux->back == NULL)
  126.                 printf("] ");
  127.             else if (aux->back != NULL) {
  128.                 temp = *cab;
  129.                 while(aux->back != temp && temp->prox != NULL) {
  130.                     //contador para a posição
  131.                     i++;
  132.                     temp = temp->prox;
  133.                 }
  134.                 printf(",%d] ", i);
  135.             }
  136.             i = 0;
  137.         }
  138.     }
  139.     printf("\n");
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement