Advertisement
KDOXG

Cadastro dos Stormtroopers

Jun 10th, 2019
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Lista{
  6.     char *nome;
  7.     struct Lista *pNext;
  8. };
  9.  
  10. struct Lista* delete(char *nome, struct Lista *lista)
  11. {
  12.     if (lista == NULL)
  13.     {
  14.         printf("Nome nao existente.\n");
  15.         return lista;
  16.     }
  17.     struct Lista *aux = lista, *cabeca = lista;
  18.     char flag = 0b0;
  19.     while (lista->pNext != NULL && strcmp(nome,lista->nome) != 0)
  20.     {
  21.         aux = lista;
  22.         lista = lista->pNext;
  23.         flag = 0b1;
  24.     }
  25.     if (strcmp(nome,lista->nome) != 0)
  26.     {
  27.         printf("Nome nao existente.\n");
  28.         return cabeca;
  29.     }
  30.     if (flag == 1)
  31.     {
  32.         aux->pNext = lista->pNext;
  33.         free(lista);
  34.         return cabeca;
  35.     }
  36.     else
  37.     {
  38.         cabeca = lista->pNext;
  39.         free(lista);
  40.         return cabeca;
  41.     }
  42. }
  43.  
  44. void insertInOrder(char *nome, struct Lista **lista)
  45. {
  46.     int a, b;
  47.     struct Lista *aux;
  48.     if (*lista == NULL)
  49.     {
  50.         *lista = malloc(sizeof(struct Lista));
  51.         (*lista)->nome = nome;
  52.         (*lista)->pNext = NULL;
  53.         return;
  54.     }
  55.     if (strcmp(nome,(*lista)->nome) == 0)
  56.     {
  57.         printf("Nome ja esta na lista\n");
  58.         return;
  59.     }
  60.  
  61.     a = (int)*(*lista)->nome;
  62.     b = (int)*nome;
  63.  
  64.     if ((*lista)->pNext == NULL)
  65.     {
  66.         if (a > b)
  67.         {
  68.             (*lista)->pNext = malloc(sizeof(struct Lista));
  69.             *(*lista)->pNext = **lista;
  70.             (*lista)->pNext->pNext = NULL;
  71.             (*lista)->nome = nome;
  72.         }
  73.         else
  74.         {
  75.             (*lista)->pNext = malloc(sizeof(struct Lista));
  76.             (*lista)->pNext->nome = nome;
  77.             (*lista)->pNext->pNext = NULL;
  78.         }
  79.     }
  80.     else
  81.     {
  82.         if (a > b)
  83.         {
  84.             aux = (*lista);
  85.             (*lista) = malloc(sizeof(struct Lista));
  86.             (*lista)->nome = nome;
  87.             (*lista)->pNext = aux;
  88.         }
  89.         else
  90.             insertInOrder(nome,&(*lista)->pNext);
  91.     }
  92.     return;
  93. }
  94.  
  95. void main()
  96. {
  97.     struct Lista *lista = NULL;
  98.     insertInOrder("Ernie Wells",&lista);
  99.     insertInOrder("Casworan Wells",&lista);
  100.     insertInOrder("Dunmore Wells",&lista);
  101.     insertInOrder("Northcliff Wells",&lista);
  102.     insertInOrder("Kam Wells",&lista);
  103.     insertInOrder("Trymian Wells",&lista);
  104.     insertInOrder("Aedan Wells",&lista);
  105.     lista = delete("Northcliff Wells",lista);
  106.     insertInOrder("Isaiah Wells",&lista);
  107.     lista = delete("Casworan Wells",lista);
  108.     insertInOrder("Chaunceler Wells",&lista);
  109.     insertInOrder("Asriel Wells",&lista);
  110.     lista = delete("Aedan Wells",lista);
  111.     for (; lista != NULL; lista = lista->pNext)
  112.         printf("%s\n", lista->nome);
  113.     return;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement