Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int n;
  6.     Node *next;
  7.     Node(int e, Node *N): n(e), next(N) {}
  8. };
  9.  
  10. class List {
  11. protected:
  12.     Node *head;
  13. public:
  14.     List() {}
  15.     void push_front(int e) {
  16.         Node *n = new Node(e, head);
  17.         head = n;
  18.     }
  19.     void print() const {
  20.         Node *it = head;
  21.         while(it != nullptr){
  22.             cout << it->n << " ";
  23.             it = it->next;
  24.         }
  25.         cout << endl;
  26.     }
  27.     ~List() {
  28.         Node *it = head;
  29.         Node *brisi;
  30.         while(it != nullptr){
  31.             brisi = it;
  32.             it = it->next;
  33.             delete brisi;
  34.         }
  35.     }
  36.     Node *search(int e) {
  37.         Node *it = head;
  38.         while(it != nullptr){
  39.             if(it->n == e)
  40.                 return it;
  41.             it = it->next;
  42.         }
  43.         return nullptr;
  44.     }
  45.     bool erease(int e) {
  46.         Node *it = head;
  47.         Node *brisi;
  48.         if(head->n == e){
  49.             brisi = head;
  50.             head = head->next;
  51.             delete brisi;
  52.             return true;
  53.         }
  54.         while(it != nullptr) {
  55.             if (it->next->n == e) {
  56.                 brisi = it->next;
  57.                 it->next = it->next->next;
  58.                 delete brisi;
  59.                 return true;
  60.             }
  61.         }
  62.         return false;
  63.     }
  64.     int size() const {
  65.         Node *it = head;
  66.         int cnt=0;
  67.         while(it!= nullptr){
  68.             cnt++;
  69.             it = it->next;
  70.         }
  71.         return cnt;
  72.     }
  73. };
  74.  
  75. int main() {
  76.  
  77.     List L;
  78.     for(int i=1; i<6; i++)
  79.         L.push_front(i);
  80.  
  81.     L.erease(5);
  82.     cout << L.size() << endl;
  83.     cout << L.search(3) << endl;
  84.  
  85.     L.print();
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement