Advertisement
Guest User

Jajajajaja

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct elemento{
  5.     int d;
  6.     struct elemento *p;
  7. } elemento;
  8.  
  9. int listarElementos(elemento *e);
  10. int inserirElementoInicio(elemento **inicio, int d);
  11. int inserirElementoFinal(elemento **inicio, int d);
  12. int inserirElementoIndice(elemento **inicio, int d, int idx);
  13.  
  14. int main(){
  15.     elemento *inicio = NULL;
  16.     /*printf("%d\n",listarElementos(inicio));
  17.     printf("%d\n",inserirElementoInicio(&inicio, 5));
  18.     printf("%d\n",inserirElementoInicio(&inicio, 3));
  19.     printf("%d\n",inserirElementoInicio(&inicio, 7));
  20.     printf("%d\n",inserirElementoFinal(&inicio, 8));
  21.     printf("%d\n",listarElementos(inicio));*/
  22.     inserirElementoInicio(&inicio, 4);
  23.     listarElementos(inicio);
  24.     printf("\n");
  25.     printf("\n");
  26.     printf("\n");
  27.     inserirElementoInicio(&inicio, 2);
  28.     listarElementos(inicio);
  29.     printf("\n");
  30.     printf("\n");
  31.     printf("\n");
  32.     inserirElementoFinal(&inicio, 5);
  33.     listarElementos(inicio);
  34.     printf("\n");
  35.     printf("\n");
  36.     printf("\n");
  37.     inserirElementoFinal(&inicio, 3);
  38.     listarElementos(inicio);
  39.     printf("\n");
  40.     printf("\n");
  41.     printf("\n");
  42.     inserirElementoIndice(&inicio, 99, 2);
  43.     listarElementos(inicio);
  44.     printf("\n");
  45.     printf("\n");
  46.     printf("\n");
  47.     removerElementoIndice(0, &inicio);
  48.     listarElementos(inicio);
  49.     printf("\n");
  50.     printf("\n");
  51.     printf("\n");
  52.  
  53.     return 0;
  54. }
  55.  
  56. int listarElementos(elemento *e){
  57.     if (e==NULL)
  58.         return 1; //return 1: lista vazia
  59.     while(e!=NULL){
  60.         printf("%p -> %d | %p\n",e, e->d, e->p);
  61.         e = e->p;
  62.     }
  63.     return 0; //return 0: operação realizada com sucesso
  64. }
  65.  
  66. int inserirElementoInicio(elemento **i, int d){
  67.     elemento *novo = malloc(sizeof(elemento));
  68.     if (novo == NULL)
  69.         return -1; // return -1: não foi possivel alocar memória
  70.     novo->d = d;
  71.     novo->p = *i;
  72.     *i = novo;
  73.     return 0; //return 0: operação realizada com sucesso
  74. }
  75.  
  76. int inserirElementoFinal(elemento **i, int d){
  77.     if (*i == NULL)
  78.         return inserirElementoInicio(i, d);
  79.     elemento *novo = malloc(sizeof(elemento));
  80.     novo->d = d;
  81.     novo->p = NULL;
  82.     if (novo == NULL)
  83.         return -1; //return -1: não foi possivel alocar memória
  84.     elemento *aux = *i;
  85.     while (aux->p != NULL){
  86.         aux = aux->p;
  87.     }
  88.     aux->p = novo;
  89.     return 0; //return 0: operação realizada com sucesso
  90. }
  91.  
  92. int inserirElementoIndice(elemento **i, int d, int idx){
  93.     if (idx < 0)
  94.         return -2; // return -2: indice invalido
  95.     else if (idx == 0)
  96.         return inserirElementoInicio(i, d);
  97.     else if (*i == NULL)
  98.         return -3; // return -3: indice não existe
  99.     else{
  100.         elemento *aux = *i;
  101.         int idxAux = 0;
  102.         while (aux != NULL && idxAux < idx -1){
  103.             aux = aux->p;
  104.             idxAux++;
  105.         }
  106.         if (idxAux < idx -1)
  107.             return -3; // return -3: indice não existe
  108.         elemento *novo = malloc(sizeof(elemento));
  109.         if (novo == NULL)
  110.             return -1; // return -1: não foi possivel alocar memória
  111.         novo->d = d;
  112.         novo->p = aux->p;
  113.         aux->p = novo;
  114.         return 0;
  115.     }
  116. }
  117.  
  118. int removerElementoIndice (int i, elemento **r ){
  119.  
  120. if (i < 0){
  121.  
  122.     return -1;
  123. }
  124.  
  125. if (i == 0){
  126.  
  127.     if (*r == NULL){
  128.         return -2;
  129.     }
  130.  
  131.     elemento *aux = *r;
  132.     *r = aux -> p;
  133.     free (aux);
  134.  
  135. }else{
  136.  
  137.     if (*r == NULL){
  138.         return -2;
  139.     }
  140.  
  141.     elemento *aux = *r;
  142.     int rAux = 0;
  143.  
  144.     while ((aux -> p != NULL) && rAux < r - 1){
  145.  
  146.     aux = aux -> p;
  147.     rAux++;
  148.  
  149.     }
  150.  
  151.     if (aux -> p != NULL){
  152.         elemento *aux2 = aux -> p;
  153.         aux -> p = aux2 -> p;
  154.         free(aux2);
  155.  
  156.     }else{
  157.  
  158.         return -2;
  159.     }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement