利用temp栈将一个非空栈s1的所有元素按原样复制到另一个栈s2当中去。
个人答案:
int CopyStack(SqStack S1, SqStack* S2, SqStack temp)
{
DataType e;
// 将S1的元素弹出并压入temp
while (!StackEmpty(S1))//当s1栈未弹空时
{
Pop(&S1, &e);
Push(&temp, e);
}
// 将temp的元素弹出并压入S2
while (!StackEmpty(temp)) //当temp栈未弹空时
{
Pop(&temp, &e);
Push(S2, e);
}
return 1; // 复制成功
}
函数接口定义:
int CopyStack(SqStack S1,SqStack *S2,SqStack temp);/*本题要求函数*/
其中 S1
代表源栈, S2
代表目的栈,temp代表中间栈。
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
#define STACKSIZE 100
typedef struct {
DataType items[STACKSIZE];
int top;
}SqStack;
int InitStack(SqStack* S)
{ // 初始化顺序栈
S->top = -1;
return 1;
}
int StackEmpty(SqStack S)
{ // 判栈空
if (S.top == -1) return 1;
else return 0;
}
int Push(SqStack* S, DataType e)
{ // 压栈函数
if (S->top >= STACKSIZE - 1) {
return 0;
}
S->top++;
S->items[S->top] = e;
return 1;
}
int Pop(SqStack* S, DataType* e)
{ // 弹栈函数
if (S->top <= -1) {
return 0;
}
*e = S->items[S->top];
S->top--;
return 1;
}
int TraverseStack(SqStack S)
{ // 遍历顺序栈
int i;
for (i = 0; i < S.top + 1; i++)
printf("%d ", S.items[i]);
return 1;
}
/* 本题要求函数——利用temp栈将一个非空栈s1的所有元素按原样复制到另一个栈s2当中去 */
int CopyStack(SqStack S1, SqStack* S2, SqStack temp);
int main()
{
int i, input;
SqStack S1, S2, temp; //定义顺序栈
InitStack(&S1);//初始化建空栈
InitStack(&S2);
InitStack(&temp);
for (i = 0;; i++)
{
scanf("%d", &input); // 某些编译器要求此处改为scanf_s
if (input == -1)break;
Push(&S1, input);
}
CopyStack(S1, &S2, temp);
TraverseStack(S2);//遍历
return 0;
}
/* 请在这里填写答案 */
输入样例:
这里给出一组输入,输入-1结束。例如:
1 2 3 4 5 6 -1
输出样例:
这里给出相应的输出。例如:
1 2 3 4 5 6
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB