吠错了树 2018-12-06 07:19 采纳率: 0%
浏览 490

栈的基本操作,为什么输出有乱码?

#include
#include
#include
#define OK 1
#define ERROR 0
#define Stack_Size 20//定义栈最多能够存储的元素个数
typedef struct
{
int elem[Stack_Size];//利用一维数组顺序存储栈中的元素
int top;//整型变量top存储栈顶元素的下标,作为栈顶指针,top为-1时表示空栈
}SeqStack;

int InitStack(SeqStack );//初始化栈,即将一个栈清除为空
int StackEmpty(SeqStack *);//检查一个栈是否为空
int CreateStack(SeqStack *, int);//随机数顺序栈的建立
int pop(SeqStack
,int*);//向一个栈中删除元素
int push(SeqStack , int);//向一个栈中插入元素
void Print(SeqStack
);//输出栈
int Copy(SeqStack*, SeqStack*);//将栈S1中的元素倒序输入到栈S2中

int main()
{
SeqStack S1, S2; int n;
InitStack(&S1);
InitStack(&S2);
StackEmpty(&S1);
StackEmpty(&S2);
printf("请输入你要输入的元素个数:\n");
scanf_s("%d", &n);
CreateStack(&S1, n);
Print(&S1);
Copy(&S1, &S2);
Print(&S2);
return OK;
}
int InitStack(SeqStack *S)
{
S->top = -1;
return OK;
}
int StackEmpty(SeqStack *S)
{
return(S->top == -1 ? OK : ERROR);
}
int CreateStack(SeqStack *S, int n)
{
srand((unsigned)time(0));//srand种下随机种子,time(0)是得到当前的时间值,每时每刻的时间都不一样,所以每次执行得到的随机数都不一样
for (int i = 0; i < n; i++)
{
S->elem[++S->top] = rand() % 8+1;//随机抽取的从1到8的数字
}
return OK;
}
int pop(SeqStack *S, int *x)
{
if (S->top == -1) return ERROR;
else
{
*x = S->elem[S->top];
S->top--;
return OK;
}
}
int push(SeqStack *S, int x)
{
if (S->top = Stack_Size - 1) return ERROR;
S->top++;
S->elem[S->top] = x;
return OK;
}
void Print(SeqStack *S)
{
printf("输出元素:\n");
for (int i = S->top; i >= 0; i--)
{
printf("%d", S->elem[i]);
}
printf("\n");
}
int Copy(SeqStack *S1, SeqStack *S2)
{
int t;
for (int i=S1->top; i>=0; i--)
{
pop(S1, &t);
push(S2, t);
}
return OK;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?