Advertisement
Guest User

ChezaHuinia

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