fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. struct tipo_lista{
  6. int id;
  7. struct tipo_lista* prox;
  8. };
  9.  
  10. typedef struct tipo_lista tipo_lista;
  11.  
  12. tipo_lista* cria_no(int id){
  13.  
  14. tipo_lista* ret = malloc(sizeof(tipo_lista));
  15. ret->prox = NULL;
  16. ret->id = id;
  17.  
  18. return ret;
  19. }
  20.  
  21. tipo_lista* inserir_fim (tipo_lista * p, tipo_lista * novo_no)
  22. {
  23. //Se a lista estiver vazia
  24. if(p==NULL)
  25. return novo_no;
  26.  
  27. tipo_lista* r = p; //Para manter a referencia ao primeiro elemento
  28. while (p->prox != NULL)
  29. {
  30. p = p->prox;
  31. }
  32. p->prox = novo_no;
  33. return r;
  34. }
  35.  
  36. //inicio
  37. tipo_lista * inserir_inicio (tipo_lista * p, tipo_lista * novo_no)
  38. {
  39. novo_no -> prox = p;
  40. return novo_no;
  41. }
  42.  
  43. void print (tipo_lista *p){
  44.  
  45. puts("==BEGIN==\n");
  46. while (p != NULL){
  47. puts("+++++++++");
  48. printf("%s%p\n%s%d\n%s%p\n",
  49. "This : ", p,
  50. "ID : ", p->id,
  51. "Prox : ", p->prox);
  52. puts("---------");
  53. p = p->prox;
  54. }
  55. puts("===END===\n");
  56.  
  57. }
  58.  
  59.  
  60. int main(){
  61.  
  62. tipo_lista *p = NULL;
  63.  
  64. print(p);
  65.  
  66. p = inserir_inicio(p,cria_no(2));
  67.  
  68. print(p);
  69.  
  70. p = inserir_fim(p, cria_no(3));
  71.  
  72. print(p);
  73.  
  74. p = inserir_inicio(p,cria_no(4));
  75.  
  76. print(p);
  77.  
  78. p = inserir_fim(p, cria_no(5));
  79.  
  80. print(p);
  81.  
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
==BEGIN==

===END===

==BEGIN==

+++++++++
This : 0x2b8ce007c020
ID   : 2
Prox : (nil)
---------
===END===

==BEGIN==

+++++++++
This : 0x2b8ce007c020
ID   : 2
Prox : 0x2b8ce007c040
---------
+++++++++
This : 0x2b8ce007c040
ID   : 3
Prox : (nil)
---------
===END===

==BEGIN==

+++++++++
This : 0x2b8ce007c060
ID   : 4
Prox : 0x2b8ce007c020
---------
+++++++++
This : 0x2b8ce007c020
ID   : 2
Prox : 0x2b8ce007c040
---------
+++++++++
This : 0x2b8ce007c040
ID   : 3
Prox : (nil)
---------
===END===

==BEGIN==

+++++++++
This : 0x2b8ce007c060
ID   : 4
Prox : 0x2b8ce007c020
---------
+++++++++
This : 0x2b8ce007c020
ID   : 2
Prox : 0x2b8ce007c040
---------
+++++++++
This : 0x2b8ce007c040
ID   : 3
Prox : 0x2b8ce007c080
---------
+++++++++
This : 0x2b8ce007c080
ID   : 5
Prox : (nil)
---------
===END===