Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct elemento_t{
  5.     int dado;
  6.     int id;
  7.     struct elemento_t* prox;
  8. } elemento_t;
  9.  
  10. typedef struct elemento_t lista_t;
  11.  
  12.  
  13. elemento_t* aloca_elemento(int valor, int id){
  14.     elemento_t* ptr = (elemento_t *) malloc(sizeof(elemento_t));
  15.  
  16.     if(!ptr)
  17.         return NULL;
  18.  
  19.     ptr->dado = valor;
  20.     ptr->prox = NULL;
  21.     ptr->id   = id;
  22.  
  23.     return ptr;
  24. }
  25.  
  26. void inserir_final(lista_t** lista, int valor, int id ){
  27.     lista_t** ptr = lista;
  28.  
  29.     if( !lista )
  30.         return;
  31.  
  32.     while(*ptr) {
  33.         ptr = &((*ptr)->prox);
  34.     }
  35.  
  36.     *ptr = aloca_elemento(valor, id);
  37.     (*ptr)->id = id;
  38.  
  39. }
  40.  
  41.  
  42. void imprime_lista(lista_t** lista){
  43. lista_t** ptr = lista;
  44.  
  45. if( !lista )
  46. return;
  47.  
  48. while(*ptr) {
  49. printf("%d - %d\n", (*ptr)->id, (*ptr)->dado);
  50. ptr = &((*ptr)->prox);
  51. }
  52.  
  53. printf("\n");
  54. }//end imprime_lista()
  55.  
  56.  
  57. void imprime_lista_posicoes(lista_t** lista){
  58.     lista_t** ptr = lista;
  59.     int i = 1;
  60.  
  61.     if( !lista )
  62.         return;
  63.  
  64.     while(*ptr) {
  65.         printf("%d - %d\n", i++, (*ptr)->id);
  66.         ptr = &((*ptr)->prox);
  67.     }
  68.  
  69.     printf("\n");
  70. }//end imprime_lista()
  71.  
  72.  
  73.  
  74. int main() {
  75.     lista_t *lista_original   = NULL;
  76.     lista_t *lista_normal     = NULL;
  77.     lista_t *lista_prioridade = NULL;
  78.     lista_t *ptr_aux;
  79.     int id;
  80.     int i = 1;
  81.  
  82.     while(scanf("%d", &id) > 0){
  83.         inserir_final(&lista_original, id, i++);
  84.     }
  85.  
  86.     printf("Fila geral original\n");
  87.     imprime_lista(&lista_original);
  88.  
  89.     ptr_aux = lista_original;
  90.  
  91.     while(ptr_aux) {
  92.         if(ptr_aux->dado >= 60)
  93.             inserir_final(&lista_prioridade, ptr_aux->dado, ptr_aux->id);
  94.         else
  95.             inserir_final(&lista_normal, ptr_aux->dado, ptr_aux->id);
  96.  
  97.         ptr_aux = ptr_aux->prox;
  98.     }
  99.  
  100.     printf("Fila preferencial\n");
  101.     imprime_lista(&lista_prioridade);
  102.  
  103.     printf("Fila geral atualizada\n");
  104.     imprime_lista(&lista_normal);
  105.  
  106.     printf("Resultado esperado fila preferencial\n");
  107.     imprime_lista_posicoes(&lista_prioridade);
  108.  
  109.     printf("Resultado esperado fila geral\n");
  110.     imprime_lista_posicoes(&lista_normal);
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement