Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- template <typename T>
- 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 T& 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 T& 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) {
- if (index == 0) {
- pop_front();
- return;
- }
- if (index > 0 && index < size_) {
- Node* temp = head_;
- for (int i = 0; i < index - 1; i++) {
- temp = temp->next;
- }
- Node* buf = temp->next->next;
- delete temp->next;
- temp->next = buf;
- size_--;
- }
- }
- T& operator[] (const int index) {
- if (index >= 0 && index < size_) {
- Node* temp = head_;
- for (int i = 0; i < index; i++) {
- temp = temp->next;
- }
- return temp->value;
- }
- }
- const T& operator[] (const int index) const {
- if (index >= 0 && index < size_) {
- Node* temp = head_;
- for (int i = 0; i < index; i++) {
- temp = temp->next;
- }
- return temp->value;
- }
- }
- bool operator==(const ForwardList& other) const {
- if (size_ != other.size_) {
- return false;
- }
- Node* temp = head_;
- Node* other_temp = other.head_;
- while (temp != nullptr) {
- if (temp->value != other_temp->value) {
- return false;
- }
- temp = temp->next;
- other_temp = other_temp->next;
- }
- return true;
- }
- bool operator!=(const ForwardList& other) const {
- return !(*this == other);
- }
- int size() const {
- return size_;
- }
- int find(const T& value) const {
- int index = 0;
- Node* temp = head_;
- while (temp != nullptr && temp->value != value) {
- temp = temp->next;
- index++;
- }
- if (temp != nullptr) {
- return index;
- }
- return -1;
- }
- T& front() {
- if (head_ != nullptr) {
- return head_->value;
- }
- }
- const T& front() const {
- if (head_ != nullptr) {
- return head_->value;
- }
- }
- ~ForwardList() {
- clear();
- }
- private:
- struct Node { // односвязный список состоит из узлов
- T value; // узел хранит информативную часть
- Node* next; // указатель на следующий узел
- };
- int size_;
- Node* head_;
- };
- int main() {
- ForwardList<int> list1;
- for (int i = 0; i < 10; i++) {
- list1.push_front(i + 1);
- }
- list1.print();
- cout << list1.size() << endl;
- const ForwardList<int> list2(list1);
- ForwardList<int> list3;
- list3 = move(list1);
- list3.print();
- list3.insert(2, 563);
- list3.print();
- list3.erase(5);
- list3.print();
- cout << list3[2] << endl;
- cout << list2[2] << endl;
- cout << (list2 == list3) << endl;
- cout << (list2 != list3) << endl;
- cout << list3.find(8) << endl;
- cout << list3.find(25) << endl;
- cout << list3.front() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement