Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class List {
- public:
- List() : size_(0), head_(nullptr), last_(nullptr) {}
- List(const List& other) : size_(0), head_(nullptr), last_(nullptr) {
- Node* temp = other.last_;
- while (temp != nullptr) {
- push_front(temp->value);
- temp = temp->prev;
- }
- }
- List(List&& other) : size_(other.size_), head_(other.head_), last_(other.last_) {
- other.head_ = other.last_ = nullptr;
- }
- List& operator=(const List& other) {
- if (this != &other) {
- clear();
- Node* temp = other.last_;
- while (temp != nullptr) {
- push_front(temp->value);
- temp = temp->prev;
- }
- }
- return *this;
- }
- List& operator=(List&& other) {
- if (this != &other) {
- clear();
- size_ = other.size_;
- head_ = other.head_;
- last_ = other.last_;
- other.head_ = other.last_ = nullptr;
- }
- return *this;
- }
- void push_front(const int& value) {
- if (size_ == 0) {
- last_ = head_ = new Node{ value, nullptr, nullptr };
- size_++;
- return;
- }
- Node* temp = new Node{ value, head_, nullptr };
- head_->prev = temp;
- head_ = temp;
- size_++;
- }
- void push_back(const int& value) {
- if (size_ == 0) {
- last_ = head_ = new Node{ value, nullptr, nullptr };
- size_++;
- return;
- }
- Node* temp = new Node{ value, nullptr, last_};
- last_->next = temp;
- last_ = temp;
- size_++;
- }
- void pop_front() {
- if (size_ > 0) {
- if (size_ == 1) {
- delete head_;
- last_ = head_ = nullptr;
- size_--;
- return;
- }
- Node* temp = head_;
- head_ = head_->next;
- delete temp;
- head_->prev = nullptr;
- size_--;
- }
- }
- void pop_back() {
- if (size_ > 0) {
- if (size_ == 1) {
- delete head_;
- last_ = head_ = nullptr;
- size_--;
- return;
- }
- Node* temp = last_;
- last_ = last_->prev;
- delete temp;
- last_->next = nullptr;
- size_--;
- }
- }
- void print() {
- Node* temp = head_;
- while (temp != nullptr) {
- cout << temp->value << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- int size() const {
- return size_;
- }
- void clear() {
- while (head_ != nullptr) {
- pop_front();
- }
- }
- ~List() {
- clear();
- }
- private:
- struct Node { // двусвязный список состоит из узлов
- int value; // узел хранит информативную часть
- Node* next; // указатель на следующий узел в списке
- Node* prev; // указатель на предыдущий узел
- };
- int size_;
- Node* head_;
- Node* last_;
- };
- int main() {
- List list1;
- for (int i = 0; i < 10; i++) {
- list1.push_front(i + 1);
- }
- list1.print();
- cout << list1.size() << endl;
- for (int i = 0; i < 3; i++) {
- list1.pop_front();
- }
- list1.print();
- cout << list1.size() << endl;
- List list2(list1);
- list2.print();
- cout << list2.size() << endl;
- List list3;
- list3 = move(list1);
- list3.print();
- list3.push_back(34);
- list3.push_back(35);
- list3.print();
- list3.pop_back();
- list3.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement