Advertisement
esquilo10

vai e vem

Oct 9th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct no
  5. {
  6. int info;
  7. no *esq, *dir;
  8. };
  9. typedef no* noptr;
  10.  
  11. void criaLista(noptr &lista, int x)
  12. {
  13. lista = new no;
  14. if(lista == NULL)
  15. {
  16. lista->esq = NULL;
  17. lista->dir = NULL;
  18. lista->info = x;
  19. }
  20. }
  21.  
  22. int inseredir(noptr &p, int x)
  23. {
  24. if(p == NULL)
  25. {
  26. return 1;
  27. }
  28. else
  29. {
  30. noptr q = new no;
  31. q->info = x;
  32. noptr d = p->dir;
  33. p->dir = q;
  34. q->esq = p;
  35. q->dir = d;
  36. return 0;
  37. }
  38. }
  39.  
  40. int main()
  41. {
  42. noptr lista = NULL;
  43. noptr cursor = NULL;
  44. int val;
  45. int move;
  46.  
  47. cin >> val;
  48. while(val != 0)
  49. {
  50. if(lista == NULL)
  51. {
  52. criaLista(lista, val);
  53. }
  54. else
  55. {
  56. inseredir(lista, val);
  57. }
  58. cin >> val;
  59. }
  60.  
  61. cursor = lista;
  62.  
  63. cin >> move;
  64. while(move != -1)
  65. {
  66. if(move == 1)
  67. {
  68. if(cursor->dir != NULL)
  69. cursor = cursor->dir;
  70. }
  71. if(move == 0)
  72. {
  73. if(cursor->esq != NULL)
  74. cursor = cursor->esq;
  75. }
  76. val = cursor->info;
  77. cin >> move;
  78. }
  79. cout << cursor->info;
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement