Advertisement
Guest User

lista jednokierunkowa

a guest
Dec 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. /** @file */
  2.  
  3. /*
  4. PODSTAWY PROGRAMOWANIA KOMPUTERÓW
  5.  
  6. wykład 8: lista jednokierunkowa
  7.  
  8.  */
  9.  
  10. #define debug(x)  std::cerr << "(" << __LINE__ << ") " << #x << " == " << (x) << std::endl
  11.  
  12.  
  13. #include <iostream>
  14. #include <vector>
  15. #include <climits>
  16. #include <random>
  17. #include <chrono>
  18. #include <string>
  19. #include <iomanip>
  20.  
  21. #include "funkcje.h"
  22. #include "memory/nvwa/debug_new.h"
  23.  
  24. element * znajdzPoprzednikIteracyjnie(element* pHead, element* pNastepnik)
  25. {
  26.     if (not pHead or pNastepnik == pHead)
  27.         return nullptr;
  28.    
  29.     auto p = pHead;
  30.     while (p and p->pNext != pNastepnik)
  31.         p = p->pNext;
  32.    
  33.     return p;
  34. }
  35.  
  36.  
  37. element * znajdzElementRekurencyjnie(element* pHead, typ liczba)
  38. {
  39.     if (pHead)
  40.     {
  41.         if (pHead->wartosc == liczba)
  42.             return pHead;
  43.         else
  44.             return /*!!*/ znajdzElementRekurencyjnie(pHead->pNext, liczba);
  45.     }
  46.     else // lista pusta
  47.         return nullptr;
  48. }
  49.  
  50.  
  51. void usunListeOdKoncaRekurencyjnie(element *& pHead)
  52. {
  53.     if (pHead)
  54.     {
  55.         usunListeOdKoncaRekurencyjnie(pHead->pNext);
  56.         delete pHead;
  57.         pHead = nullptr;
  58.     }
  59. }
  60.  
  61.  
  62. void usunListeOdPoczatkuRekurencyjnie(element *& pHead)
  63. {
  64.     if (pHead)
  65.     {
  66.         auto p = pHead->pNext;
  67.         delete pHead;
  68.         pHead = nullptr;
  69.         usunListeOdPoczatkuRekurencyjnie(p);
  70.     }    
  71. }
  72.  
  73.  
  74. void dodajNaKoniecRekurencyjnie(element *& pHead, typ liczba)
  75. {
  76.     if (not pHead) // lista pusta
  77.         pHead = new element { liczba, nullptr };
  78.     else
  79.         dodajNaKoniecRekurencyjnie(pHead->pNext, liczba);
  80. }
  81.  
  82.  
  83. void dodajNaKoniecIteracyjnie(element *& pHead, typ liczba)
  84. {
  85.     if (not pHead) // lista pusta
  86.         pHead = new element { liczba, nullptr };
  87.     else
  88.     {
  89.         auto p = pHead;
  90.         while (p->pNext)
  91.            p = p->pNext;
  92.  
  93.         // p wskazuje ostatni element listy
  94.         p->pNext = new element { liczba, nullptr};        
  95.     }
  96. }
  97.  
  98.  
  99. void usunListeIteracyjnie(element *& pHead)
  100. {
  101.     while (pHead)
  102.     {
  103.         auto p = pHead->pNext;
  104.         delete pHead;
  105.         pHead = p;
  106.     }  
  107. }
  108.  
  109. void wypiszOdPoczatkuRekurencyjnie(element* pHead, std::ostream& ss)
  110. {
  111.     if (pHead)
  112.     {
  113.         ss << pHead->wartosc << ' ';
  114.         wypiszOdPoczatkuRekurencyjnie(pHead->pNext, ss);
  115.     }
  116. }
  117.  
  118. void wypiszOdKoncaRekurencyjnie(element* pHead, std::ostream& ss)
  119. {
  120.     if (pHead)
  121.     {
  122.         wypiszOdKoncaRekurencyjnie(pHead->pNext, ss);
  123.         ss << pHead->wartosc << ' ';
  124.     }    
  125. }
  126.  
  127.  
  128. void wypiszOdPoczatkuIteracyjnie(element* pHead)
  129. {
  130. // wersja 1
  131.     //     auto p = pHead;
  132. //    
  133. //     while ( p != nullptr )
  134. //     {
  135. //         std::cout << p->wartosc << ' ';
  136. //         p = p->pNext;
  137. //     }
  138.  
  139.     // wersja 2
  140.    
  141.     while (pHead)
  142.     {
  143.         std::cout << pHead->wartosc << ' ';
  144.         pHead = pHead->pNext;
  145.     }
  146.  
  147. }
  148.  
  149.  
  150. void dodajNaPoczatek(element * & pHead, typ liczba)
  151. {
  152.     // wersja oryginalna
  153. //     if (pHead == nullptr) // lista pusta
  154. //     {
  155. //         element * pNowy = new element;
  156. //         (*pNowy).wartosc = liczba;
  157. //         pNowy->pNext = nullptr;
  158. //         pHead = pNowy;
  159. //     }
  160. //     else // lista niepusta
  161. //     {
  162. //         element * pNowy = new element;
  163. //         (*pNowy).wartosc = liczba;
  164. //         pNowy->pNext = pHead;
  165. //         pHead = pNowy;
  166. //     }
  167.  
  168.     // ufikuśnianie, wersja 2:
  169.    
  170. //     if (pHead == nullptr) // lista pusta
  171. //     {
  172. //         element * pNowy = new element;
  173. //         (*pNowy).wartosc = liczba;
  174. //         pNowy->pNext = pHead;
  175. //         pHead = pNowy;
  176. //     }
  177. //     else // lista niepusta
  178. //     {
  179. //         element * pNowy = new element;
  180. //         (*pNowy).wartosc = liczba;
  181. //         pNowy->pNext = pHead;
  182. //         pHead = pNowy;
  183. //     }
  184.    
  185.    
  186.     //    
  187.     // ufikuśnianie, wersja 3:
  188.    
  189. //     element * pNowy = new element;
  190. //     (*pNowy).wartosc = liczba;
  191. //     pNowy->pNext = pHead;
  192. //     pHead = pNowy;
  193.    
  194.     // ufikuśnianie, wersja 4:
  195.    
  196. //     element * pNowy = new element { liczba, pHead };
  197. //     pHead = pNowy;
  198.  
  199.     // ufikuśnianie, wersja 5:
  200.  
  201.     pHead = new element { liczba, pHead };
  202. }
  203.  
  204.  
  205. int * polacz(const int* t1, const int r1, const int* t2, const int r2)
  206. {
  207.     int * ptab = new int [r1 + r2];
  208.    
  209.     for (int i = 0; i < r1; i++)
  210.         ptab[i] = t1[i];
  211.    
  212.     for (int i = 0; i < r2; i++)
  213.         ptab[r1 + i] = t2[i];
  214.    
  215.     return ptab;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement