Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct element {
- char x;
- element* next = nullptr;
- };
- class List {
- element* head;
- int size;
- public:
- List() { head = nullptr; size = 0; };
- ~List();
- char& operator[](int index);
- int pop();
- void pushBack(char x);
- void pushToHead(char x);
- void equal(List *L2);
- void cleaner();
- void print();
- private:
- };
- char &List::operator[](int index) {
- element* current = head;
- int counter = 0;
- while (current != nullptr) {
- if (counter == index) {
- return current->x;
- }
- current = current->next;
- counter++;
- }
- }
- List::~List() {
- while (head != nullptr) {
- element* temp = head->next;
- delete head;
- head = temp;
- }
- };
- int List::pop() {
- if (size == 0) { return (int()); }
- if (size == 1) { return head->x; head = nullptr; }
- else {
- element* current = head;
- for (int i = 0; i < size - 2; i++) { current = current->next; }
- int i = (*this)[size - 1];
- current->next = current->next->next;
- size--;
- return i;
- }
- }
- void List::pushBack(char x) {
- size++;
- if (head == nullptr) {
- head = new element;
- head->x = x;
- }
- else {
- element* current = head;
- while (current->next != nullptr) {
- current = current->next;
- }
- current->next = new element;
- current->next->x = x;
- }
- };
- void List::pushToHead(char x) {
- size++;
- element* temp = new element;
- temp->x = x;
- temp->next = head;
- head = temp;
- };
- void List::print() {
- element* temp = head;
- while (temp != nullptr) {
- cout << temp->x << " ";
- temp = temp->next;
- }
- printf("\n");
- };
- void List::cleaner() {
- List* nList = new List();
- element* current = this->head;
- for (int i = 0; i < size; i++) {
- if (isalpha(current->x)) { nList->pushBack(current->x); }
- current = current->next;
- }
- *this = *nList;
- }
- void List::equal(List* L2) {
- bool status = true;
- if (this->size != L2->size)
- {
- status = false;
- }
- element* current1 = this->head;
- element* current2 = L2->head;
- while (current1 != nullptr && current2 != nullptr)
- {
- if (current1->x == current2->x) {
- current1 = current1->next;
- current2 = current2->next;
- }
- else {
- status = false;
- }
- }
- if (status == true) {
- cout << "EQUALITY = " << status << endl;
- }
- }
- int main() {
- setlocale(0, "");
- List L1;
- List L2;
- L1.pushBack('A');
- L1.pushBack('B');
- L1.pushBack('C');
- L2.pushBack('A');
- L2.pushBack('B');
- L2.pushBack('C');
- L1.equal(&L2);
- L1.print();
- L2.print();
- printf("Добавим целочисленный элемент с конца и попробуем его удалить\n");
- L1.pushBack('1');
- L1.print();
- L1.cleaner();
- L1.print();
- printf("Реализуем вставку последнего символа L2 в L1\n");
- L1.pushBack(L2.pop());
- L1.print();
- return 0;
- }
Add Comment
Please, Sign In to add comment