fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Questão 2
  5.  
  6. Crie um TAD Lista (genérica), com as funções a seguir.
  7.  
  8. criarLista() Cria a lista vazia, alocando a memória necessária.
  9. inserir(lista, elem) Insere um elemento ao final da lista.
  10. inserirPos(lista, pos, elem) Insere um elemento na posição informada.
  11. remover(lista, pos) Remove o elemento da posição informada (lembre-se de liberar a memória).
  12. obter(lista, pos) Retorna o elemento localizado na posição informada.
  13. tamanho(lista) Retorna a quantidade de elementos da lista.
  14. destruirLista(lista) Destrói toda a lista (lembre-se de liberar a memória).
  15.  
  16. */
  17.  
  18.  
  19.  
  20. /* Minhas estruturas */
  21. typedef struct lista
  22. {
  23. struct no *prim;
  24. struct no *ult;
  25. int tam;
  26. }lista;
  27.  
  28.  
  29. typedef struct no
  30. {
  31. struct no *prox;
  32. void *elem;
  33. }no;
  34.  
  35. typedef struct pessoa
  36. {
  37. char nome;
  38. int idade;
  39. }pessoa;
  40.  
  41.  
  42. /* minhas funções */
  43. lista *criaLista()
  44. {
  45. lista *lst = malloc(sizeof(lista));
  46. lst -> prim = NULL;
  47. lst -> ult = NULL;
  48. lst -> tam = 0;
  49. return lst;
  50. }
  51.  
  52.  
  53. no *criaNo(void *n)
  54. {
  55. no *no = malloc(sizeof(no));
  56. no -> prox = NULL;
  57. no -> valor = n;
  58. return no;
  59. }
  60.  
  61.  
  62. pessoa *criaPessoa(char nome, int idade)
  63. {
  64. pessoa *pessoa = malloc(sizeof(pessoa));
  65.  
  66. pessoa -> nome = nome;
  67. pessoa -> idade = idade;
  68.  
  69. return pessoa;
  70. }
  71.  
  72. /* falta essa daqui */
  73. lista *inserir(lista *lst, void )
  74. {
  75. no *novoNo = criaNo(val);
  76.  
  77. if (lst -> prim == NULL)
  78. lst -> prim = novoNo;
  79. else
  80. lst -> ult -> prox = novoNo;
  81.  
  82. lst -> ult = novoNo;
  83. lst -> tam++;
  84. return lst;
  85. }
  86.  
  87. /* falta essa daqui */
  88. lista *inserirPos(lista *lst, int pos, int val)
  89. {
  90. no *novoNo = criaNo(val);
  91. no *noAux;
  92. int cont = 1;
  93.  
  94. if (pos == 0)
  95. {
  96. novoNo -> prox = lst -> prim;
  97. lst -> prim = novoNo;
  98. }
  99.  
  100. else if ((pos > 0) && (pos <= lst -> tam - 1))
  101. {
  102. noAux = lst -> prim;
  103. while (cont < pos)
  104. {
  105. noAux = noAux -> prox;
  106. cont++;
  107. }
  108. novoNo -> prox = noAux -> prox;
  109. noAux -> prox = novoNo;
  110. }
  111.  
  112. else
  113. {
  114. lst -> ult -> prox = novoNo;
  115. lst -> ult = novoNo;
  116. }
  117.  
  118. lst -> tam++;
  119. return lst;
  120. }
  121.  
  122. /* falta essa daqui */
  123. lista *remover(lista *lst, int pos)
  124. {
  125. no *auxNo, *auxRem;
  126. int cont;
  127.  
  128. if (pos == 0)
  129. {
  130. auxNo = lst -> prim -> prox;
  131. free(lst -> prim);
  132. lst -> prim = auxNo;
  133. }
  134. else if ((pos > 0) && (pos < lst -> tam))
  135. {
  136.  
  137. auxNo = lst -> prim;
  138. cont = 1;
  139. while (cont < pos)
  140. {
  141. auxNo = auxNo -> prox;
  142. cont++;
  143. }
  144. auxRem = auxNo -> prox;
  145. auxNo -> prox = auxNo -> prox -> prox;
  146. free(auxRem);
  147.  
  148. }
  149. else
  150. {
  151. auxNo = lst -> prim;
  152. cont = 1;
  153. while (cont < pos)
  154. {
  155. auxNo = auxNo -> prox;
  156. cont++;
  157. }
  158. free (lst -> ult);
  159. lst -> ult = auxNo;
  160. }
  161.  
  162. lst -> tam--;
  163. return lst;
  164. }
  165.  
  166.  
  167. /* falta essa daqui */
  168. int obter(lista *lst, int pos)
  169. {
  170. int i = 0;
  171. no *noAux = lst -> prim;
  172.  
  173. if (pos == 0)
  174. {
  175. return lst -> prim -> valor;
  176. }
  177. else if ((pos > 0) && (pos < lst->tam - 1))
  178. {
  179. while(i < pos)
  180. {
  181. noAux = noAux -> prox;
  182. i++;
  183. }
  184. return noAux -> valor;
  185. }
  186. else
  187. {
  188. return lst -> ult -> valor;
  189. }
  190. }
  191.  
  192.  
  193. int tamanho(lista *lst)
  194. {
  195. return lst -> tam;
  196. }
  197.  
  198.  
  199.  
  200. void destruirLista(lista *lst)
  201. {
  202. no *auxNo;
  203.  
  204. while (lst -> prim != NULL)
  205. {
  206. auxNo = lst -> prim;
  207. lst -> prim = auxNo -> prox;
  208. free(auxNo);
  209. }
  210.  
  211. free(lst);
  212. }
  213.  
  214.  
  215. int main()
  216. {
  217. /* criando pessoas */
  218. pessoa *p1 = criaPessoa("anne", 24);
  219. pessoa *p2 = criaPessoa("joão", 21);
  220. pessoa *p2 = criaPessoa("maria", 20);
  221.  
  222.  
  223. /* testa função criarLista */
  224. lista *lst = criaLista();
  225.  
  226. /* testa função inserir */
  227. inserir(lst, p1);
  228. inserir(lst, p2);
  229.  
  230.  
  231. /* testa função inserirPos */
  232. inserirPos(lst, 2, p3);
  233.  
  234. /* testa função imprimir */
  235. imprimir(lst);
  236.  
  237. /* testa função tamanho */
  238. printf("Teste da função tamanho(): %d\n", tamanho(lst));
  239.  
  240. /* testa função obter */
  241. printf("Teste da função obter(): %d\n", obter(lst, 2));
  242.  
  243.  
  244. /* testa função remover
  245. remover(lst, 0);
  246. imprimir(lst);*/
  247.  
  248.  
  249. /* testa função destruirLista
  250. destruirLista(lst);*/
  251.  
  252. return 0;
  253. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘criaNo’:
prog.c:57:5: error: ‘no {aka struct no}’ has no member named ‘valor’
  no -> valor = n;
     ^~
prog.c: At top level:
prog.c:73:1: error: ‘void’ must be the only parameter
 lista *inserir(lista *lst, void )
 ^~~~~
prog.c: In function ‘inserir’:
prog.c:75:22: error: ‘val’ undeclared (first use in this function)
  no *novoNo = criaNo(val);
                      ^~~
prog.c:75:22: note: each undeclared identifier is reported only once for each function it appears in
prog.c: In function ‘inserirPos’:
prog.c:90:22: warning: passing argument 1 of ‘criaNo’ makes pointer from integer without a cast [-Wint-conversion]
  no *novoNo = criaNo(val);
                      ^~~
prog.c:53:5: note: expected ‘void *’ but argument is of type ‘int’
 no *criaNo(void *n)
     ^~~~~~
prog.c: In function ‘obter’:
prog.c:175:22: error: ‘struct no’ has no member named ‘valor’
   return lst -> prim -> valor;
                      ^~
prog.c:184:16: error: ‘no {aka struct no}’ has no member named ‘valor’
   return noAux -> valor;
                ^~
prog.c:188:21: error: ‘struct no’ has no member named ‘valor’
   return lst -> ult -> valor;
                     ^~
prog.c: In function ‘main’:
prog.c:218:26: warning: passing argument 1 of ‘criaPessoa’ makes integer from pointer without a cast [-Wint-conversion]
  pessoa *p1 = criaPessoa("anne", 24);
                          ^~~~~~
prog.c:62:9: note: expected ‘char’ but argument is of type ‘char *’
 pessoa *criaPessoa(char nome, int idade)
         ^~~~~~~~~~
prog.c:219:26: warning: passing argument 1 of ‘criaPessoa’ makes integer from pointer without a cast [-Wint-conversion]
  pessoa *p2 = criaPessoa("joão", 21);
                          ^~~~~~~
prog.c:62:9: note: expected ‘char’ but argument is of type ‘char *’
 pessoa *criaPessoa(char nome, int idade)
         ^~~~~~~~~~
prog.c:220:10: error: redefinition of ‘p2’
  pessoa *p2 = criaPessoa("maria", 20);
          ^~
prog.c:219:10: note: previous definition of ‘p2’ was here
  pessoa *p2 = criaPessoa("joão", 21);
          ^~
prog.c:220:26: warning: passing argument 1 of ‘criaPessoa’ makes integer from pointer without a cast [-Wint-conversion]
  pessoa *p2 = criaPessoa("maria", 20);
                          ^~~~~~~
prog.c:62:9: note: expected ‘char’ but argument is of type ‘char *’
 pessoa *criaPessoa(char nome, int idade)
         ^~~~~~~~~~
prog.c:227:2: error: too many arguments to function ‘inserir’
  inserir(lst, p1);
  ^~~~~~~
prog.c:73:8: note: declared here
 lista *inserir(lista *lst, void )
        ^~~~~~~
prog.c:228:2: error: too many arguments to function ‘inserir’
  inserir(lst, p2);
  ^~~~~~~
prog.c:73:8: note: declared here
 lista *inserir(lista *lst, void )
        ^~~~~~~
prog.c:232:21: error: ‘p3’ undeclared (first use in this function)
  inserirPos(lst, 2, p3);
                     ^~
prog.c:235:2: warning: implicit declaration of function ‘imprimir’ [-Wimplicit-function-declaration]
  imprimir(lst);
  ^~~~~~~~
prog.c: In function ‘obter’:
prog.c:190:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty