Advertisement
Guest User

Linked List

a guest
Oct 23rd, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #include "List.h"
  6.  
  7. //tail трябва да сочи към последният валиден елемент в списъка,
  8. //или към nullptr ако списъкът е празен.
  9.  
  10. //Съответно head трябва винаги да сочи към първият елемент в списъка или към nullptr.
  11.  
  12. List::Node::Node(int value, Node * prev, Node * next) {
  13. }
  14.  
  15. int List::Node::getValue() const {
  16.     return this->value;
  17. }
  18. void List::Node::setValue(int value) {
  19.     this->value = value;
  20. }
  21.  
  22. //  Node * getNext() const;
  23. List::Node* List::Node::getNext() const {
  24.     return this->next;
  25. }
  26. void List::Node::setNext(Node * next) {
  27.     this->next = next;
  28. }
  29.  
  30. //   Node * getPrev() const;
  31. List::Node* List::Node::getPrev() const {
  32.     return this->prev;
  33. }
  34. void List::Node::setPrev(Node * prev) {
  35.     this->prev = prev;
  36. }
  37.  
  38. //Node * head;
  39. //Node * tail;
  40. //size_t size;
  41.  
  42. List::List() {    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
  43.     this->size = 0;
  44.     this->head = nullptr;
  45.     this->tail = nullptr;
  46. }
  47.  
  48. List::List(const List& other) {  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  49.     if (other.head == nullptr) {
  50.         this->head = nullptr;
  51.         this->tail = nullptr;
  52.         this->size = 0;
  53.     }
  54.     else {
  55.         Node * currPtr = other.head;  //set to first link
  56.         this->size = 0;
  57.         this->head = nullptr;
  58.         this->tail = nullptr;
  59.  
  60.         while (currPtr != nullptr) {
  61.             //create a new Node to enter into the new List
  62.             Node* newNode = new Node(currPtr->getValue(), nullptr, nullptr);
  63.             //add newNode to new List
  64.             if (this->size == 0) { //new list is empty
  65.                 this->head = this->tail = newNode;
  66.             }
  67.             else { //add newNode to end of new List
  68.                 this->tail->setNext(newNode);
  69.                 newNode->setPrev(this->tail);
  70.                 this->tail = this->tail->getNext();
  71.             }
  72.             this->size++;
  73.             currPtr = currPtr->getNext();
  74.         }
  75.     }
  76. }
  77.  
  78. int List::first() const {     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
  79.     if (this->size > 0) {
  80.         return this->head->getValue();  //head E на първи елемент !!!!!!!!!!!
  81.     }
  82.     else {
  83.         return -1;
  84.     }
  85. }
  86. void List::add(int value) {  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
  87.     Node* newNode = new Node(value, nullptr, nullptr);
  88.     if (this->size == 0) {
  89.         this->head = newNode;
  90.         this->tail = newNode;
  91.     }
  92.     else {
  93.         this->tail->setNext(newNode);
  94.         this->tail = this->tail->getNext();
  95.     }
  96.     this->size++;
  97. }
  98. void List::addAll(const List& other) {
  99.  
  100. }
  101.  
  102. void List::removeFirst() {    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  103.     if (this->size > 0) {
  104.         Node *temp = this->head;
  105.         this->head = temp->getNext();
  106.         delete temp;
  107.         temp = nullptr;
  108.         this->size--;
  109.     }
  110. }
  111. void List::removeAll() {
  112.     while (this->size) {    // или while (!isEmpty())
  113.         this->removeFirst();
  114.         //this->size--;
  115.     }
  116. }
  117.  
  118. size_t List::getSize() const {   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
  119.     return this->size;
  120. }
  121. bool List::isEmpty() const {   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
  122.     return !(this->size);
  123. }
  124.  
  125. List List::getReversed(List l) {
  126.     //TO DO
  127.     return l;
  128. }
  129.  
  130. std::string List::toString() const {
  131.     //TO DO
  132.     std::string s;
  133.     return s;
  134. }
  135.  
  136. List& List::operator<<(const int& value) {  //!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
  137.  
  138.     Node* newNode = new Node(value, nullptr, nullptr);
  139.     if (this->size == 0) {
  140.         this->head = newNode;
  141.         this->tail = newNode;
  142.     }
  143.     else {
  144.         this->tail->setNext(newNode);
  145.         this->tail = this->tail->getNext();
  146.     }
  147.     this->size++;
  148.     return *this;
  149. }
  150. List& List::operator<<(const List& other) {
  151.     //TO DO
  152.     List l;
  153.     return l;
  154. }
  155.  
  156. List& List::operator=(const List& other) {    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVv
  157.     //1.DeleteAll:
  158.     Node * temp = this->head;
  159.     Node  * prevPtr = nullptr;
  160.     while (temp->getNext() != nullptr) {
  161.         prevPtr = temp;
  162.         temp = this->head->getNext();
  163.         delete prevPtr;
  164.         prevPtr = nullptr;
  165.     }
  166.     delete temp;
  167.     temp = nullptr;
  168.     this->head = nullptr;
  169.     this->tail = nullptr;
  170.     this->size = 0;
  171.  
  172.     //2.Copy
  173.     Node * currPtr = other.head;  //set to first link
  174.     while (currPtr != nullptr) {
  175.         currPtr = currPtr->getNext(); // move to next link
  176.     }
  177.     this->head = currPtr;
  178.     return *this;
  179. }
  180.  
  181. List::~List() {    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  182.     Node * temp = this->head;
  183.     Node  * prevPtr = nullptr;
  184.     while (temp->getNext() != nullptr) {
  185.         prevPtr = temp;
  186.         temp = this->head->getNext();
  187.         delete prevPtr;
  188.         prevPtr = nullptr;
  189.     }
  190.     delete temp;
  191.     temp = nullptr;
  192.     this->head = nullptr;
  193.     this->tail = nullptr;
  194.     this->size = 0;
  195.     //ИЛИ:
  196.     //this->removeAll();
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement