fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct elem {
  5. int dane;
  6. elem* nast;
  7. };
  8.  
  9. void insert(int x, int i, elem* &a) {
  10. if (i < 0) {
  11. string s = "Indeks z poza zakresu.";
  12. throw s;
  13. }
  14. elem* e = new elem;
  15. e->dane = x;
  16. e->nast = NULL;
  17. if (i == 0) {
  18. e->nast = a;
  19. a = e;
  20. }
  21. else {
  22. elem* t = a;
  23. for (i -= 1; i > 0; --i) {
  24. t = t->nast;
  25. if (t == NULL) {
  26. string s = "Indeks z poza zakresu.";
  27. throw s;
  28. }
  29. }
  30. e->nast = t->nast;
  31. t->nast = e;
  32. }
  33. }
  34.  
  35. elem* tail(elem* pocz) {
  36. return pocz->nast;
  37. }
  38.  
  39. elem* zad3(elem* pocz, int n) {
  40. if (n == 0)
  41. return pocz;
  42. else
  43. return zad3(tail(pocz), n - 1);
  44. }
  45.  
  46. elem* reverse(elem* pocz) {
  47. if (pocz == NULL || pocz->nast == NULL) {
  48. return pocz;
  49. } else {
  50. elem* wsk = reverse(tail(pocz));
  51. pocz->nast->nast = pocz;
  52. pocz->nast = NULL;
  53. return wsk;
  54. }
  55. }
  56. elem* zad4(elem* pocz) {
  57. if (pocz == NULL || pocz->nast == NULL)
  58. return pocz;
  59. else
  60. return zad4(tail(reverse(tail(pocz))));
  61. }
  62.  
  63. void show(elem* lista, int x) {
  64. if (lista->nast != NULL)
  65. show(lista->nast, x - 1);
  66. cout << lista->dane + x;
  67. }
  68.  
  69. int main() {
  70. elem* poczatek = NULL;
  71. try {
  72. insert(1, 0, poczatek);
  73. insert(2, 1, poczatek);
  74. insert(3, 2, poczatek);
  75. insert(4, 3, poczatek);
  76. insert(5, 4, poczatek);
  77. insert(6, 5, poczatek);
  78. //insert(7, 6, poczatek);
  79. //insert(8, 7, poczatek);
  80. //insert(9, 8, poczatek);
  81. //elem* nowa = zad4(poczatek);
  82. //while (nowa) {
  83. // cout << nowa->dane;
  84. // nowa = nowa->nast;
  85. //}
  86. show(poczatek,1);
  87. }
  88. catch (string message) {
  89. cout << "Wyjatek: " << message << endl;
  90. }
  91. return 0;
  92. }
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
222222