123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #pragma once
- #include <iostream>
- template <typename T> class LinkedListNode;
- class LinkedListException {};
- template <typename T> class LinkedList {
- bool isCircular;
- LinkedListNode<T>* start;
-
-
-
-
- LinkedList(const LinkedList& list);
-
-
-
-
-
- LinkedList& operator =(const LinkedList& list);
- public:
- LinkedList();
- ~LinkedList();
-
-
-
-
- LinkedListNode<T>* getStart();
-
-
-
- void deleteFirst();
-
-
-
-
- void deleteAfter(LinkedListNode<T>* ptr);
-
-
-
-
- void insertFirst(const T& value);
-
-
-
-
-
- void insertAfter(LinkedListNode<T>* ptr, const T& value);
-
-
-
- void makeCircular();
-
-
-
-
-
-
-
- template <typename U> friend std::ostream& operator <<(std::ostream& out, LinkedList<U>& list);
- };
- template <typename T> LinkedList<T>::LinkedList()
- : start(nullptr), isCircular(false) {}
- template <typename T> LinkedList<T>::~LinkedList() {
- if (isCircular) {
- LinkedListNode<T>* current = start;
- while (current->_next != start) {
- current = current->_next;
- }
- current->_next = nullptr;
- }
- while (start) {
- deleteFirst();
- }
- }
- template <typename T> LinkedListNode<T>* LinkedList<T>::getStart() {
- return start;
- }
- template <typename T> void LinkedList<T>::deleteFirst() {
- if (start) {
- LinkedListNode<T>* temp = start->_next;
- delete start;
- start = temp;
- }
- else {
- throw LinkedListException();
- }
- }
- template <typename T> void LinkedList<T>::deleteAfter(LinkedListNode<T>* ptr) {
- if (ptr && ptr->_next) {
- if (ptr->_next == start) {
- LinkedListNode<T>* temp = ptr->_next;
- ptr->_next = ptr->_next->_next;
- delete temp;
- start = ptr->_next;
- }
- else {
- LinkedListNode<T>* temp = ptr->_next;
- ptr->_next = ptr->_next->_next;
- delete temp;
- }
- }
- else {
- throw LinkedListException();
- }
- }
- template <typename T> void LinkedList<T>::insertFirst(const T& value) {
- LinkedListNode<T>* second = start;
- start = new LinkedListNode<T>(value, second);
- }
- template <typename T> void LinkedList<T>::insertAfter(LinkedListNode<T>* ptr, const T& value) {
- if (ptr) {
- LinkedListNode<T>* temp = ptr->_next;
- ptr->_next = new LinkedListNode<T>(value, temp);
- }
- }
- template <typename T> std::ostream& operator<<(std::ostream& out, LinkedList<T>& list) {
- LinkedListNode<T>* ptr = list.getStart();
- if (!ptr) {
- out << "EMPTY ";
- }
- else {
- do {
- out << ptr->getValue() << ' ';
- ptr = ptr->getNext();
- } while (ptr && ptr != list.getStart());
- }
- return out;
- }
- template <typename T> void LinkedList<T>::makeCircular() {
- if (start == nullptr) {
- return;
- }
- LinkedListNode<T>* current = start;
- while (current->_next != nullptr) {
- current = current->_next;
- }
- current->_next = start;
- isCircular = true;
- }
|