C++高级知识Stack内四个数据的大小排序分析分享

#include <iostream>

#include<string>

using namespace std;

//First we need to create two Stack,one stack to store the data we have and another one extra stack to store temporary data.

//Using the two stacks,we can sort the data in the first stack.

class Node{

public:

int num;

Node* next;

};

class Stack {

public:

Stack(int size = 10);

~Stack() { delete [] values; }

bool IsEmpty() { return top == -1; }

bool IsFull() { return top == maxTop; }

double Top();

void Push(const double x);

double Pop();

void DisplayStack();

private:

int maxTop;

int top;

double* values;

};

Stack::Stack(int size /*= 10*/) {

maxTop = size - 1;

values = new double[size];

top = -1;

}

void Stack::Push(const double x) {

if (IsFull())

cout << "Error: the stack is full." << endl;

else

values[++top] = x;

}

double Stack::Pop() {

if (IsEmpty()) {

cout << "Error: the stack is empty." << endl;

return -1;

}

else {

return values[top--];

}

}

double Stack::Top() {

if (IsEmpty()) {

cout << "Error: the stack is empty." << endl;

return -1;

}

else

return values[top];

}

void Stack::DisplayStack() {

cout << "top -->";

for (int i = top; i >= 0; i--)

cout << "\t|\t" << values[i] << "\t|" << endl;

cout << "\t|---------------|" << endl;

}

class tmpStack {

public:

tmpStack(int size = 10);

~tmpStack() { delete [] values; }

bool IsEmpty() { return top == -1; }

bool IsFull() { return top == maxTop; }

double Top();

void Push(const double x);

double Pop();

void DisplaytmpStack();

private:

int maxTop;

int top;

double* values;

};

tmpStack::tmpStack(int size /*= 10*/) {

maxTop = size - 1;

values = new double[size];

top = -1;

}

void tmpStack::Push(const double x) {

if (IsFull())

cout << "Error: the stack is full." << endl;

else

values[++top] = x;

}

double tmpStack::Pop() {

if (IsEmpty()) {

cout << "Error: the stack is empty." << endl;

return -1;

}

else {

return values[top--];

}

}

double tmpStack::Top() {

if (IsEmpty()) {

cout << "Error: the stack is empty." << endl;

return -1;

}

else

return values[top];

}

void tmpStack::DisplaytmpStack() {

cout << "top -->";

for (int i = top; i >= 0; i--)

cout << "\t|\t" << values[i] << "\t|" << endl;

cout << "\t|---------------|" << endl;

}

//So now we have created two stack(stack and tmpstack),we will start the main function.

