Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Удалить из списка все элементы, находящиеся между его максимальным и минимальным значениями.
- #include "pch.h"
- #include <iostream>
- #include <ctime>
- #include <iomanip>
- #define U 1
- #define NULL 0
- using namespace std;
- class biTiedStack {
- public: int data; biTiedStack *right; biTiedStack *left;
- /*constructor*/ biTiedStack(int x) { this->data = x; this->right = NULL; this->left = NULL; }
- void changeStackData(int y) { this->data = y; }
- void showStackData() { cout << this->data << setw(10); }
- void tieLeft(biTiedStack *leftStack) { this->left = leftStack; }
- void tieRight(biTiedStack *rightStack) { this->right = rightStack; }
- };
- void addStackLeftward(biTiedStack *);
- void addStackRightward(biTiedStack *);
- void showEntireStackFromLeftSide(biTiedStack *, char);
- void showEntireStackFromRightSide(biTiedStack *, char);
- biTiedStack *findMaxAndMinValueAdress(biTiedStack *); // Begin from left side
- void deleteElementsBetweenMaxAndMin(biTiedStack *); // Begin from left side
- int main()
- {
- int n; cin >> n;
- srand(time(0));
- biTiedStack *topLeft = new biTiedStack(-50 + rand() % 101);
- biTiedStack *topRight = new biTiedStack(-50 + rand() % 101);
- topLeft->tieRight(topRight); topLeft->tieLeft(NULL); topRight->tieLeft(topLeft); topRight->tieRight(NULL);
- for (int i(NULL); i < n; i++) {
- addStackLeftward(topLeft);
- } showEntireStackFromLeftSide(topLeft, 'A'); cout << "\n\n"; showEntireStackFromRightSide(topRight, 'B'); cout << "\n\n";
- //biTiedStack *maxminValueAdress = findMaxAndMinValueAdress(topLeft);
- //maxminValueAdress->right->showStackData(); cout << "\t"; maxminValueAdress->left->showStackData(); cout << "\t"; maxminValueAdress->showStackData(); cout << "\n\n";
- deleteElementsBetweenMaxAndMin(topLeft);
- showEntireStackFromLeftSide(topLeft, 'C');
- }
- // functions
- void addStackLeftward(biTiedStack *bts) {
- biTiedStack *newStack = new biTiedStack(-50 + rand() % 101);
- newStack->tieRight(bts->right);
- newStack->tieLeft(bts);
- bts->right->tieLeft(newStack);
- bts->tieRight(newStack);
- }
- void addStackRightward(biTiedStack *bts) {
- biTiedStack *newStack = new biTiedStack(-50 + rand() % 101);
- newStack->tieLeft(bts->left);
- newStack->tieRight(bts);
- bts->left->tieRight(newStack);
- bts->tieLeft(newStack);
- }
- void showEntireStackFromLeftSide(biTiedStack *leftStack, char name) {
- biTiedStack *checkStack = leftStack; int numerator(NULL);
- while (checkStack != NULL) {
- checkStack->showStackData();
- checkStack = checkStack->right;
- numerator++;
- } cout << "\n";
- while (numerator > 0) {
- cout << name << numerator << setw(9);
- numerator--;
- }
- }
- void showEntireStackFromRightSide(biTiedStack *rightStack, char name) {
- biTiedStack *checkStack = rightStack; int numerator(NULL);
- while (checkStack != NULL) {
- checkStack->showStackData();
- checkStack = checkStack->left;
- numerator++;
- } cout << "\n";
- while (numerator > 0) {
- cout << name << numerator << setw(9);
- numerator--;
- }
- }
- biTiedStack *findMaxAndMinValueAdress(biTiedStack *bts) { // Begin from left side (right - max / left - min) ToRight: 0 - mintomax / 1 - maxtomin
- biTiedStack *maxminValueAdress = new biTiedStack(bts->data); maxminValueAdress->tieLeft(NULL); maxminValueAdress->tieRight(NULL); int temporaryvalue = 0;
- while (bts->right != NULL) {
- if (bts->data > maxminValueAdress->data) { maxminValueAdress->tieRight(bts); maxminValueAdress->changeStackData(bts->data); }
- bts = bts->right;
- } // finding max adress
- while (bts->left != NULL) {
- if (bts->data < maxminValueAdress->data) { maxminValueAdress->tieLeft(bts); maxminValueAdress->changeStackData(bts->data); }
- bts = bts->left;
- } // finding min adress
- bts = maxminValueAdress->right; while (bts->right != NULL) {
- if (bts->right == maxminValueAdress->left) { temporaryvalue = 1; break; }
- bts = bts->right;
- } maxminValueAdress->changeStackData(temporaryvalue);
- return maxminValueAdress;
- }
- void deleteElementsBetweenMaxAndMin(biTiedStack *bts) { // begin from left side
- biTiedStack *maxminValueAdress = findMaxAndMinValueAdress(bts);
- biTiedStack *deleteElement = bts;
- if (maxminValueAdress->data == 1) {
- bts = maxminValueAdress->right->right;
- while (bts->right != maxminValueAdress->left) {
- deleteElement = bts;
- bts = bts->right;
- delete deleteElement;
- }
- maxminValueAdress->right->tieRight(maxminValueAdress->left); maxminValueAdress->left->tieLeft(maxminValueAdress->right);
- }
- else if (maxminValueAdress->data == 0) {
- bts = maxminValueAdress->left->right;
- while (bts->right != maxminValueAdress->right) {
- deleteElement = bts;
- bts = bts->right;
- delete deleteElement;
- }
- maxminValueAdress->left->tieRight(maxminValueAdress->right); maxminValueAdress->right->tieLeft(maxminValueAdress->left);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement