Advertisement
adwas33

Lista wskaźnikowa

Aug 31st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Lista{
  6. public:
  7.  typedef int pozycja;
  8.  typedef int wartosc;
  9. private:
  10.     wartosc*tab;
  11.     unsigned int n,roz_tab;//liczba elementow i rozmiar tablicy
  12. public: Lista();
  13.     ~Lista();
  14.     bool empty();
  15.     wartosc front();
  16.     wartosc back();
  17.     void push_front(wartosc);
  18.     void push_back(wartosc);
  19.     void pop_front();
  20.     void pop_back();
  21.     void insert(pozycja,wartosc);
  22.     void erase(pozycja);
  23.     pozycja first();
  24.     pozycja last();
  25.     pozycja next(pozycja);
  26.     wartosc & at(pozycja);
  27.  
  28.  
  29. };
  30.  
  31. int main()
  32. {
  33.     cout << "Hello World!" << endl;
  34.     return 0;
  35. }
  36. Lista::Lista() // konstruktor
  37. {
  38.     n=0;
  39.     roz_tab=100; //na początek stała wartość która ma nam wystarczyć (nie sugerować się tym)
  40.     tab=new wartosc[roz_tab];
  41.  
  42. }
  43. Lista::~Lista() // destruktor
  44. {
  45.     delete [] tab; //usuwa tablice
  46. }
  47. bool Lista::empty(){
  48.     if(n==0)
  49.         return true; //pusty ( tak)
  50.     else
  51.         return false;// pusty(nie)
  52. }
  53. Lista::wartosc Lista::front(){
  54.     return tab[0]; // zwraca pierwszy
  55. }
  56. Lista::wartosc Lista::back(){
  57.     return tab [n-1]; // zwraca ostatni
  58. }
  59. void Lista::push_back(wartosc w)
  60. {
  61.     tab[n]=w; // dodajemy na końcu element
  62.     n++;
  63. }
  64.  
  65. void Lista::push_front(wartosc w)
  66. {
  67.     for (unsigned i=n;i>0;i--) {
  68.         tab[i]=tab[i-1];
  69.         tab[0]=w;
  70.         n++;
  71.  
  72.     }
  73. }
  74. void Lista ::pop_back(){
  75.     n--;
  76. }
  77. void Lista:: pop_front()
  78. {
  79.     for(unsigned int i=0;i<n-1;i++)
  80.         tab[i]=tab[i+1];
  81.     n--;
  82. }
  83. void Lista::insert(pozycja p, wartosc w)
  84. {
  85.     for(unsigned int i=n;i>p;i--)
  86.     tab[i]=tab[i-1];
  87.     tab[p]=w;
  88.     n++;
  89. }
  90.  
  91. //void Lista::push_front(wartosc w){ Alternatywa dla powyższego zapisu;
  92. //    insert(0,w);
  93. //}
  94. //void Lista::push_back(wartosc w)
  95. //{
  96. //    insert(n,w);
  97. //}
  98. void Lista::erase(pozycja p)
  99. {
  100.     for(unsigned int i=p;i<n-1;i++)
  101.    tab[i]=tab[i+1];
  102.    n--;
  103. }
  104. //void Lista:: pop_front(wartosc w)
  105. //{
  106. //    erase(0,w);
  107. //}
  108.  
  109. //void Lista::pop_back(wartosc w)
  110. //{
  111. //    erase(n-1,w);
  112. //}
  113. Lista::pozycja Lista::first(){
  114.     return 0;
  115.  
  116. }
  117. Lista::pozycja Lista::last(){
  118.    return  n-1;
  119. }
  120. Lista::pozycja Lista::next(pozycja p){
  121.     return p+1;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement