Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright [2017] <Jean Leopoldo>
- #ifndef STRUCTURES_CIRCULAR_LIST_H
- #define STRUCTURES_CIRCULAR_LIST_H
- #include <cstdint>
- #include <stdexcept>
- namespace structures {
- template<typename T>
- //! class CircularList
- /*
- * class CircularList
- */
- class CircularList {
- public:
- //! constructor CircularList
- /*
- * constructor CircularList
- */
- CircularList() {
- head = nullptr;
- tail = nullptr;
- size_ = 0;
- }
- //! destructor CircularList
- /*
- * destructor CircularList
- */
- ~CircularList() {
- clear();
- }
- //! clear CircularList
- /*
- * clear CircularList
- */
- void clear() {
- while (!empty()) {
- pop_front();
- }
- head = nullptr;
- tail = nullptr;
- size_ = 0;
- }
- //! push_back
- /*
- * push_back
- */
- void push_back(const T& data) {
- Node *node = new Node(data);
- if (node == nullptr) {
- throw std::out_of_range("full");
- } else if (empty()) {
- push_front(data);
- } else {
- Node *aux = head;
- for (int i = 0; i < size_-1; i++) {
- aux = aux->next();
- }
- size_++;
- aux->next(node);
- node->next(head);
- }
- }
- //! push_front
- /*
- * push_front
- */
- void push_front(const T& data) {
- Node *node = new Node(data, head);
- if (node == nullptr) {
- throw std::out_of_range("full");
- } else if (empty()) {
- size_++;
- head = node;
- tail = node;
- node->next(node);
- } else {
- size_++;
- head = node;
- tail->next(node);
- }
- }
- //! insert
- /*
- * insert
- */
- void insert(const T& data, std::size_t index) {
- if (empty() || index == 1) {
- push_front(data);
- } else if (index == size_) {
- push_back(data);
- } else {
- Node *node = new Node(data);
- if (node == nullptr) {
- throw std::out_of_range("full");
- } else {
- Node *aux = head;
- for (int i = 0; i < index; i++) {
- aux = aux->next();
- }
- node->next(aux->next());
- aux->next(node);
- delete aux;
- }
- }
- }
- //! insert
- /*
- * insert
- */
- void insert_sorted(const T& data) {
- sort();
- Node *aux = head;
- int i = 0;
- for (; aux->data() < data; i++) {
- aux = aux->next();
- }
- insert(data, i);
- }
- //! insert
- /*
- * insert
- */
- T& at(std::size_t index) {
- if (empty()) {
- throw std::out_of_range("empty");
- } else {
- Node *aux = head;
- for (int i = 0; i < index; i++) {
- aux = aux->next();
- }
- T& data = aux->data();
- delete aux;
- return data;
- }
- }
- //! insert
- /*
- * insert
- */
- const T& at(std::size_t index) const {
- if (empty()) {
- throw std::out_of_range("empty");
- } else {
- Node *aux = head;
- T& data;
- for (int i = 0; i < index; i++) {
- aux = aux->next();
- }
- data = aux->data;
- delete aux;
- return data;
- }
- }
- //! insert
- /*
- * insert
- */
- T pop(std::size_t index) {
- if (empty()) {
- throw std::out_of_range("empty");
- } else if (index == 1) {
- return pop_front();
- } else {
- Node *aux = head;
- Node *del;
- T data;
- for (int i = 0; i < index-1; i++) {
- aux = aux->next();
- }
- del = aux->next();
- data = del->data();
- aux->next(del->next());
- delete aux;
- delete del;
- return data;
- }
- }
- //! insert
- /*
- * insert
- */
- T pop_back() {
- return pop(size_);
- }
- //! insert
- /*
- * insert
- */
- T pop_front() {
- if (empty())
- throw std::out_of_range("empty");
- Node *aux = head;
- head = aux->next();
- T data = aux->data();
- delete aux;
- return data;
- }
- //! insert
- /*
- * insert
- */
- void remove(const T& data) {
- Node *aux = head;
- for (int i = 0; i < size_-1; i++) {
- if (aux->data() == data) {
- pop(i);
- }
- }
- throw std::out_of_range("data has not been found");
- }
- //! insert
- /*
- * insert
- */
- bool empty() const {
- if (size_ == 0)
- return true;
- return false;
- }
- //! insert
- /*
- * insert
- */
- bool contains(const T& data) const {
- if (empty())
- throw std::out_of_range("empty");
- Node *aux = head;
- for (int i = 0; i < size_-1; i++) {
- if (aux->data() == data)
- return true;
- aux = aux->next();
- }
- throw std::out_of_range("data has not been found");
- }
- //! insert
- /*
- * insert
- */
- std::size_t find(const T& data) const {
- if (empty())
- throw std::out_of_range("empty");
- Node *aux = head;
- for (int i = 0; i < size_-1; i++) {
- if (aux->data() == data)
- return i;
- aux = aux->next();
- }
- throw std::out_of_range("data has not been found");
- }
- //! insert
- /*
- * insert
- */
- std::size_t size() const {
- return size_;
- }
- private:
- class Node {
- public:
- explicit Node(const T& data) {
- data_ = data;
- next_ = nullptr;
- }
- Node(const T& data, Node *next) {
- data_ = data;
- next_ = next;
- }
- Node *next() {
- return next_;
- }
- void next(Node *next) {
- next_ = next;
- }
- T& data() {
- return data_;
- }
- private:
- Node *next_;
- T data_;
- };
- std::size_t size_;
- Node *head;
- Node *tail;
- void sort() {
- Node *aux = head;
- for (int i = 0; i < size_-1; i++) {
- T data = aux->data();
- for (int j = 0; j < size_ -1; j++) {
- aux = aux->next();
- if (aux->data() > data) {
- data = aux->data();
- }
- }
- }
- }
- };
- } // namespace structures
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement