Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- 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(0), head(nullptr) {
- while (size != other.size) {
- Node* temp = other.head;
- for (int i = 0; i != other.size - size - 1; i++) {
- temp = temp->next;
- }
- Push_front(temp->value);
- }
- };*/
- ForwardList(ForwardList&& other) : size(other.size), head(other.head) {
- other.size = 0;
- 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;
- }
- }
- size = other.size;
- }
- return *this;
- }
- ForwardList& operator=(ForwardList&& other) {
- if (this != &other) {
- Clear();
- size = other.size;
- head = other.head;
- other.size = 0;
- other.head = nullptr;
- }
- return *this;
- }
- 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() {
- Node* temp = head;
- while (temp != nullptr) {
- cout << temp->value << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- int Size() const {
- return size;
- }
- T Front() const {
- if (head != nullptr) {
- return head->value;
- }
- }
- void Clear() {
- while (head != nullptr) {
- Pop_front();
- }
- }
- 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) {
- 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--;
- }
- }
- ~ForwardList() {
- Clear();
- }
- private:
- struct Node { //односвязный список состоит из узлов
- T value; // узел хранит информативную часть
- Node* next; // и указатель на следующий узел в списке
- };
- int size = 0;
- Node* head = nullptr;
- };
- // реализовать функцию Clear (очистка списка), конструктор по умолчанию, деструктор
- int main() {
- ForwardList<int> 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();
- }
- list1.Insert(2, 12);
- list1.Print();
- list1.Erase(3);
- list1.Print();
- cout << list1.Size() << endl;
- cout << list1.Front() << endl;
- ForwardList<int> list2;
- list2 = move(list1);
- cout << "list2:" << endl;
- list2.Print();
- cout << "list1:" << endl;
- list1.Print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement