Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- #include <iostream>
- using namespace std;
- const string HELP_MSG = "0 – exit\n1 – add to begin\n2 – add to end\n3 – remove from begin\n4 – remove from end\n5 - print list\n6 – swap\n\n";
- struct Node
- {
- int value;
- Node *next = NULL;
- };
- //создать новый лист с одним элементом
- Node *init(int a)
- {
- Node *node = new Node;
- node->value = a;
- return (node);
- };
- void print(Node *list)
- {
- Node *node = list;
- while (node != NULL)
- {
- cout << node->value << " ";
- node = node->next;
- }
- cout << endl;
- };
- Node *push_back(Node *list, int a)
- {
- if (list == NULL)
- {
- return init(a);
- }
- Node *new_node = new Node;
- new_node->value = a;
- new_node->next = list;
- return new_node;
- }
- Node *push_front(Node *list, int a)
- {
- if (list == NULL)
- {
- return init(a);
- }
- Node *new_node = new Node;
- new_node->value = a;
- Node *t = list;
- while (t->next != NULL)
- {
- t = t->next;
- }
- t->next = new_node;
- return list;
- }
- Node *pull_back(Node *list)
- {
- if (list == NULL)
- {
- cout << "oops! error =(" << endl;
- exit(1);
- }
- return list->next;
- }
- Node *pull_front(Node *list)
- {
- if (list == NULL)
- {
- cout << "oops! error =(" << endl;
- exit(1);
- }
- if (list->next == NULL)
- return NULL;
- Node *t = list;
- while (t->next->next != NULL)
- {
- t = t->next;
- }
- t->next = NULL;
- return list;
- }
- int size(Node *list)
- {
- int s = 0;
- Node *t = list;
- while (t != NULL)
- {
- t = t->next;
- s++;
- }
- return s;
- }
- Node *get(Node *list, int index)
- {
- if (size(list) <= index)
- {
- cout << "oops! error =(" << endl;
- exit(1);
- }
- int i = 0;
- Node *t = list;
- while (i != index)
- {
- t = t->next;
- i++;
- }
- return t;
- };
- Node *swap(Node *list, int first_index, int second_index)
- {
- int n = size(list);
- if (first_index < 0 || first_index >= n || second_index < 0 || second_index >= n)
- {
- cout << "oops! error =(" << endl;
- exit(1);
- }
- if (first_index == second_index)
- {
- return list;
- }
- if (first_index > second_index)
- {
- int buf = first_index;
- first_index = second_index;
- second_index = buf;
- }
- Node *first = get(list, first_index);
- Node *second = get(list, second_index);
- if (first_index == 0)
- {
- // F S ... (n - 1)
- if (second_index == first_index + 1)
- {
- Node *second_next = second->next;
- second->next = first;
- first->next = second_next;
- list = second;
- }
- // F ... S
- else if (second_index == n - 1)
- {
- Node *second_prev = get(list, second_index - 1);
- Node *first_next = first->next;
- second->next = first_next;
- second_prev->next = first;
- first->next = NULL;
- list = second;
- }
- // F ... S ... (n - 1)
- else
- {
- Node *second_prev = get(list, second_index - 1);
- Node *second_next = second->next;
- Node *first_next = first->next;
- second->next = first_next;
- second_prev = first;
- first->next = second->next;
- list = second;
- }
- }
- else if (first_index > 0)
- {
- // 0 ... F S
- if (first_index == n - 2 && second_index == n - 1)
- {
- Node *first_prev = get(list, first_index - 1);
- first_prev->next = second;
- second->next = first;
- first->next = NULL;
- }
- // 0 ... F ... S
- else if (second_index == n - 1)
- {
- Node *first_prev = get(list, first_index - 1);
- Node *first_next = first->next;
- Node *second_prev = get(list, second_index - 1);
- first_prev->next = second;
- second->next = first_next;
- second_prev->next = first;
- first->next = NULL;
- }
- // 0 ... F S ... (n - 1)
- else if (second_index == first_index + 1)
- {
- Node *first_prev = get(list, first_index - 1);
- Node *second_next = second->next;
- first_prev->next = second;
- second->next = first;
- first->next = second_next;
- }
- // 0 ... F ... S ... (n - 1)
- else
- {
- Node *first_prev = get(list, first_index - 1);
- Node *first_next = first->next;
- Node *second_prev = get(list, second_index - 1);
- Node *second_next = second->next;
- first_prev->next = second;
- second->next = first_next;
- second_prev->next = first;
- first->next = second_next;
- }
- }
- return list;
- };
- Node *swap35(Node *list)
- {
- int list_size = size(list);
- if (list_size < 2)
- {
- return list;
- }
- int first_index = -1;
- int second_index = -1;
- Node *t = list;
- int i = 0;
- while (t != NULL)
- {
- if (t->value % 3 == 0)
- {
- first_index = i;
- break;
- }
- i++;
- t = t->next;
- }
- t = list;
- i = 0;
- while (t != NULL)
- {
- if (t->value % 5 == 0)
- {
- second_index = i;
- break;
- }
- i++;
- t = t->next;
- }
- if (first_index == -1 || second_index == -1)
- return list;
- if (first_index == second_index)
- return list;
- return swap(list, first_index, second_index);
- }
- int main()
- {
- //create list
- Node *list = NULL;
- int a;
- int b;
- while (true)
- {
- cout << HELP_MSG;
- cin >> a;
- switch (a)
- {
- case 0:
- exit(0);
- break;
- case 1:
- cin >> b;
- list = push_back(list, b);
- break;
- case 2:
- cin >> b;
- list = push_front(list, b);
- break;
- case 3:
- list = pull_back(list);
- break;
- case 4:
- list = pull_front(list);
- break;
- case 5:
- print(list);
- break;
- case 6:
- list = swap35(list);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement