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