Argentum_02

LinkedList.cpp

Jul 15th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include "test_runner.h"
  2.  
  3. #include <vector>
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class LinkedList {
  8. public:
  9.   struct Node {
  10.     T value;
  11.     Node* next = nullptr;
  12.   };
  13.  
  14.   ~LinkedList(){
  15.       while(head!=nullptr){
  16.           Node *space = head->next;
  17.           delete head;
  18.           head = space;
  19.       }
  20.   }
  21.  
  22.   void PushFront(const T& value){
  23.       Node *space = new Node;
  24.       space->value = value;
  25.       space->next = head;
  26.       head = space;
  27.   }
  28.   void InsertAfter(Node* node, const T& value){
  29.       if(node==nullptr){
  30.          PushFront(value);
  31.          return;
  32.       }
  33.  
  34.           Node *space = new Node;
  35.           space->value = value;
  36.           space->next = node->next;
  37.           node->next = space;
  38.  
  39.   }
  40.   void RemoveAfter(Node* node){
  41.       if(head == nullptr){
  42.           PopFront();
  43.           return;
  44.       }
  45.       if(node->next == nullptr){
  46.           return;
  47.       }
  48.       Node *space = node->next;
  49.       node->next = space->next;
  50.       delete space;
  51.   }
  52.   void PopFront(){
  53.       if(head == nullptr){
  54.           return;
  55.       }
  56.       if(head->next == nullptr){
  57.           delete head;
  58.           head = nullptr;
  59.           return;
  60.       }
  61.       Node* space = head->next;
  62.       delete head;
  63.       head = space;
  64.   }
  65.  
  66.   Node* GetHead() { return head; }
  67.   const Node* GetHead() const { return head; }
  68.  
  69. private:
  70.   Node* head = nullptr;
  71. };
  72.  
  73. template <typename T>
  74. vector<T> ToVector(const LinkedList<T>& list) {
  75.   vector<T> result;
  76.   for (auto node = list.GetHead(); node; node = node->next) {
  77.     result.push_back(node->value);
  78.   }
  79.   return result;
  80. }
  81.  
  82. void TestPushFront() {
  83.   LinkedList<int> list;
  84.   list.PushFront(1);
  85.   ASSERT_EQUAL(list.GetHead()->value, 1);
  86.   list.PushFront(2);
  87.   ASSERT_EQUAL(list.GetHead()->value, 2);
  88.   list.PushFront(3);
  89.   ASSERT_EQUAL(list.GetHead()->value, 3);
  90.  
  91.   const vector<int> expected = {3, 2, 1};
  92.   ASSERT_EQUAL(ToVector(list), expected);
  93. }
  94.  
  95. void TestInsertAfter() {
  96.   LinkedList<string> list;
  97.  
  98.   list.InsertAfter(list.GetHead(),"a");
  99.   //list.PushFront("a");
  100.   auto head = list.GetHead();
  101.   ASSERT(head);
  102.   ASSERT_EQUAL(head->value, "a");
  103.  
  104.   list.InsertAfter(head, "b");
  105.   const vector<string> expected1 = {"a", "b"};
  106.   ASSERT_EQUAL(ToVector(list), expected1);
  107.  
  108.   list.InsertAfter(head, "c");
  109.   const vector<string> expected2 = {"a", "c", "b"};
  110.   ASSERT_EQUAL(ToVector(list), expected2);
  111. }
  112.  
  113. void TestRemoveAfter() {
  114.   LinkedList<int> list;
  115.   for (int i = 1; i <= 5; ++i) {
  116.     list.PushFront(i);
  117.   }
  118.  
  119.   const vector<int> expected = {5, 4, 3, 2, 1};
  120.   ASSERT_EQUAL(ToVector(list), expected);
  121.  
  122.   auto next_to_head = list.GetHead()->next;
  123.   list.RemoveAfter(next_to_head); // удаляем 3
  124.   list.RemoveAfter(next_to_head); // удаляем 2
  125.  
  126.   const vector<int> expected1 = {5, 4, 1};
  127.   ASSERT_EQUAL(ToVector(list), expected1);
  128.  
  129.   while (list.GetHead()->next) {
  130.     list.RemoveAfter(list.GetHead());
  131.   }
  132.   ASSERT_EQUAL(list.GetHead()->value, 5);
  133. }
  134.  
  135. void TestPopFront() {
  136.   LinkedList<int> list;
  137.  
  138.   for (int i = 1; i <= 5; ++i) {
  139.     list.PushFront(i);
  140.   }
  141.   for (int i = 1; i <= 5; ++i) {
  142.     list.PopFront();
  143.   }
  144.   ASSERT(list.GetHead() == nullptr);
  145. }
  146.  
  147. int main() {
  148.   TestRunner tr;
  149.   RUN_TEST(tr, TestPushFront);
  150.   RUN_TEST(tr, TestInsertAfter);
  151.   RUN_TEST(tr, TestRemoveAfter);
  152.   RUN_TEST(tr, TestPopFront);
  153.   LinkedList<int> a;
  154.  
  155.   return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment