Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Linked list Example by Fuad Hasan
- I made it in my style.
- Timer class is just to see how much it takes to traverse throw this list.
- */
- #include <iostream>
- #include <chrono>
- #define DEBUG
- class Timer
- {
- public:
- Timer()
- {
- _start_time_point = std::chrono::steady_clock::now();
- }
- ~Timer()
- {
- _end_time_point = std::chrono::steady_clock::now();
- int start = std::chrono::time_point_cast<std::chrono::nanoseconds>(_start_time_point).time_since_epoch().count();
- int end = std::chrono::time_point_cast<std::chrono::nanoseconds>(_end_time_point).time_since_epoch().count();
- std::cout << "Time taken " << end - start << " nanoseconds" << std::endl;
- }
- private:
- std::chrono::time_point<std::chrono::steady_clock> _start_time_point;
- std::chrono::time_point<std::chrono::steady_clock> _end_time_point;
- };
- class Node
- {
- public:
- Node* next;
- int data;
- };
- class list
- {
- public:
- list() :_head(nullptr), _tail(nullptr), _size(0) {}
- void add(int element)
- {
- Node* temporary_node = new Node;
- temporary_node->data = element;
- temporary_node->next = nullptr;
- if (_head == nullptr)
- {
- _head = temporary_node;
- _tail = temporary_node;
- }
- else
- {
- _tail->next = temporary_node;
- _tail = _tail->next;
- }
- _size += 1;
- }
- void print()
- {
- Node* p_node = new Node;
- p_node = _head;
- std::cout << "----------------------" << std::endl;
- while (p_node != nullptr)
- {
- std::cout << p_node->data << std::endl;
- p_node = p_node->next;
- }
- std::cout << "----------------------" << std::endl;
- }
- void pop(size_t index)
- {
- try {
- if (index == 0)
- {
- _head = _head->next;
- return;
- }
- else if (index < 0)
- {
- throw "Index cannot be negative";
- return;
- }
- else
- {
- if (index >= _size)
- {
- throw "Out of range [Could not remove element.Exiting method.]\n";
- }
- Node* removable = _head;
- Node* temp = _head;
- while (index-- != 1)
- {
- temp = temp->next;
- removable = temp;
- }
- #ifdef DEBUG
- std::cerr << "----------------------" << std::endl;
- std::cout << removable->next->data << " called to delete" << std::endl;
- std::cerr << "----------------------" << std::endl;
- #endif // DEBUG
- delete removable->next;
- removable->next = removable->next->next;
- return;
- }
- }
- catch (const char* e)
- {
- std::cerr << "----------------------" << std::endl;
- std::cerr << "Error : " << e << std::endl;
- std::cerr << "----------------------" << std::endl;
- return;
- }
- }
- static void concate(list* first, list* second)
- {
- first->_tail->next = second->_head;
- first->_size += second->_size;
- }
- public:
- size_t _size;
- private:
- Node* _head, * _tail;
- };
- int main()
- {
- {
- Timer timer;
- list li;
- li.add(1);
- li.add(2);
- li.add(3);
- li.print();
- list li2;
- li2.add(6);
- li2.add(9);
- li2.add(13);
- li2.print();
- list::concate(&li, &li2);
- li.pop(4);
- li.print();
- }
- std::cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement