Advertisement
fuad_cs22

Linked List

Jan 25th, 2021
1,210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. /*
  2. Linked list Example by Fuad Hasan
  3. I made it in my style.
  4. Timer class is just to see how much it takes to traverse throw this list.
  5. */
  6.  
  7. #include <iostream>
  8. #include <chrono>
  9. #define DEBUG
  10.  
  11. class Timer
  12. {
  13. public:
  14.     Timer()
  15.     {
  16.         _start_time_point = std::chrono::steady_clock::now();
  17.     }
  18.     ~Timer()
  19.     {
  20.         _end_time_point = std::chrono::steady_clock::now();
  21.         int start = std::chrono::time_point_cast<std::chrono::nanoseconds>(_start_time_point).time_since_epoch().count();
  22.         int end = std::chrono::time_point_cast<std::chrono::nanoseconds>(_end_time_point).time_since_epoch().count();
  23.         std::cout << "Time taken " << end - start << " nanoseconds" << std::endl;
  24.     }
  25. private:
  26.     std::chrono::time_point<std::chrono::steady_clock> _start_time_point;
  27.     std::chrono::time_point<std::chrono::steady_clock> _end_time_point;
  28. };
  29.  
  30. class Node
  31. {
  32. public:
  33.     Node* next;
  34.     int data;
  35. };
  36.  
  37. class list
  38. {
  39. public:
  40.     list() :_head(nullptr), _tail(nullptr), _size(0) {}
  41.     void add(int element)
  42.     {
  43.         Node* temporary_node = new Node;
  44.         temporary_node->data = element;
  45.         temporary_node->next = nullptr;
  46.  
  47.         if (_head == nullptr)
  48.         {
  49.             _head = temporary_node;
  50.             _tail = temporary_node;
  51.         }
  52.         else
  53.         {
  54.             _tail->next = temporary_node;
  55.             _tail = _tail->next;
  56.         }
  57.         _size += 1;
  58.     }
  59.     void print()
  60.     {
  61.         Node* p_node = new Node;
  62.         p_node = _head;
  63.         std::cout << "----------------------" << std::endl;
  64.         while (p_node != nullptr)
  65.         {
  66.             std::cout << p_node->data << std::endl;
  67.             p_node = p_node->next;
  68.         }
  69.         std::cout << "----------------------" << std::endl;
  70.     }
  71.     void pop(size_t index)
  72.     {
  73.         try {
  74.             if (index == 0)
  75.             {
  76.                 _head = _head->next;
  77.                 return;
  78.             }
  79.             else if (index < 0)
  80.             {
  81.                 throw "Index cannot be negative";
  82.                 return;
  83.             }
  84.             else
  85.             {
  86.                 if (index >= _size)
  87.                 {
  88.                     throw "Out of range [Could not remove element.Exiting method.]\n";
  89.                 }
  90.                 Node* removable = _head;
  91.                 Node* temp = _head;
  92.                 while (index-- != 1)
  93.                 {
  94.                     temp = temp->next;
  95.                     removable = temp;
  96.                 }
  97. #ifdef DEBUG
  98.                 std::cerr << "----------------------" << std::endl;
  99.                 std::cout << removable->next->data << "  called to delete" << std::endl;
  100.                 std::cerr << "----------------------" << std::endl;
  101. #endif // DEBUG
  102.                 delete removable->next;
  103.                 removable->next = removable->next->next;
  104.                 return;
  105.             }
  106.         }
  107.         catch (const char* e)
  108.         {
  109.             std::cerr << "----------------------" << std::endl;
  110.             std::cerr << "Error : " << e << std::endl;
  111.             std::cerr << "----------------------" << std::endl;
  112.             return;
  113.         }
  114.     }
  115.     static void concate(list* first, list* second)
  116.     {
  117.         first->_tail->next = second->_head;
  118.         first->_size += second->_size;
  119.     }
  120. public:
  121.     size_t _size;
  122. private:
  123.     Node* _head, * _tail;
  124. };
  125.  
  126. int main()
  127. {
  128.     {
  129.         Timer timer;
  130.         list li;
  131.         li.add(1);
  132.         li.add(2);
  133.         li.add(3);
  134.         li.print();
  135.         list li2;
  136.         li2.add(6);
  137.         li2.add(9);
  138.         li2.add(13);
  139.         li2.print();
  140.         list::concate(&li, &li2);
  141.         li.pop(4);
  142.         li.print();
  143.     }
  144.     std::cin.get();
  145. }
  146.  
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement