Guest User

Untitled

a guest
Apr 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Node
  5. {
  6. private:
  7.     int _id;
  8.     std::string _name;
  9. public:
  10.     Node(int id, std::string name)
  11.         : nextNode(0), _id(id), _name(name)
  12.     { }
  13.  
  14.     Node(Node* nextNode, int id, std::string name)
  15.         : nextNode(nextNode), _id(id), _name(name)
  16.     { }
  17.  
  18.     int getId() { return _id; }
  19.     std::string getName() { return _name; }
  20.  
  21.     Node* nextNode;
  22.  
  23.     void print()
  24.     {
  25.         std::cout << getId() << " - " << getName() << std::endl;
  26.     }
  27. };
  28.  
  29. class List
  30. {
  31. private:
  32.     Node* _first;
  33. public:
  34.     void push(int id, std::string name);
  35.     bool pushAfter(int targetId, int id, std::string name);
  36.     bool remove(int targetId);
  37.     bool swap(int targetId);
  38.     bool print();
  39.  
  40.     List() : _first(0) { }
  41.     ~List();
  42. };
  43.  
  44. void List::push(int id, std::string name)
  45. {
  46.     if (_first)
  47.     {
  48.         Node* node = new Node(_first, id, name);
  49.         Node* curr = _first;
  50.         while (curr->nextNode != _first)
  51.             curr = curr->nextNode;
  52.         curr->nextNode = node;
  53.     }
  54.     else
  55.     {
  56.         _first = new Node(id, name);
  57.         _first->nextNode = _first;
  58.     }
  59. }
  60.  
  61. bool List::pushAfter(int targetId, int id, std::string name)
  62. {
  63.     if (!_first)
  64.         return false;
  65.  
  66.     Node* curr = _first;
  67.     for (; curr->nextNode != _first && curr->getId() != targetId; curr = curr->nextNode);
  68.    
  69.     if (curr->getId() != targetId)
  70.         return false;
  71.  
  72.     Node* node = new Node(curr->nextNode, id, name);
  73.     curr->nextNode = node;
  74.  
  75.     return true;
  76. }
  77.  
  78. bool List::remove(int targetId)
  79. {
  80.     if (!_first)
  81.         return false;
  82.  
  83.     Node* curr = _first;
  84.     for (; curr->nextNode->getId() != targetId && curr->nextNode != _first; curr = curr->nextNode);
  85.  
  86.     if (curr->nextNode->getId() != targetId)
  87.         return false;
  88.  
  89.     Node* next = curr->nextNode->nextNode;
  90.     if (curr->nextNode == _first)
  91.         _first = next;
  92.     delete curr->nextNode;
  93.     curr->nextNode = next;
  94.     return true;
  95. }
  96.  
  97. bool List::swap(int targetId)
  98. {
  99.     if (!_first)
  100.         return false;
  101.  
  102.     Node* curr = _first;
  103.     for (; curr->nextNode->getId() != targetId && curr->nextNode != _first; curr = curr->nextNode);
  104.  
  105.     if (curr->nextNode->getId() != targetId)
  106.         return false;
  107.  
  108.     Node* a = curr->nextNode;
  109.     Node* b = a->nextNode;
  110.     Node* c = b->nextNode;
  111.  
  112.     curr->nextNode = b;
  113.     b->nextNode = a;
  114.     a->nextNode = c;
  115.  
  116.     if (a == _first) _first = b;
  117.     if (b == _first) _first = a;
  118.  
  119.     return true;
  120. }
  121.  
  122. bool List::print()
  123. {
  124.     if (!_first)
  125.         return false;
  126.  
  127.     _first->print();
  128.     for (Node* curr = _first->nextNode; curr != _first; curr = curr->nextNode)
  129.         curr->print();
  130.  
  131.     return true;
  132. }
  133.  
  134. List::~List()
  135. {
  136.     Node* curr = _first;
  137.     Node* next = 0;
  138.  
  139.     while (curr != _first && next)
  140.     {
  141.         next = curr->nextNode;
  142.         delete curr;
  143.         curr = next;
  144.     }
  145. }
  146.  
  147. int main()
  148. {
  149.     List list;
  150.  
  151.     list.push(0, "a");
  152.     list.push(1, "b");
  153.     list.push(2, "c");
  154.     list.push(3, "d");
  155.     /* [0, 1, 2, 3] */
  156.  
  157.     list.remove(2);
  158.     /* [0, 1, 3] */
  159.  
  160.     list.swap(1);
  161.     /* [0, 3, 1] */
  162.  
  163.     list.print();
  164.     return 0;
  165. }
Add Comment
Please, Sign In to add comment