题目:
设顺序栈S中有2n个元素,从栈顶到栈底的元素依次为a2n,a2n-1,…,a2,a1,要求通过一个循环队列重新排列栈中元素,使得从栈顶到栈底的元素依次为a2n,a2n-2,…,a2,a2n-1,a2n-3,…,a1,请设计算法实现该操作,要求空间复杂度和时间复杂度均为O(n).
PS:作业记录
using namespace std;
#include<iostream>
#include<string>
#define Stack_Size 100
#define Queue_Size 100
#define STACK_INCREMENT 10
typedef struct Stack //栈的结构体定义
{
int *S_data; //数据域
int top; //顺序栈栈顶
int stacksize; //顺序栈当前长度
}Stack;
typedef struct Queue //循环队列结构体定义
{
int *Q_data;
int front; //队头指针
int rear; //队尾指针
}Queue;
//顺序栈函数定义
Stack Creat_Stack(int a)
{
int length = a;
int data = 0;
Stack ST;
ST.S_data = new int[Stack_Size]; //动态申请
if (!ST.S_data)
cout << ("内存申请失败!") << endl;
ST.top = -1;
ST.stacksize = Stack_Size;
cout << "请输入顺序栈元素内容:" << endl;
for (int i = 0; i < length; i++)
{
cin >> data;
ST.S_data[i] = data;
ST.top++;
}
cout << "顺序栈创建成功!" << endl;
return ST;
}
//销毁栈
void Destroy_Stack(Stack &l)
{
delete[] l.S_data;
l.top = -1;
l.stacksize = 0;
cout << "该顺序栈销毁成功!" << endl;
}
//增加空间
void Increment_Stack(Stack &ST)
{
int *newstack = new int[ST.stacksize + STACK_INCREMENT];
if (!newstack)
cout << ("存储分配失败!") << endl;
for (int i = 0; i < ST.top; i++)
{
newstack[i] = ST.S_data[i];
}
ST.S_data = newstack;
delete[] ST.S_data;
ST.stacksize += STACK_INCREMENT;
}
//入栈
void Push_Stack(Stack &ST, int e)
{
if (ST.top == (ST.stacksize - 1))
Increment_Stack(ST);
ST.S_data[++ST.top] = e;
}
//出栈
int Pop_Stack(Stack &ST, int e)
{
if (ST.top == -1)
cout << ("该顺序栈为空栈!") << endl;
return e = ST.S_data[ST.top--];
}
//打印栈,仅用于观看结果
void Print_Stack(Stack &ST)
{
cout << "该顺序栈的元素是:" << endl;
for (int i = ST.top; i > -1; i--)
{
cout << ST.S_data[i] << " ";
}
cout << endl;
}
//循环队列函数定义
void Creat_Queue(Queue &queue)
{
queue.Q_data = new int[Stack_Size];
if (!queue.Q_data)
cout << ("存储分配失败!") << endl;
queue.front = queue.rear = 0;
}
//销毁队列
void Destroy_Sqqueue(Queue &queue)
{
delete[] queue.Q_data;
queue.front = queue.rear = 0;
cout << "该循环队列销毁成功!";
}
//入队
void En_Queue(Queue &queue, int e)
{
if (((queue.rear + 1) % Queue_Size) == queue.front)
cout << ("该循环队列已满!") << endl;
queue.Q_data[queue.rear] = e;
queue.rear = (queue.rear + 1) % Queue_Size;
}
//出队
int De_Queue(Queue &queue, int e)
{
if (queue.front == queue.rear)
cout << ("该循环队列为空队列!") ;
e = queue.Q_data[queue.front];
queue.front = (queue.front + 1) % Queue_Size;
return e;
}
//打印队列,仅用于观看结果
void Print_Queue(Queue &queue)
{
cout << "该循环队列的元素是:";
while (queue.front != queue.rear)
{
cout << queue.Q_data[queue.front] << " ";
queue.front = (queue.front + 1) % Queue_Size;
}
cout << endl;
}
//测试
void test01()
{
Stack s; Queue q; //创建栈和队列
int e = 0, n = 0, temp = 0;
Creat_Queue(q);
cout << "请输入顺序栈长度(栈的长度为偶数):" ;
cin >> n;
s = Creat_Stack(n); //初始化顺序栈
cout << "\n原始顺序栈: " << endl;
Print_Stack(s);
for (int i = 0; i < n; i++)
{
temp = Pop_Stack(s, e);
En_Queue(q, temp); //将出栈后的元素依次入队
}
for (int i = 0; i < n; i++)
{
temp=De_Queue(q, e);
if (i % 2 == 0) //判断是否为偶数结点
Push_Stack(s, temp); //若是偶数结点,元素入栈
else
En_Queue(q, temp); //若是奇数结点,元素再次入队
}
for (int i = 0; i < n / 2; i++)
Push_Stack(s, De_Queue(q, e)); //循环队列中的奇数结点,依次入栈,此时所有元素皆在顺序栈中
for (int i = 0; i < n; i++)
En_Queue(q, Pop_Stack(s, e)); //将顺序栈中的元素依次出栈并入队,此时所有元素皆在循环队列中
for (int i = 0; i < n; i++)
Push_Stack(s, De_Queue(q, e)); //将循环队列中的元素依次再次入队,此时顺序栈中的元素排列为题目要求
cout << "\n更改后的:" << endl;
Print_Stack(s); //打印
}
int main()
{
test01();
return 0;
}
运行结果: