Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include "List.h"
- //tail трябва да сочи към последният валиден елемент в списъка,
- //или към nullptr ако списъкът е празен.
- //Съответно head трябва винаги да сочи към първият елемент в списъка или към nullptr.
- List::Node::Node(int value, Node * prev, Node * next) {
- }
- int List::Node::getValue() const {
- return this->value;
- }
- void List::Node::setValue(int value) {
- this->value = value;
- }
- // Node * getNext() const;
- List::Node* List::Node::getNext() const {
- return this->next;
- }
- void List::Node::setNext(Node * next) {
- this->next = next;
- }
- // Node * getPrev() const;
- List::Node* List::Node::getPrev() const {
- return this->prev;
- }
- void List::Node::setPrev(Node * prev) {
- this->prev = prev;
- }
- //Node * head;
- //Node * tail;
- //size_t size;
- List::List() { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
- this->size = 0;
- this->head = nullptr;
- this->tail = nullptr;
- }
- List::List(const List& other) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- if (other.head == nullptr) {
- this->head = nullptr;
- this->tail = nullptr;
- this->size = 0;
- }
- else {
- Node * currPtr = other.head; //set to first link
- this->size = 0;
- this->head = nullptr;
- this->tail = nullptr;
- while (currPtr != nullptr) {
- //create a new Node to enter into the new List
- Node* newNode = new Node(currPtr->getValue(), nullptr, nullptr);
- //add newNode to new List
- if (this->size == 0) { //new list is empty
- this->head = this->tail = newNode;
- }
- else { //add newNode to end of new List
- this->tail->setNext(newNode);
- newNode->setPrev(this->tail);
- this->tail = this->tail->getNext();
- }
- this->size++;
- currPtr = currPtr->getNext();
- }
- }
- }
- int List::first() const { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
- if (this->size > 0) {
- return this->head->getValue(); //head E на първи елемент !!!!!!!!!!!
- }
- else {
- return -1;
- }
- }
- void List::add(int value) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
- Node* newNode = new Node(value, nullptr, nullptr);
- if (this->size == 0) {
- this->head = newNode;
- this->tail = newNode;
- }
- else {
- this->tail->setNext(newNode);
- this->tail = this->tail->getNext();
- }
- this->size++;
- }
- void List::addAll(const List& other) {
- }
- void List::removeFirst() { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
- if (this->size > 0) {
- Node *temp = this->head;
- this->head = temp->getNext();
- delete temp;
- temp = nullptr;
- this->size--;
- }
- }
- void List::removeAll() {
- while (this->size) { // или while (!isEmpty())
- this->removeFirst();
- //this->size--;
- }
- }
- size_t List::getSize() const { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
- return this->size;
- }
- bool List::isEmpty() const { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
- return !(this->size);
- }
- List List::getReversed(List l) {
- //TO DO
- return l;
- }
- std::string List::toString() const {
- //TO DO
- std::string s;
- return s;
- }
- List& List::operator<<(const int& value) { //!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVV
- Node* newNode = new Node(value, nullptr, nullptr);
- if (this->size == 0) {
- this->head = newNode;
- this->tail = newNode;
- }
- else {
- this->tail->setNext(newNode);
- this->tail = this->tail->getNext();
- }
- this->size++;
- return *this;
- }
- List& List::operator<<(const List& other) {
- //TO DO
- List l;
- return l;
- }
- List& List::operator=(const List& other) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVv
- //1.DeleteAll:
- Node * temp = this->head;
- Node * prevPtr = nullptr;
- while (temp->getNext() != nullptr) {
- prevPtr = temp;
- temp = this->head->getNext();
- delete prevPtr;
- prevPtr = nullptr;
- }
- delete temp;
- temp = nullptr;
- this->head = nullptr;
- this->tail = nullptr;
- this->size = 0;
- //2.Copy
- Node * currPtr = other.head; //set to first link
- while (currPtr != nullptr) {
- currPtr = currPtr->getNext(); // move to next link
- }
- this->head = currPtr;
- return *this;
- }
- List::~List() { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
- Node * temp = this->head;
- Node * prevPtr = nullptr;
- while (temp->getNext() != nullptr) {
- prevPtr = temp;
- temp = this->head->getNext();
- delete prevPtr;
- prevPtr = nullptr;
- }
- delete temp;
- temp = nullptr;
- this->head = nullptr;
- this->tail = nullptr;
- this->size = 0;
- //ИЛИ:
- //this->removeAll();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement