allia

лист 31

Nov 12th, 2020 (edited)
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  struct ListElem
  5. {
  6.    int value;
  7.    ListElem *next;
  8. };
  9.  
  10. class List
  11. {
  12.    ListElem *pbeg, *pend;
  13.    int Size = 0;
  14. public:
  15.    List()
  16.     {
  17.      pend = pbeg = NULL;
  18.     }
  19.    int get_Size()
  20.    {
  21.      return Size;
  22.    }
  23.    void push_front(int val);
  24.    int pop_front();
  25. };
  26.  
  27. void List::push_front(int val)
  28. {
  29.    ListElem *pnew = new ListElem;
  30.    pnew->value = val;
  31.    pnew->next = pbeg;
  32.    pbeg = pnew;
  33.    if (!pend)
  34.      pend = pnew;
  35.   Size++;
  36. }
  37.  
  38. int List::pop_front()
  39. {
  40.    if (!pbeg) return -1;
  41.    ListElem *ptr = pbeg;
  42.    int val = pbeg->value;
  43.    pbeg = pbeg->next;
  44.    if (!pbeg) pend = NULL;
  45.    delete ptr; return val;
  46. }
  47.  
  48. int main ()
  49. {
  50.    List lst;
  51.   int number, n;
  52.   cin >> n;
  53.  
  54.   for (int i = 0; i < n ; i++)
  55.     {
  56.       cin >> number;
  57.       lst.push_front(number);
  58.     }
  59.  
  60.  
  61.   for (int i = 0; i < lst.get_Size(); i++)
  62.    cout << lst.pop_front() << " ";
  63. }
Add Comment
Please, Sign In to add comment