Advertisement
Guest User

???v3

a guest
Mar 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. // Удалить из списка все элементы, находящиеся между его максимальным и минимальным значениями.
  2.  
  3. #include "pch.h"
  4. #include <iostream>
  5. #include <ctime>
  6. #include <iomanip>
  7.  
  8. #define U 1
  9. #define NULL 0
  10.  
  11. using namespace std;
  12.  
  13. class biTiedStack {
  14. public: int data; biTiedStack *right; biTiedStack *left;
  15.         /*constructor*/  biTiedStack(int x) { this->data = x; this->right = NULL; this->left = NULL; }
  16.         void changeStackData(int y) { this->data = y; }
  17.         void showStackData() { cout << this->data << setw(10); }
  18.         void tieLeft(biTiedStack *leftStack) { this->left = leftStack; }
  19.         void tieRight(biTiedStack *rightStack) { this->right = rightStack; }
  20. };
  21.  
  22. void addStackLeftward(biTiedStack *);
  23. void addStackRightward(biTiedStack *);
  24. void showEntireStackFromLeftSide(biTiedStack *, char);
  25. void showEntireStackFromRightSide(biTiedStack *, char);
  26. biTiedStack *findMaxAndMinValueAdress(biTiedStack *);            // Begin from left side
  27. void deleteElementsBetweenMaxAndMin(biTiedStack *);             // Begin from left side
  28.  
  29. int main()
  30. {
  31.     int n; cin >> n;
  32.     srand(time(0));
  33.     biTiedStack *topLeft = new biTiedStack(-50 + rand() % 101);
  34.     biTiedStack *topRight = new biTiedStack(-50 + rand() % 101);
  35.     topLeft->tieRight(topRight); topLeft->tieLeft(NULL); topRight->tieLeft(topLeft); topRight->tieRight(NULL);
  36.     for (int i(NULL); i < n; i++) {
  37.         addStackLeftward(topLeft);
  38.     } showEntireStackFromLeftSide(topLeft, 'A'); cout << "\n\n"; showEntireStackFromRightSide(topRight, 'B'); cout << "\n\n";
  39.     //biTiedStack *maxminValueAdress = findMaxAndMinValueAdress(topLeft);
  40.     //maxminValueAdress->right->showStackData(); cout << "\t"; maxminValueAdress->left->showStackData(); cout << "\t"; maxminValueAdress->showStackData(); cout << "\n\n";
  41.     deleteElementsBetweenMaxAndMin(topLeft);
  42.     showEntireStackFromLeftSide(topLeft, 'C');
  43. }
  44.  
  45. // functions
  46.  
  47. void addStackLeftward(biTiedStack *bts) {
  48.     biTiedStack *newStack = new biTiedStack(-50 + rand() % 101);
  49.     newStack->tieRight(bts->right);
  50.     newStack->tieLeft(bts);
  51.     bts->right->tieLeft(newStack);
  52.     bts->tieRight(newStack);
  53. }
  54.  
  55. void addStackRightward(biTiedStack *bts) {
  56.     biTiedStack *newStack = new biTiedStack(-50 + rand() % 101);
  57.     newStack->tieLeft(bts->left);
  58.     newStack->tieRight(bts);
  59.     bts->left->tieRight(newStack);
  60.     bts->tieLeft(newStack);
  61. }
  62.  
  63. void showEntireStackFromLeftSide(biTiedStack *leftStack, char name) {
  64.     biTiedStack *checkStack = leftStack; int numerator(NULL);
  65.     while (checkStack != NULL) {
  66.         checkStack->showStackData();
  67.         checkStack = checkStack->right;
  68.         numerator++;
  69.     } cout << "\n";
  70.     while (numerator > 0) {
  71.         cout << name << numerator << setw(9);
  72.         numerator--;
  73.     }
  74. }
  75.  
  76. void showEntireStackFromRightSide(biTiedStack *rightStack, char name) {
  77.     biTiedStack *checkStack = rightStack; int numerator(NULL);
  78.     while (checkStack != NULL) {
  79.         checkStack->showStackData();
  80.         checkStack = checkStack->left;
  81.         numerator++;
  82.     } cout << "\n";
  83.     while (numerator > 0) {
  84.         cout << name << numerator << setw(9);
  85.         numerator--;
  86.     }
  87. }
  88.  
  89. biTiedStack *findMaxAndMinValueAdress(biTiedStack *bts) {              // Begin from left side (right - max / left - min) ToRight: 0 - mintomax  /  1 - maxtomin
  90.     biTiedStack *maxminValueAdress = new biTiedStack(bts->data); maxminValueAdress->tieLeft(NULL); maxminValueAdress->tieRight(NULL); int temporaryvalue = 0;
  91.     while (bts->right != NULL) {
  92.         if (bts->data > maxminValueAdress->data) { maxminValueAdress->tieRight(bts); maxminValueAdress->changeStackData(bts->data); }
  93.         bts = bts->right;
  94.     }       // finding max adress
  95.     while (bts->left != NULL) {
  96.         if (bts->data < maxminValueAdress->data) { maxminValueAdress->tieLeft(bts); maxminValueAdress->changeStackData(bts->data); }
  97.         bts = bts->left;
  98.     }       // finding min adress
  99.     bts = maxminValueAdress->right; while (bts->right != NULL) {
  100.         if (bts->right == maxminValueAdress->left) { temporaryvalue = 1; break; }
  101.         bts = bts->right;
  102.     } maxminValueAdress->changeStackData(temporaryvalue);
  103.     return maxminValueAdress;
  104. }
  105.  
  106. void deleteElementsBetweenMaxAndMin(biTiedStack *bts) {           // begin from left side
  107.     biTiedStack *maxminValueAdress = findMaxAndMinValueAdress(bts);
  108.     biTiedStack *deleteElement = bts;
  109.     if (maxminValueAdress->data == 1) {
  110.         bts = maxminValueAdress->right->right;
  111.         while (bts->right != maxminValueAdress->left) {
  112.             deleteElement = bts;
  113.             bts = bts->right;
  114.             delete deleteElement;
  115.         }
  116.         maxminValueAdress->right->tieRight(maxminValueAdress->left); maxminValueAdress->left->tieLeft(maxminValueAdress->right);
  117.     }
  118.     else if (maxminValueAdress->data == 0) {
  119.         bts = maxminValueAdress->left->right;
  120.         while (bts->right != maxminValueAdress->right) {
  121.             deleteElement = bts;
  122.             bts = bts->right;
  123.             delete deleteElement;
  124.         }
  125.         maxminValueAdress->left->tieRight(maxminValueAdress->right); maxminValueAdress->right->tieLeft(maxminValueAdress->left);
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement