吠错了树 2018-12-06 09:35 采纳率: 0%
浏览 636

一个栈中的元素顺序插入另一个栈

#include
#include
#include
#define OK 1
#define ERROR 0
#define Stack_Size 20//定义栈最多能够存储的元素个数
typedef struct
{
int stacksize//当前已分配储存已使用空间
int *base//栈底指针
int *top;//整型变量top存储栈顶元素的下标,作为栈顶指针,top为-1时表示空栈
}SeqStack;

int InitStack(SeqStack );//初始化栈,即将一个栈清除为空
int StackEmpty(SeqStack *);//检查一个栈是否为空
int pop(SeqStack
,int*);//向一个栈中删除元素
int push(SeqStack , int);//向一个栈中插入元素
void Print(SeqStack
);//输出栈

int main()
{
SeqStack p = (SeqStack)malloc(sizeof(SeqStack)), q = (SeqStack)malloc(sizeof(SeqStack));
InitStack(p);
InitStack(q);
srand(time(NULL));
int i = 0, x, A[100];
cout << "随机生成100个要入栈的数字...." << endl;
while (i < 100)
{
cin.unget();
A[i] = rand() % 200;
Push(p, A[i++]);
}
cout << "打印第一个栈的内容:" << endl;
for (i = 0; i < p->sacksize; i++)
{
printf("%-4d", A[i]);
if ((i + 1) % 10 == 0)cout << endl;
}
system("pause");
cout << "打印第二个栈的内容:" << endl;
for (i = 0; p->sacksize != 0; i++)
{
Pop(p, &x);
Push(q, x);
printf("%-4d", x);
if ((i + 1) % 10 == 0)cout << endl;
}
system("pause");
}
int InitStack(SeqStack S)
{
s->base = (int
)malloc(Stack_Size * sizeof(int));
s->top = s->base;
s->sacksize = 0;
}
int StackEmpty(SeqStack *S)
{
return(s->top == s->base ? OK: ERROR);
}
int pop(SeqStack *S, int *x)
{
if (s->sacksize == 0)
return FALSE;
else
{
*x = *(--s->top);
*(s->top) = NULL;
s->sacksize--;
}
return TRUE;

}
int push(SeqStack *S, int x)
{
if (Stack_Size == s->sacksize)
return FALSE;
else
{
*(s->top++) = e;
s->sacksize++;
}
return TRUE;
}
void Print(SeqStack *S)
{
printf("输出元素:\n");
for (int i = S->top; i >=S->base; i--)
{
printf("%d", S->elem[i]);
}
printf("\n");
}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
    • ¥15 画两个图 python或R