Advertisement
ElenaR1

stack

Dec 11th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.20 KB | None | 0 0
  1. //с вграден стек:
  2. #include <iostream>
  3. #include<iomanip>
  4. #include<string.h>
  5. #include<string>
  6. #include <vector>
  7. #include <stack>
  8. #include <algorithm>
  9. #include <assert.h>
  10. using namespace std;
  11.  
  12. template<typename T>
  13. const T& getMax(stack<T> s)
  14. {
  15.     stack<int> s2;
  16.     int max = s.top();
  17.     while (!s.empty())
  18.     {
  19.         if (s.top() > max)
  20.         {
  21.             max = s.top();
  22.         }
  23.         else
  24.         {
  25.             s2.push(s.top());
  26.             s.pop();
  27.         }
  28.     }
  29.     while (!s2.empty())
  30.     {
  31.         s.push(s2.top());
  32.         s2.pop();
  33.     }
  34.     return max;
  35. }
  36. bool bracket(string a)
  37. {
  38.     stack<char>s;
  39.     for (size_t i = 0; i < a.size(); i++)
  40.     {
  41.         if (a[i] == '(')
  42.         {
  43.             s.push(a[i]);
  44.         }
  45.         if (a[i] == ')')
  46.         {
  47.             if (s.empty())
  48.             {
  49.                 return false;
  50.             }
  51.             else
  52.             s.pop();
  53.         }
  54.     }
  55.     if (s.empty()) return true;
  56.     else
  57.         return false;
  58. }
  59. void reverse1(char a[])
  60. {
  61.     stack<char> s;
  62.     int len = strlen(a);
  63.     for (size_t i = 0; i < len; i++)
  64.     {
  65.         s.push(a[i]);
  66.     }
  67.     for (size_t i = 0; i < len; i++)
  68.     {
  69.         a[i] = s.top();
  70.         s.pop();
  71.     }
  72. }
  73. void evaluatePostfix(string a)
  74. {
  75.     int n;
  76.     int result;
  77.     stack<int> newStack;
  78.     //int len = strlen(a);
  79.     for (size_t i = 0; i < a.size(); i++)// "63+2*"=18
  80.     {
  81.         if (isdigit(a[i]))
  82.         {
  83.             char c = a[i];
  84.             n = c - '0';
  85.             newStack.push(n);//6 vliza posle 3 vliza
  86.  
  87.         }
  88.         if (a[i] == '+')
  89.         {
  90.             int x = newStack.top();//x=3
  91.             newStack.pop();
  92.             int y = newStack.top();//y=6
  93.             newStack.pop();
  94.             result = x + y;//8
  95.             newStack.push(result);//vkarvame v steka 9
  96.         }
  97.         if (a[i] == '-')
  98.         {
  99.             int x = newStack.top();
  100.             newStack.pop();
  101.             int y = newStack.top();
  102.             newStack.pop();
  103.             result = y - x;//8
  104.             newStack.push(result);
  105.  
  106.         }
  107.         if (a[i] == '*')
  108.         {
  109.             int x = newStack.top();
  110.             newStack.pop();
  111.             int y = newStack.top();
  112.             newStack.pop();
  113.             result = x*y;//8
  114.             newStack.push(result);
  115.  
  116.         }
  117.         if (a[i] == '/')
  118.         {
  119.             int x = newStack.top();
  120.             newStack.pop();
  121.             int y = newStack.top();
  122.             newStack.pop();
  123.             result = y / x;//8
  124.             newStack.push(result);
  125.         }
  126.     }
  127.     cout << "result of expression is  " << newStack.top();
  128. }
  129.  
  130. //greshno
  131. /*void evaluatePrefix(string a)
  132. {
  133.     reverse(a.begin(),a.end());
  134.     evaluatePostfix(a);
  135. }*/
  136. void evaluatePrefix2(char a[])
  137. {
  138.     //reverse(a);
  139.     //evaluatePrefix(a);
  140.     stack<int> s;
  141.     int n;
  142.     int result;
  143.     int len = strlen(a);
  144.     reverse1(a);
  145.     for (int i =0; i <len; i++)
  146.     {
  147.         if (isdigit(a[i]))
  148.         {
  149.             char c = a[i];
  150.             n = c - '0';
  151.             s.push(n);
  152.         }
  153.         if (a[i] == '+')
  154.         {
  155.             int x = s.top();
  156.             s.pop();
  157.             int y = s.top();
  158.             s.pop();
  159.             result = x + y;
  160.             s.push(result);
  161.         }
  162.         if (a[i] == '-')
  163.         {
  164.             int x = s.top();
  165.             s.pop();
  166.             int y = s.top();
  167.             s.pop();
  168.             result = x - y;
  169.             s.push(result);
  170.         }
  171.         if (a[i] == '*')
  172.         {
  173.             int x = s.top();
  174.             s.pop();
  175.             int y = s.top();
  176.             s.pop();
  177.             result = y * x;
  178.             s.push(result);
  179.         }
  180.         if (a[i] == '/')
  181.         {
  182.             int x = s.top();
  183.             s.pop();
  184.             int y = s.top();
  185.             s.pop();
  186.             result = x / y;
  187.             s.push(result);
  188.         }
  189.     }
  190.     cout << "result: " << s.top() << endl;
  191. }
  192.  
  193.  
  194. /*char* f()
  195. {
  196. char*a = "few";
  197. return a;
  198. }
  199. string str()
  200. {
  201. string a = "few";
  202. return a;
  203. }
  204. char* ff()
  205. {
  206. char a[] = "few";
  207. return a;
  208. }
  209. char* fff()
  210. {
  211.  
  212. char*a = new char[4];
  213. strcpy_s(a, 4, "few");
  214. return a;
  215. }*/
  216. int main()
  217. {
  218.     stack<int> s;
  219.     s.push(5);
  220.     s.push(2);
  221.     s.push(17);
  222.     s.push(0);
  223.     s.push(11);
  224.     cout << getMax<int>(s) << endl;
  225.     string a = "((()))";
  226.     cout << bracket(a) << endl;
  227.     char str[] = "Hello";
  228.     reverse1(str);
  229.     cout << "after reversing the string: " << str << endl;
  230.     cout << strlen(str) << endl;
  231.     string expression = "63+2*";
  232.     string ex = "5234+*-";
  233.     evaluatePostfix(expression);//po razlichen nachin se presmqta ot prefix expression-a
  234.     cout << endl;
  235.     evaluatePostfix(ex);
  236.     cout << endl;
  237.     //string exp = "-*+4325"; GRESHNO
  238.     //evaluatePrefix(exp);//9
  239.     cout << endl;
  240.     char sameexp[] = "-*+4325";
  241.     evaluatePrefix2(sameexp);//VQRNO
  242.     cout << endl;
  243.  
  244.  
  245.     /*cout << f << endl << ff << endl ;
  246.     cout << str();
  247.     char*c = fff();
  248.     cout<< *c;*/
  249.     return 0;
  250. }
  251.  
  252.  
  253.  
  254. STACK
  255. #include <iostream>
  256. #include <string.h>
  257. #include <string>
  258.  
  259. using namespace std;
  260. template<class T>
  261. struct node {
  262.     node<T>* next;
  263.     T data;
  264.     node() :next(NULL) {}
  265.     node(const T& d,node<T>* n):data(d),next(n){}
  266. };
  267.  
  268. template<class T>
  269. class stack
  270. {
  271. private:
  272. node<T>* top;  
  273. public:
  274.     stack()
  275.     {
  276.         top = NULL;
  277.     }
  278.     void push(const T& newData)
  279.     {
  280.         node<T>* n = new node<T>(newData, top);//top purvonachalno e NULL zaradi default-niq kontruktor
  281. //i vsqko sledvashto dobaveno trqbva da stava top-a t.k e nai-otgore na steka
  282.         //n->data = newData;
  283.         //n->next = head;
  284.         top = n;
  285.     }
  286.     void pop()
  287.     {
  288.         if (top == NULL)
  289.         {
  290.             cout << "the stack is empty" << endl;
  291.         }
  292.         else
  293.         {
  294.             node<T>* temp = top;
  295.             top = top->next;
  296.             cout << "Deleting the first node with value " << temp->data << endl;
  297.             delete temp;
  298.         }
  299.     }
  300.     void print()
  301.     {
  302.         node<T>*curr;
  303.         curr = top;
  304.         while (curr != NULL)
  305.         {
  306.             cout << curr->data << " ";
  307.             curr = curr->next;
  308.         }
  309.     }
  310.      T& peek()
  311.     {
  312.         return top->data;
  313.     }
  314.     bool isEmpty()
  315.     {
  316.         if (top == NULL)
  317.         {
  318.             return true;
  319.         }
  320.         return false;
  321.     }
  322.     const T& getMax()
  323.     {
  324.         stack<int>stack2;
  325.         int max = peek();
  326.         while (!this->isEmpty())
  327.         {
  328.             if (this->peek() > max)
  329.             {
  330.                 max = this->peek();
  331.             }
  332.             else
  333.             {
  334.                 stack2.push(this->peek());
  335.                 this->pop();
  336.             }
  337.         }
  338.         while (!stack2.isEmpty())
  339.         {
  340.             push(stack2.peek());
  341.             stack2.pop();
  342.         }
  343.         return max;
  344.     }
  345.  
  346. };
  347. /*
  348. bool arePair(char opening, char closing)
  349. {
  350.     if (opening == '(' && closing == ')') return true;
  351.     else if (opening == '{' && closing == '}') return true;
  352.     else if (opening == '[' && closing == ']') return true;
  353.     return false;
  354. }
  355. bool bracket(string a)
  356. {
  357.     stack<char> s;
  358.     for (size_t i = 0; i < a.size(); i++)
  359.     {
  360.         if (a[i] == '('||a[i]=='{' || a[i] == '[')
  361.         {
  362.             s.push(a[i]);
  363.         }
  364.         if (a[i] == ')'|| a[i] == '}' || a[i] == ']')
  365.         {
  366.             if (s.empty()||!arePair(s.top(),a[i]))
  367.                 return false;
  368.             s.pop();
  369.         }
  370.  
  371.     }
  372.     if (s.empty())
  373.         return true;
  374.     else
  375.         return false;
  376. //string c = "({[]})";
  377.     //string d = "({[)})";
  378.     //cout << bracket(c) << endl;
  379.     //cout << bracket(d) << endl;
  380. }*/
  381. bool bracket(string a)//ako a e string a = "((()))";
  382. {
  383.     stack<char> s;
  384.     for (size_t i = 0; i < a.length(); i++)
  385.     {
  386.         if (a[i] == '(')
  387.         {
  388.             s.push(a[i]);//vseki put kato sreshtenm otvarqshta dobavqme
  389.         }
  390.         else if (a[i] == ')')//vseki put kato sreshtenm zatvarqshta mahame
  391.         {
  392.             if (s.isEmpty())
  393.             {
  394.                 return false;
  395.             }
  396.             else
  397.             {
  398.                 s.pop();
  399.             }
  400.         }
  401.     }
  402.     if (s.isEmpty())//ako e pravilen izrazut broqt na otvarqshtite i zatvarqshtitie skobi e raven-> shte se poluchi che
  403.         //stekut e prazen
  404.     {
  405.         return true;
  406.     }
  407.     else
  408.     {
  409.         return false;
  410.     }
  411. }
  412.  
  413.  
  414. void reverse(char a[])// a="Hello/0"
  415. {
  416.     stack<char> s;
  417.     int len = strlen(a);//ako e +1 ne stava
  418.     for (size_t i = 0; i < len; i++)
  419.     {
  420.         s.push(a[i]);
  421.     }
  422.     //loop for pop
  423.     for (size_t i = 0; i <len; i++)
  424.     {
  425.         a[i] = s.peek();//a[0]='/0' 1. a[1]='o' 2.a[2]='l' 3.a[3]='l' 4.a[4]='e' 5.a[5]='H' mai 1viqt element e 'o' a ne '/0'
  426.         s.pop();//i sega mahame /0 1. sega mahame 'o' 2. sega mahame 'l'...
  427.     }
  428.  
  429. }
  430. bool isNumber(char&a)
  431. {
  432.     if (!isdigit(a))
  433.     {
  434.         return false;
  435.     }
  436.     else
  437.         return true;
  438. }
  439.  
  440.  
  441. void evaluatePostfix(char*a)
  442. {
  443.     int n;
  444.     int result;
  445.     stack<int> newStack;
  446.     int len = strlen(a);
  447.     for (size_t i = 0; i < len; i++)// "63+2*"=18
  448.     {
  449.         if (isNumber(a[i]))
  450.         {
  451.             char c = a[i];
  452.             n = c - '0';
  453.             newStack.push(n);//6 vliza posle 3 vliza
  454.            
  455.         }
  456.         if (a[i] == '+')
  457.         {
  458.             int x = newStack.peek();//x=3
  459.             newStack.pop();
  460.             int y = newStack.peek();//y=6
  461.             newStack.pop();
  462.             result = x + y;//8
  463.             newStack.push(result);//vkarvame v steka 9
  464.         }
  465.         if (a[i] == '-')
  466.         {
  467.             int x = newStack.peek();
  468.             newStack.pop();
  469.             int y = newStack.peek();
  470.             newStack.pop();
  471.             result = y - x;//8
  472.             newStack.push(result);
  473.            
  474.         }
  475.         if (a[i] == '*')
  476.         {
  477.             int x = newStack.peek();
  478.             newStack.pop();
  479.             int y = newStack.peek();
  480.             newStack.pop();
  481.             result = x*y;//8
  482.             newStack.push(result);
  483.            
  484.         }
  485.         if (a[i] == '/')
  486.         {
  487.             int x = newStack.peek();
  488.             newStack.pop();
  489.             int y = newStack.peek();
  490.             newStack.pop();
  491.             result = y / x;//8
  492.             newStack.push(result);
  493.         }
  494.     }
  495.     cout << "result of expression is  " << newStack.peek();
  496. }
  497. void evaluatePrefix(char a[])
  498. {
  499.     //reverse(a);
  500.     //evaluatePrefix(a);
  501.     stack<int> s;
  502.     int n;
  503.     int result;
  504.     int len = strlen(a);
  505.     for (int i = len-1; i>=0; i--)
  506.     {
  507.         if (isNumber(a[i]))
  508.         {
  509.             char c = a[i];
  510.             n = c - '0';
  511.             s.push(n);
  512.         }
  513.         if (a[i] == '+')
  514.         {
  515.             int x = s.peek();
  516.             s.pop();
  517.             int y = s.peek();
  518.             s.pop();
  519.             result = x + y;
  520.             s.push(result);
  521.         }
  522.         if (a[i] == '-')
  523.         {
  524.             int x = s.peek();
  525.             s.pop();
  526.             int y = s.peek();
  527.             s.pop();
  528.             result = x - y;
  529.             s.push(result);
  530.         }
  531.         if (a[i] == '*')
  532.         {
  533.             int x = s.peek();
  534.             s.pop();
  535.             int y = s.peek();
  536.             s.pop();
  537.             result = y * x;
  538.             s.push(result);
  539.         }
  540.         if (a[i] == '/')
  541.         {
  542.             int x = s.peek();
  543.             s.pop();
  544.             int y = s.peek();
  545.             s.pop();
  546.             result = x / y;
  547.             s.push(result);
  548.         }
  549.     }
  550.     cout << "result: " << s.peek() << endl;
  551. }
  552.  
  553.  
  554.  
  555. int main()
  556. {
  557.     stack<int> s;
  558.     s.push(5);
  559.     s.push(4);
  560.     s.push(9);
  561.     s.push(13);
  562.     s.push(7);
  563.     s.push(10);
  564.     s.print();
  565.     cout << endl;
  566.     s.pop();
  567.     s.print();
  568.     cout << endl;
  569.     cout << s.peek();
  570.     cout << endl;
  571.  
  572.     string a = "((()))";
  573.     cout << bracket(a) << endl;
  574.  
  575.    
  576.     char expression[] = "63+2*";
  577.     evaluatePostfix(expression);
  578.     char exp[] = "-*+4325";
  579.     evaluatePrefix(exp);//9
  580.  
  581.     char str[] = "Hello";
  582.     reverse(str);
  583.     cout << "after reversing the string: " << str << endl;//// ? zashto ne stava
  584.  
  585.  
  586.     cout << endl;
  587.     cout<<"the max data in the stack is: " << s.getMax();
  588.  
  589.     return 0;
  590. }
  591.  
  592.  
  593. #include <iostream>
  594. #include <string.h>
  595. #include <string>
  596. #include <stack>
  597.  
  598. using namespace std;
  599.  
  600. bool IsOperand(char ch)
  601. {
  602.     if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
  603.         return true;
  604.     }
  605.     return false;
  606. }
  607.  
  608. bool IsOperator(char C)
  609. {
  610.     if (C == '+' || C == '-' || C == '*' || C == '/' || C == '^') {
  611.         return true;
  612.     }
  613.     return false;
  614. }
  615. bool IsLeftParenthesis(char ch)
  616. {
  617.     if (ch == '(') {
  618.         return true;
  619.     }
  620.     return false;
  621. }
  622.  
  623. bool IsRightParenthesis(char ch)
  624. {
  625.     if (ch == ')') {
  626.         return true;
  627.     }
  628.     return false;
  629. }
  630.  
  631. bool Flag(char ch)
  632. {
  633.     if (!IsOperand(ch) || !IsOperator(ch) || !IsLeftParenthesis(ch) || !IsRightParenthesis(ch)) {
  634.         return false;
  635.     }
  636.     return true;
  637. }
  638.  
  639. int IsRightAssociative(char op)
  640. {
  641.     if (op == '^') {
  642.         return true;
  643.     }
  644.     return false;
  645. }
  646.  
  647. int GetOperatorWeight(char op)
  648. {
  649.     int weight = -1;
  650.     switch (op) {
  651.     case '+':
  652.     case '-':
  653.         weight = 1;
  654.         break;
  655.     case '*':
  656.     case '/':
  657.         weight = 2;
  658.         break;
  659.     case '^':
  660.         weight = 3;
  661.         break;
  662.     }
  663.     return weight;
  664. }
  665.  
  666.  
  667. bool HasHigherPrecedence(char op1, char op2)
  668. {
  669.     int op1Weight = GetOperatorWeight(op1);
  670.     int op2Weight = GetOperatorWeight(op2);
  671.  
  672.     // If operators have equal precedence, return true if they are left associative.
  673.     // BUT REMEMBER...return false, if right associative.
  674.     // if operator is left-associative, left one should be given priority.
  675.     if (op1Weight == op2Weight) {
  676.         if (IsRightAssociative(op1)) {
  677.             return false;
  678.         }
  679.         else {
  680.             return true;
  681.         }
  682.     }
  683.     return op1Weight > op2Weight ? true : false;
  684. }
  685.  
  686. string InfixToPostfix(string expression)
  687. {
  688.     stack <char> S;
  689.     string postfix = "";
  690.     for (int i = 0; i < expression.length(); i++)//
  691.     {
  692.         if (Flag(expression[i])) {
  693.             continue;
  694.         }
  695.         // If character is operator, pop two elements from stack, perform operation and push the result back.
  696.         else if (IsOperator(expression[i]))
  697.         {
  698.             while (!S.empty() && S.top() != '(' && HasHigherPrecedence(S.top(), expression[i])) {
  699.                 postfix += S.top();
  700.                 S.pop();
  701.             }
  702.             S.push(expression[i]);
  703.         }
  704.         else if (IsOperand(expression[i])) {
  705.             postfix += expression[i];
  706.         }
  707.         else if (expression[i] == '(') {
  708.             S.push(expression[i]);
  709.         }
  710.         else if (expression[i] == ')') {
  711.             while (!S.empty() && S.top() != '(') {
  712.                 postfix += S.top();
  713.                 S.pop();
  714.             }
  715.             S.pop();
  716.         }
  717.     }
  718.  
  719.     while (!S.empty()) {
  720.         postfix += S.top();
  721.         S.pop();
  722.     }
  723.  
  724.    return postfix;
  725. }
  726. void changeOfBrackets(string &a)
  727. {
  728.     int n = a.size();
  729.     char l = '(';
  730.     char r = ')';
  731.     for (size_t i = 0; i < n; i++)
  732.     {
  733.         if (IsLeftParenthesis(a[i]))
  734.         {
  735.             a[i] = r;
  736.         }
  737.         else if (IsRightParenthesis(a[i]))
  738.         {
  739.             a[i] = l;
  740.         }
  741.     }
  742. }
  743.  
  744.  
  745.  
  746.  
  747. void reverse1(string &expression)
  748. {
  749.     reverse(expression.begin(), expression.end());
  750.     changeOfBrackets(expression);  
  751. }
  752.  
  753. string InfixToPrefix(string &expression)
  754. {
  755.     reverse1(expression);
  756.     string c= InfixToPostfix(expression);  
  757.     reverse(c.begin(), c.end());
  758.     return c;
  759. };
  760.  
  761. int main()
  762. {
  763.  
  764.     //string a = "A+B*C-D*E";
  765.     string a = "(A+B/C*(D+E)-F)";
  766.     cout << InfixToPostfix(a) << endl;
  767.     //string b = "((A+B)*C-D)*E";
  768.     string c = "A+(B*C-(D/E^F)*G)";
  769.     //cout << InfixToPostfix(b) << endl;
  770.     cout << InfixToPrefix(c);//rezultatu trqbva da e: +A-*BC*/DÊFG
  771.    
  772.    
  773.     return 0;
  774. }
  775.  
  776. //QUEUE
  777. #include <iostream>
  778. #include <assert.h>
  779. #include <string.h>
  780. #include <vector>
  781. #include <fstream>
  782.  
  783.  
  784. using namespace std;
  785.  
  786.  
  787.  
  788. template <class T>
  789. struct node {
  790.     T data;
  791.     node<T> *next;
  792.     node(T& d, node<T>*nextt) :data(d), next(nextt) {}//podavame go po referencia zashtoto moje da podavame obekt i ne iskame da ni se izvikva cpy constr
  793. };
  794.  
  795. template<typename T>
  796. class queue {
  797. private:
  798.     node<T>* head;
  799.     node<T>* last;
  800. public:
  801.     queue()
  802.     {
  803.         head = NULL;
  804.     }
  805.     void push(T& newData)
  806.     {
  807.         node<T>* n = new node<T>(newData, null);
  808.         if (head == NULL)
  809.         {
  810.             head = n;
  811.         }
  812.         last->next = n;//moje i s while cikul s current
  813.         last = last->next;
  814.     }
  815.     void pop()
  816.     {
  817.         if (head == NULL)
  818.         {
  819.             cout << "the queue is empty ";
  820.         }
  821.         else
  822.         {
  823.             node<T>*temp = head;
  824.             head = head->next;
  825.             cout << "deleting the first node with value: " << temp->data;
  826.             delete temp;
  827.         }
  828.     }
  829.     bool isEmpty()
  830.     {
  831.         if (head == NULL)
  832.         {
  833.             return true;
  834.         }
  835.         return false;
  836.     }
  837.      T& front()
  838.     {
  839.         if (head == NULL)
  840.         {
  841.             cout << "empty ";
  842.         }
  843.         else
  844.         {
  845.             return head->data;
  846.         }
  847.     }
  848.  
  849. };
  850.  
  851. class Heap {// Priority queue
  852. private:
  853.     vector<int> v;
  854.  
  855. public:
  856.     int parent(int index)//parent of node with index 'index'
  857.     {
  858.         return v[index / 2];
  859.     }
  860.     int leftChild(int index)
  861.     {
  862.         return v[2 * index + 1];
  863.     }
  864.     int rightChild(int index)
  865.     {
  866.         return v[2 * index + 2];
  867.     }
  868.     void heapDown(int i)
  869.     {
  870.         int largest = i;
  871.         int left = 2*i+1;
  872.         int right = 2*i+2;
  873.  
  874.         if (i < v.size())
  875.         {
  876.             if (largest < left)
  877.             {
  878.                 swap(largest, left);
  879.             }
  880.             if (largest < right)
  881.             {
  882.                 swap(largest, right);
  883.             }
  884.         }
  885.     }
  886.     void swap(int &a, int &b)
  887.     {
  888.         int temp = a;
  889.         a = b;
  890.         b = temp;
  891.     }
  892. };
  893.  
  894. int main()
  895. {
  896.     queue<int> a;
  897.     int b = 1;
  898.     int c = 2;
  899.  
  900.     a.push(b);
  901.     a.push(2);
  902.     a.push(3);
  903.     a.front() = 10;
  904.     cout << a.front();
  905.  
  906.     return 0;
  907. }
  908.  
  909.  
  910. //sorted stack
  911. #include <iostream>
  912. #include <iomanip>
  913. #include <stack>
  914. #include <set>
  915. #include<fstream>
  916.  
  917. using namespace std;
  918.  
  919. void printStack(stack<int> s)
  920. {
  921.     while (!s.empty()) {
  922.         cout << s.top() << " ";
  923.         s.pop();
  924.     }
  925.     cout << endl;
  926. }
  927. stack<int> sortAscending(stack<int> s)// 1 3 2 4
  928. {
  929.     stack<int> newstack;
  930.     if (s.empty())
  931.     {
  932.         return s;
  933.     }
  934.     int x = s.top();
  935.     s.pop();
  936.     newstack.push(x);//x=1
  937.     while (!s.empty())
  938.     {
  939.         int temp = s.top();//3
  940.         s.pop();
  941.         while (!newstack.empty()&&temp>newstack.top())//3 > 1
  942.         {
  943.             s.push(newstack.top());//s.push(1), s= 1 2 4
  944.             newstack.pop();
  945.         }
  946.         newstack.push(temp);//newstack=3
  947.     }
  948.     return newstack;
  949. }
  950.  
  951. stack<int> sortDescending(stack<int> s)// 1 3 2 4
  952. {
  953.     stack<int> newstack;
  954.     if (s.empty())
  955.     {
  956.         return s;
  957.     }
  958.     int x = s.top();
  959.     s.pop();
  960.     newstack.push(x);//x=1
  961.     while (!s.empty())
  962.     {
  963.         int temp = s.top();//3
  964.         s.pop();
  965.         while (!newstack.empty() && temp<newstack.top())//3 > 1
  966.         {
  967.             s.push(newstack.top());//s.push(1), s= 1 2 4
  968.             newstack.pop();
  969.         }
  970.         newstack.push(temp);//newstack=3
  971.     }
  972.     return newstack;
  973. }
  974. int main()
  975. {
  976.     stack<int> s;
  977.     s.push(4);
  978.     s.push(2);
  979.     s.push(3);
  980.     s.push(1);
  981.     printStack(sortAscending(s));
  982.     printStack(sortDescending(s));
  983.    
  984. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement