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