int main(void) {

int tmp;

int tmpst;

int num = 4;

cout<< "(1)push the elements 55,100,45,60 onto the stack st."<<endl;

//we insert the elements we want to sort in stack.

Stack Stack(55);

Stack.Push(55);

Stack.Push(100);

Stack.Push(45);

Stack.Push(60);

cout<< "(2)The process of sorting element of stack st is:"<<endl;

tmpStack tmpStack(55);

tmpStack.Push(22);

tmpStack.Pop();//now, our tmpstack is empty,and stack have four elements.

tmp = Stack.Pop();//we make tmp = stack.pop ,and then push it on tmpstack.

cout<<"Stack: pop out "<<tmp<<"=>"<<endl;

int n = 0;

while(n!=4){//n! =4 because we have only four elements, and the while loop will determine the size between the elements. So the loop runs at most four times.

while(tmp<Stack.Top()&&!Stack.IsEmpty())

// push tmp from stack to tmpstack.

//If tmp from stack is smaller than the new top of stack Then pop the new top value down and make it tmp, it will be pushed on the tmpstack in the next loop.

//I do this so that the larger value is always the top of the tmpstack.

{

tmpStack.Push(tmp);

cout<<"tmpStack: push "<<tmp<<"=>"<<endl;

tmp = Stack.Pop();

cout<<"Stack: pop out "<<tmp<<"=>"<<endl;

cout<<"tmp: get the top element of Stack "<<tmp<<"=>"<<endl;}

while(tmp>Stack.Top()&&!Stack.IsEmpty()){

// push tmp, the value popped from the stack, to the tmpstack. If tmp, the value popped from the stack, is larger than the value of the new top of the stack, then switch the positions of the two values so that the larger value is always the top value of the tmpstack. Then tmp = Stack.Pop() and enter the next loop.

tmpStack.Push(tmp);

cout<<"tmpStack: push "<<tmp<<"=>"<<endl;

tmpst = Stack.Pop();

cout<<"Stack: pop out "<<tmpst<<"=>"<<endl;

cout<<"tmpst: get the top element of Stack "<<tmpst<<"=>"<<endl;

cout<<"Since  "<<tmp<<" > "<<tmpst<<" Stack: pop out "<<tmpst <<" tmpStack: pop "<<tmp<<" tmpStack: push "<< tmpst <<" Stack: push "<< tmp <<endl;

tmpStack.Pop();

Stack.Push(tmp);

tmpStack.Push(tmpst);

tmp = Stack.Pop();

cout<<"Stack: pop out "<<tmp<<"=>"<<endl;}// Through these two loops, we gradually move elements from the stack onto the tmpstack, always keeping the top of tempstack at its maximum value.

if(Stack.IsEmpty())// Because tmp = Stack.Pop() after each while loop, when the loop terminates, the program pops one more number, and we push it back.

// This also makes the maximum the only element in the stack.

    {

        Stack.Push(tmp);

        cout<<"Stack: push "<<tmp<<"=>"<<endl;

    }

n++;

}

// After this big loop, only one element is left in the stack, and this element is the largest of the four elements, while the other three elements are placed in the tmpstack.

// Next I will sort the three elements in the tmpstack.

Stack.DisplayStack();

tmpStack.DisplaytmpStack();

// The next operation is similar to the previous one, except this time instead of finding the maximum value, we find the minimum value and make it the only element in the tmpstack. This time, the tmpstack is used to store data, and the stack is used to store temporary data.

tmpst = tmpStack.Pop();

cout<<"tmpStack: pop out "<<tmpst<<"=>"<<endl;

cout<<"tmpst: get the top element of tmpStack "<<tmpst<<"=>"<<endl;

int b =0;

while(b<2)

// Since there are only three elements in the tmpstack, we only need to loop twice at most.

{

while(tmpst<tmpStack.Top()&&!tmpStack.IsEmpty())

// push the value tmpst from tmpstack to the stack. If the value tmpst from tmpstack is smaller than the value of the new top of tmpstack, then switch the positions of the two values. This allows the smaller value to be the top of the stack and pushed onto the stack in the next loop. Then make tmpst = tmpStack.Pop() to enter the next loop.

{Stack.Push(tmpst);

cout<<"Stack: push "<<tmpst<<"=>"<<endl;

tmp = Stack.Pop();

tmpst=tmpStack.Pop();

 tmpStack.Push(tmp);

 Stack.Push(tmpst);

 cout<<"Since  "<<tmpst<<" > "<<tmp<<" Stack: pop out "<<tmp <<" tmpStack: pop "<<tmpst<<" tmpStack: push "<< tmp <<" Stack: push "<< tmpst <<endl;

 tmpst = tmpStack.Pop();

cout<<"tmpStack: pop out "<<tmpst<<"=>"<<endl;

cout<<"tmpst: get the top element of tmpStack "<<tmpst<<"=>"<<endl;b++;

}

while(tmpst>tmpStack.Top()&&!tmpStack.IsEmpty())

// push the previous value tmpst from the tmpstack onto the stack. If the value tmpst from the tmpstack is larger than the new top value of the tmpstack, then the smaller value tmpst is pushed onto the stack. Then make tmpst = tmpStack.Pop() to enter the next loop.

{

Stack.Push(tmpst);

cout<<"Stack: push "<<tmpst<<"=>"<<endl;

tmpst=tmpStack.Pop();

cout<<"Since "<<tmp<<" > "<<tmpst<<"=>"<<endl;

cout<<"tmpStack: pop out "<<tmpst<<"=>"<<endl;

cout<<"tmpst: get the top element of tmpStack "<<tmpst<<"=>"<<endl;

b++;

}

}

// With two loops, we can gradually transfer elements to the stack, leaving tempstack with only one element, and that element is the minimum.

 tmpStack.Push(tmpst);

// This step is because we have completed the loop, but since each loop will pop the top of the tmpstack at the end, we will push it back to the tmpstack.

// This also makes the minimum the only element in the tmpstack.

   cout<<"tmpStack: push "<<tmpst<<"=>"<<endl;

Stack.DisplayStack();

tmpStack.DisplaytmpStack();

tmp = Stack.Pop();

// The last step is very simple, we have found the maximum and minimum values of the four elements, and the maximum value is at the bottom of the stack, and the minimum value is the only element in the tmpstack.

// We just need to determine the size relationship between the two elements above the maximum value and arrange them in the stack in order, and finally push the minimum value in the tmpstack to the top of the stack.

// Finally, we get the answer we want in the stack.

cout<<"Stack: pop out "<<tmp<<"=>"<<endl;

if(tmp>Stack.Top())

{

 int tmp1 = Stack.Pop();

 Stack.Push(tmp);

 Stack.Push(tmp1);

 tmpst = tmpStack.Pop();

 cout<<"Since "<<tmp<<" > "<<tmp1<<"=>"<<endl;

 cout<<"Stack: pop out "<<tmp1<<"=>"<<endl;

 cout<<"tmpStack: pop out "<<tmpst<<"=>"<<endl;

 cout<<"Stack: push "<<tmp<<"=>"<<endl;

 cout<<"Stack: push "<<tmp1<<"=>"<<endl;

 Stack.Push(tmpst);

 cout<<"Stack: push "<<tmpst<<"=>"<<endl;

}

if(tmp<Stack.Top())

{   cout<<"Since "<<tmp<<" <"<<Stack.Top()<<"=>"<<endl;

    Stack.Push(tmp);

    cout<<"Stack: push "<<tmp<<"=>"<<endl;

    tmpst = tmpStack.Pop();

    cout<<"tmpStack: pop out "<<tmpst<<"=>"<<endl;

    Stack.Push(tmpst);

    cout<<"Stack: push "<<tmpst<<"=>"<<endl;

}

Stack.DisplayStack();

tmpStack.DisplaytmpStack();

return 0;

} 作者:空檪炑 https://www.bilibili.com/read/cv21251178?from=search&spm_id_from=333.337.0.0 出处:bilibili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值