#include<iostream>
#include<string>
using namespace std;
void push(int *stack, int num, int &index, int stack_len)
{
if (index > stack_len - 1)
cout << "the stack is enough, please pop the data!" << endl;
else
{
stack[index] = num;
++index;
}
}
void pop(int *stack, int &index)
{
if (index == 0)
cout << "the stack is empty, please push the data!" << endl;
else
{
cout << endl << '\t' <<stack[index - 1];
--index;
}
}
void peak(int *stack, int &index)
{
if (index == 0)
cout << "the stack is empty, please push the data!" << endl;
else
{
cout << endl << '\t' <<stack[index - 1];
}
}
void show(int *stack, int &index)
{
if (index == 0)
cout << "the stack is empty, please push the data!" << endl;
else
{
for (int i = index; i > 0; i--)
cout << endl << '\t' <<stack[i - 1];
}
}
void ex_dao(int *stack1, int *stack2, int index)
{
for(int i = index; i > 0; i--)
stack2[index - i] = stack1[i - 1];
}
int main()
{
int stack_len, num;
cout << "please input the length of the queue:" << '\t';
cin >> stack_len;
int *stack1 = new int [stack_len];
int *stack2 = new int [stack_len];
int index = 0;
string str;
cout << endl << "input data. please select the method: push pop peak show exit: " << '\t';
while(cin >> str)
{
cout << endl;
if(str == "push")
{
cout << "please input the data: " << '\t';
cin >> num;
push(stack1, num, index, stack_len);
}
else if(str == "pop")
{
ex_dao(stack1, stack2, index);
pop(stack2, index);
ex_dao(stack2, stack1, index);
}
else if(str == "peak")
{
ex_dao(stack1, stack2, index);
peak(stack2, index);
ex_dao(stack2, stack1, index);
}
else if(str == "show")
show(stack1, index);
else if(str == "exit")
{
delete [] stack1;
delete [] stack2;
return 0;
}
else
cout << "try again" << endl;
cout << endl << "input data. please select the method: push pop peak exit: " << '\t';
}
system("pause");
return 0;
}
用栈实现队列(基于数组)
最新推荐文章于 2022-08-06 01:24:26 发布