C++ USES one stack to implement an example of a sorting algorithm for another stack

  • 2020-05-19 05:14:02
  • OfStack

This paper illustrates the sorting algorithm of C++ using one stack to realize another stack. I will share it with you for your reference as follows:

Topic:

The element type in 1 stack is integer. If you want to sort the stack from the top to the bottom in the order from small to large, you can only apply for 1 auxiliary stack.

In addition, you can apply for new variables, but not for additional data structures. How do you do sorting?

Algorithm C++ code:


class Solution
{
public:
  // With the help of 1 Sort the source stack on a temporary stack 
  static void sortStackByStack(stack<int>& s)
  {
    stack<int>* sTemp = new stack<int>;
    while (!s.empty())
    {
      int cur = s.top();
      s.pop();
      // When the top element in the source stack is larger than the top element in the temporary stack, the top element in the temporary stack is placed back on the source stack 
      // Ensure that elements in the temporary stack are large and small from bottom to top 
      while (!sTemp->empty() && cur > sTemp->top())
      {
        int temp = sTemp->top();
        sTemp->pop();
        s.push(temp);
      }
      sTemp->push(cur);
    }
    // Place the elements of the temporary stack from the top of the stack into the source stack 
    while (!sTemp->empty())
    {
      int x = sTemp->top();
      sTemp->pop();
      s.push(x);
    }
  }
};

Test case procedure:


#include <iostream>
#include <stack>
using namespace std;
class Solution
{
public:
  // With the help of 1 Sort the source stack on a temporary stack 
  static void sortStackByStack(stack<int>& s)
  {
    stack<int>* sTemp = new stack<int>;
    while (!s.empty())
    {
      int cur = s.top();
      s.pop();
      // When the top element in the source stack is larger than the top element in the temporary stack, the top element in the temporary stack is placed back on the source stack 
      // Ensure that elements in the temporary stack are large and small from bottom to top 
      while (!sTemp->empty() && cur > sTemp->top())
      {
        int temp = sTemp->top();
        sTemp->pop();
        s.push(temp);
      }
      sTemp->push(cur);
    }
    // Place the elements of the temporary stack from the top of the stack into the source stack 
    while (!sTemp->empty())
    {
      int x = sTemp->top();
      sTemp->pop();
      s.push(x);
    }
  }
};
void printStack(stack<int> s)
{
  while (!s.empty())
  {
    cout << s.top() << " ";
    s.pop();
  }
  cout << endl;
}
int main()
{
  stack<int>* s = new stack<int>;
  s->push(5);
  s->push(7);
  s->push(6);
  s->push(8);
  s->push(4);
  s->push(9);
  s->push(2);
  cout << " Stack before sorting: " << endl;
  printStack(*s);
  Solution::sortStackByStack(*s);
  cout << " Sorted stack: " << endl;
  printStack(*s);
  system("pasue");
}

Operation results:


 Stack before sorting: 
2 9 4 8 6 7 5
 Sorted stack: 
9 8 7 6 5 4 2

I hope this article is helpful to you C++ programming.


Related articles: