Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct DoubleListNumber
- {
- int valuekey;
- DoubleListNumber* prev;
- DoubleListNumber* next;
- } *start = NULL;
- struct DoubleListNumber2
- {
- int valuekey;
- DoubleListNumber2* prev;
- DoubleListNumber2* next;
- } *start2 = NULL;
- struct DoubleListNumber3
- {
- int valuekey;
- DoubleListNumber3* prev;
- DoubleListNumber3* next;
- } *start3 = NULL;
- void printAllNumbers(unsigned short choice)
- {
- DoubleListNumber* p = start;
- DoubleListNumber2* q = start2;
- DoubleListNumber3* r = start3;
- switch (choice)
- {
- case 1:
- while (p)
- {
- cout << p->valuekey << " ";
- p = p->next;
- }
- break;
- case 2:
- while (q)
- {
- cout << q->valuekey << " ";
- q = q->next;
- }
- break;
- case 3:
- while (r)
- {
- cout << r->valuekey << " ";
- r = r->next;
- }
- break;
- default:
- break;
- }
- cout << "" << endl;
- }
- int addingPosition(int n, unsigned short choice)
- {
- DoubleListNumber* p = start;
- DoubleListNumber2* q = start2;
- DoubleListNumber3* r = start3;
- int position = 0;
- switch (choice)
- {
- case 1:
- while (p)
- {
- if (n < p->valuekey)
- {
- break;
- }
- position++;
- p = p->next;
- }
- break;
- case 2:
- while (q)
- {
- if (n < q->valuekey)
- {
- break;
- }
- position++;
- q = q->next;
- }
- break;
- case 3:
- while (r)
- {
- if (n < r->valuekey)
- {
- break;
- }
- position++;
- r = r->next;
- }
- break;
- }
- return position;
- }
- void addNumber(int n, short choice, unsigned short& step, unsigned int& countOfNumbers)
- {
- DoubleListNumber* p = start;
- DoubleListNumber* q;
- int position = -1;
- if (countOfNumbers > 0)
- {
- position = addingPosition(n, 1);
- }
- if (choice == 1 && position > 0 && position < countOfNumbers || choice == 2 && position < countOfNumbers && position > 0)
- {
- choice = 3;
- step = position;
- }
- else if (choice == 1 && position == countOfNumbers || choice == 2 && position == 0)
- {
- choice = position == countOfNumbers ? 2 : 1;
- }
- else if (choice == 3 && position == 0 || choice == 3 && position == countOfNumbers)
- {
- choice = position == 0 ? 1 : 2;
- }
- switch (choice)
- {
- case 1:
- p = start;
- start = new DoubleListNumber;
- start->valuekey = n;
- start->prev = NULL;
- start->next = p;
- if (p)
- {
- p->prev = start;
- }
- break;
- case 2:
- p = start;
- q = new DoubleListNumber;
- q->valuekey = n;
- q->next = NULL;
- if (start)
- {
- while (p->next)
- {
- p = p->next;
- }
- p->next = q;
- q->prev = p;
- }
- else {
- start = q;
- }
- break;
- case 3:
- p = start;
- for (int i = countOfNumbers; i >= step; i--)
- {
- if (i == countOfNumbers) {
- while (p->next)
- {
- p = p->next;
- }
- q = new DoubleListNumber;
- q->valuekey = p->valuekey;
- q->next = NULL;
- q->prev = p;
- p->next = q;
- }
- else if (i == step) {
- p->valuekey = n;
- }
- else {
- p->valuekey = p->prev->valuekey;
- p = p->prev;
- }
- }
- break;
- default:
- break;
- }
- countOfNumbers++;
- }
- void addNumberToLIst2(int n, short choice, unsigned short& step, unsigned int& countOfNumbers)
- {
- DoubleListNumber2* p = start2;
- DoubleListNumber2* q;
- int position = -1;
- if (countOfNumbers > 0)
- {
- position = addingPosition(n, 2);
- }
- if (choice == 1 && position > 0 && position < countOfNumbers || choice == 2 && position < countOfNumbers && position > 0)
- {
- choice = 3;
- step = position;
- }
- else if (choice == 1 && position == countOfNumbers || choice == 2 && position == 0)
- {
- choice = position == countOfNumbers ? 2 : 1;
- }
- else if (choice == 3 && position == 0 || choice == 3 && position == countOfNumbers)
- {
- choice = position == 0 ? 1 : 2;
- }
- switch (choice)
- {
- case 1:
- p = start2;
- start2 = new DoubleListNumber2;
- start2->valuekey = n;
- start2->prev = NULL;
- start2->next = p;
- if (p)
- {
- p->prev = start2;
- }
- break;
- case 2:
- p = start2;
- q = new DoubleListNumber2;
- q->valuekey = n;
- q->next = NULL;
- if (start2)
- {
- while (p->next)
- {
- p = p->next;
- }
- p->next = q;
- q->prev = p;
- }
- else {
- start2 = q;
- }
- break;
- case 3:
- p = start2;
- for (int i = countOfNumbers; i >= step; i--)
- {
- if (i == countOfNumbers) {
- while (p->next)
- {
- p = p->next;
- }
- q = new DoubleListNumber2;
- q->valuekey = p->valuekey;
- q->next = NULL;
- q->prev = p;
- p->next = q;
- }
- else if (i == step) {
- p->valuekey = n;
- }
- else {
- p->valuekey = p->prev->valuekey;
- p = p->prev;
- }
- }
- break;
- default:
- break;
- }
- countOfNumbers++;
- }
- void addNumberToLIst3(int n, short choice, unsigned short& step, unsigned int& countOfNumbers)
- {
- DoubleListNumber3* p = start3;
- DoubleListNumber3* q;
- int position = -1;
- if (countOfNumbers > 0)
- {
- position = addingPosition(n, 3);
- }
- if (choice == 1 && position > 0 && position < countOfNumbers || choice == 2 && position < countOfNumbers && position > 0)
- {
- choice = 3;
- step = position;
- }
- else if (choice == 1 && position == countOfNumbers || choice == 2 && position == 0)
- {
- choice = position == countOfNumbers ? 2 : 1;
- }
- else if (choice == 3 && position == 0 || choice == 3 && position == countOfNumbers)
- {
- choice = position == 0 ? 1 : 2;
- }
- switch (choice)
- {
- case 1:
- p = start3;
- start3 = new DoubleListNumber3;
- start3->valuekey = n;
- start3->prev = NULL;
- start3->next = p;
- if (p)
- {
- p->prev = start3;
- }
- break;
- case 2:
- p = start3;
- q = new DoubleListNumber3;
- q->valuekey = n;
- q->next = NULL;
- if (start3)
- {
- while (p->next)
- {
- p = p->next;
- }
- p->next = q;
- q->prev = p;
- }
- else {
- start3 = q;
- }
- break;
- case 3:
- p = start3;
- for (int i = countOfNumbers; i >= step; i--)
- {
- if (i == countOfNumbers) {
- while (p->next)
- {
- p = p->next;
- }
- q = new DoubleListNumber3;
- q->valuekey = p->valuekey;
- q->next = NULL;
- q->prev = p;
- p->next = q;
- }
- else if (i == step) {
- p->valuekey = n;
- }
- else {
- p->valuekey = p->prev->valuekey;
- p = p->prev;
- }
- }
- break;
- }
- countOfNumbers++;
- }
- void removeNumbersList3(int n, unsigned int& countOfNumbers)
- {
- int position = addingPosition(n, 3);
- if (position == 0)
- {
- DoubleListNumber3* p = start3;
- DoubleListNumber3* q;
- start3 = p->next;
- delete p;
- p = start3;
- countOfNumbers--;
- }
- else if (position == countOfNumbers)
- {
- DoubleListNumber3* p = start3;
- DoubleListNumber3* q;
- while (p->next)
- {
- p = p->next;
- }
- q = p->prev;
- delete p;
- p = q;
- p->next = NULL;
- countOfNumbers--;
- }
- else
- {
- DoubleListNumber3* p = start3;
- for (int j = 1; j < position; j++)
- {
- p = p->next;
- }
- p->prev->next = p->next;
- p->next->prev = p->prev;
- delete p;
- countOfNumbers--;
- }
- }
- void removeNumbers(short choice, int count, int n, unsigned short step, unsigned int& countOfNumbers, unsigned int& countOfNumbers3)
- {
- if (n == 2 || (n == 1 && count == countOfNumbers))
- {
- DoubleListNumber* p = start;
- DoubleListNumber* q;
- switch (choice)
- {
- case 1:
- while (start)
- {
- start = p->next;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- p = start;
- }
- break;
- case 2:
- while (p->next)
- {
- p = p->next;
- }
- while (p)
- {
- q = p->prev;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- p = q;
- }
- break;
- default:
- break;
- }
- countOfNumbers = 0;
- }
- else if (n == 1 || choice == 3)
- {
- DoubleListNumber* p = start;
- DoubleListNumber* q;
- switch (choice)
- {
- case 1:
- for (int i = 0; i < count; i++)
- {
- DoubleListNumber* p = start;
- start = start->next;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- }
- start->prev = NULL;
- countOfNumbers -= count;
- break;
- case 2:
- while (p->next)
- {
- p = p->next;
- }
- for (int i = 0; i < count; i++)
- {
- q = p->prev;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- p = q;
- }
- p->next = NULL;
- countOfNumbers -= count;
- break;
- case 3:
- for (int j = 1; j < step; j++)
- {
- p = p->next;
- }
- p->prev->next = p->next;
- p->next->prev = p->prev;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- countOfNumbers--;
- break;
- default:
- break;
- }
- }
- }
- void removeNumbersList2(short choice, int count, int n, unsigned short step, unsigned int& countOfNumbers, unsigned int& countOfNumbers3)
- {
- if (n == 2 || (n == 1 && count == countOfNumbers))
- {
- DoubleListNumber2* p = start2;
- DoubleListNumber2* q;
- switch (choice)
- {
- case 1:
- while (start2)
- {
- start2 = p->next;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- p = start2;
- }
- break;
- case 2:
- while (p->next)
- {
- p = p->next;
- }
- while (p)
- {
- q = p->prev;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- p = q;
- }
- break;
- default:
- break;
- }
- countOfNumbers = 0;
- }
- else if (n == 1 || choice == 3)
- {
- DoubleListNumber2* p = start2;
- DoubleListNumber2* q;
- switch (choice)
- {
- case 1:
- for (int i = 0; i < count; i++)
- {
- DoubleListNumber2* p = start2;
- start2 = start2->next;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- }
- start2->prev = NULL;
- countOfNumbers -= count;
- break;
- case 2:
- while (p->next)
- {
- p = p->next;
- }
- for (int i = 0; i < count; i++)
- {
- q = p->prev;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- p = q;
- }
- p->next = NULL;
- countOfNumbers -= count;
- break;
- case 3:
- for (int j = 1; j < step; j++)
- {
- p = p->next;
- }
- p->prev->next = p->next;
- p->next->prev = p->prev;
- removeNumbersList3(p->valuekey, countOfNumbers3);
- delete p;
- countOfNumbers--;
- }
- }
- }
- int main()
- {
- short choice = -1;
- short choice2 = -1;
- unsigned int countOfNumbers = 0;
- unsigned int countOfNumbers2 = 0;
- unsigned int countOfNumbers3 = 0;
- int count = 0;
- int n = 0;
- unsigned short step = 0;
- unsigned short MAX_OPTION = countOfNumbers >= 2 ? 3 : 2;
- unsigned short MAX_OPTION2 = countOfNumbers2 >= 2 ? 3 : 2;
- unsigned int mediumMaxCount = choice == 3 ? countOfNumbers - 2 : countOfNumbers;
- unsigned int mediumMaxCount2 = choice == 3 ? countOfNumbers2 - 2 : countOfNumbers2;
- unsigned int mediumMaxCount3 = choice == 3 ? countOfNumbers3 - 2 : countOfNumbers3;
- int choice3 = -1;
- do
- {
- cout << "Choice option:\n[1] Add number\n[2] Remove number\n[3] Print all numbers\n[0] Exit\n";
- cin >> choice;
- switch (choice)
- {
- case 1:
- system("cls");
- do
- {
- cout << "Choose list\n[1] List 1\n[2] List 2\n[0] Exit\n";
- cin >> choice3;
- } while (choice3 < 0 || choice3 > 2);
- switch (choice3)
- {
- case 1:
- do
- {
- MAX_OPTION = countOfNumbers >= 2 ? 3 : 2;
- countOfNumbers >= 2 ?
- cout << "Choose option\n[1] in START\n[2] in END\n[3] in MEDIUM\n[0] Exit\n" :
- cout << "Choose option\n[1] in START\n[2] in END\n[0] Exit\n";
- cin >> choice2;
- if (choice2 > 0)
- {
- cout << "" << endl;
- do
- {
- cout << "Add count of numbers for adding: ";
- cin >> count;
- } while (count < 1);
- cout << "" << endl;
- for (int i = 0; i < count; i++)
- {
- cout << "Enter number: ";
- cin >> n;
- if (choice2 == 3)
- {
- do
- {
- cout << "Enter step inside" << "[1-" << countOfNumbers - 1 << "]: ";
- cin >> step;
- } while (step < 1 || step > countOfNumbers - 1);
- }
- addNumber(n, choice2, step, countOfNumbers);
- addNumberToLIst3(n, choice2, step, countOfNumbers3);
- }
- cout << "Number/s added\n";
- cout << "" << endl;
- }
- } while (choice2 < 0 || choice2 > MAX_OPTION);
- if (choice2 == 0)
- {
- cout << "You choose exit\n";
- }
- break;
- case 2:
- do
- {
- MAX_OPTION2 = countOfNumbers2 >= 2 ? 3 : 2;
- countOfNumbers2 >= 2 ?
- cout << "Choose option\n[1] in START\n[2] in END\n[3] in MEDIUM\n[0] Exit\n" :
- cout << "Choose option\n[1] in START\n[2] in END\n[0] Exit\n";
- cin >> choice2;
- if (choice2 > 0)
- {
- cout << "" << endl;
- do
- {
- cout << "Add count of numbers for adding: ";
- cin >> count;
- } while (count < 1);
- cout << "" << endl;
- for (int i = 0; i < count; i++)
- {
- cout << "Enter number: ";
- cin >> n;
- if (choice2 == 3)
- {
- do
- {
- cout << "Enter step inside" << "[1-" << countOfNumbers2 - 1 << "]: ";
- cin >> step;
- } while (step < 1 || step > countOfNumbers2 - 1);
- }
- addNumberToLIst2(n, choice2, step, countOfNumbers2);
- addNumberToLIst3(n, choice2, step, countOfNumbers3);
- }
- }
- } while (choice2 < 0 || choice2 > MAX_OPTION2);
- if (choice2 == 0)
- {
- cout << "You choose exit\n";
- }
- break;
- default:
- break;
- }
- choice = -1;
- choice2 = -1;
- choice3 = -1;
- count = 0;
- n = 0;
- step = 0;
- break;
- case 2:
- system("cls");
- if (start || start2)
- {
- do
- {
- cout << "Choose list\n[1] List 1\n[2] List 2\n[0] Exit\n";
- cin >> choice3;
- if (choice3 == 1 && !start) {
- cout << "List 1 is empty\n";
- cout << "" << endl;
- choice3 = -1;
- }
- else if (choice3 == 2 && !start2)
- {
- cout << "List 2 is empty\n";
- cout << "" << endl;
- choice3 = -1;
- }
- } while (choice3 < 0 || choice3 > 2);
- switch (choice3)
- {
- case 1:
- do
- {
- MAX_OPTION = countOfNumbers >= 2 ? 3 : 2;
- countOfNumbers > 2 ?
- cout << "Choose option\n[1] from START\n[2] from END\n[3] from MEDIUM\n[0] Exit\n" :
- cout << "Choose option\n[1] from START\n[2] from END\n[0] Exit\n";
- cin >> choice2;
- if (choice2 > 0)
- {
- if (choice2 < 3) {
- do
- {
- cout << "Choose option\n[1] Current count\n[2] Remove All\n";
- cin >> n;
- } while (n < 0 || n > 2);
- }
- if (n == 1 || choice2 == 3)
- {
- mediumMaxCount = choice2 == 3 ? countOfNumbers - 2 : countOfNumbers;
- do
- {
- cout << "Enter count of numbers for removing" << "[1-" << mediumMaxCount << "]: ";
- cin >> count;
- } while (count < 1 || count > mediumMaxCount);
- }
- if (choice3 == 1)
- {
- removeNumbers(choice2, count, n, step, countOfNumbers, countOfNumbers3);
- }
- else
- {
- removeNumbersList2(choice2, count, n, step, countOfNumbers2, countOfNumbers3);
- }
- }
- } while (choice2 < 0 || choice2 > MAX_OPTION);
- if (choice2 == 0 || choice3 == 0)
- {
- cout << "You choose exit\n";
- }
- break;
- case 2:
- do
- {
- MAX_OPTION2 = countOfNumbers2 >= 2 ? 3 : 2;
- countOfNumbers2 > 2 ?
- cout << "Choose option\n[1] from START\n[2] from END\n[3] from MEDIUM\n[0] Exit\n" :
- cout << "Choose option\n[1] from START\n[2] from END\n[0] Exit\n";
- cin >> choice2;
- if (choice2 > 0)
- {
- if (choice2 < 3) {
- do
- {
- cout << "Choose option\n[1] Current count\n[2] Remove All\n";
- cin >> n;
- } while (n < 0 || n > 2);
- }
- if (n == 1 || choice2 == 3)
- {
- mediumMaxCount2 = choice2 == 3 ? countOfNumbers2 - 2 : countOfNumbers2;
- do
- {
- cout << "Enter count of numbers for removing" << "[1-" << mediumMaxCount2 << "]: ";
- cin >> count;
- } while (count < 1 || count > mediumMaxCount2);
- }
- if (choice3 == 1)
- {
- removeNumbers(choice2, count, n, step, countOfNumbers, countOfNumbers3);
- }
- else
- {
- removeNumbersList2(choice2, count, n, step, countOfNumbers2, countOfNumbers3);
- }
- }
- } while (choice2 < 0 || choice2 > MAX_OPTION2);
- if (choice2 == 0)
- {
- cout << "You choose exit\n";
- }
- break;
- default:
- break;
- }
- }
- else
- {
- cout << "List is empty\n";
- }
- choice = -1;
- choice2 = -1;
- choice3 = -1;
- count = 0;
- n = 0;
- step = 0;
- break;
- case 3:
- system("cls");
- if (start) {
- cout << "List 1: ";
- printAllNumbers(1);
- cout << "" << endl;
- }
- else {
- cout << "List is empty\n";
- cout << "" << endl;
- }
- if (start2) {
- cout << "List 2: ";
- printAllNumbers(2);
- cout << "" << endl;
- }
- else {
- cout << "List is empty\n";
- cout << "" << endl;
- }
- if (start3) {
- cout << "List 3: ";
- printAllNumbers(3);
- cout << "" << endl;
- }
- else {
- cout << "List is empty\n";
- cout << "" << endl;
- }
- choice = -1;
- break;
- default:
- break;
- }
- } while (choice < 0 || choice > 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement