作业帮 > 综合 > 作业

用栈操作来实现一组有序数据的倒排输出问题

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/18 08:33:20
用栈操作来实现一组有序数据的倒排输出问题
例如,已有有序数据为:1,2,3,4,5,6,7,我们需要用栈操作的先进后出的特性来实现它们的倒排输出,即7,6,5,4,3,2,1.
// cghhj.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#define MAXSIZE 100
typedef struct{
int data[MAXSIZE];
int top;
}SeqStack;
SeqStack *init_SeqStack()
{
SeqStack *s;
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int Empty_SeqStack(SeqStack *s){
if(s->top==-1){ return 1;printf("表为空");}
else return 0;
}
int into_SeqStack(SeqStack *s,int x){
if(s->top==MAXSIZE-1){ return 0;
printf("表满,不能插入");}
else s->top++;
s->data[s->top]=x;
return 1;
}
void printf_SeqStack(SeqStack *s){
int i,j=s->top;
for(i=0;idata[s->top]);
}
int Top_SeqStack(SeqStack *s){
if(Empty_SeqStack(s)){ return 0;
printf("表空");}
else return (s->data[s->top]);
}
void Destory_SeqStack(SeqStack *s){
if(s!=NULL){free(s); s=NULL;}
}
int main(int argc, char* argv[])
{ SeqStack *s=NULL;
int d;
s=init_SeqStack();
printf("\n请输入要入栈的数据元素(当输入-1时退出):");
do{
scanf("%d",&m);
if(m==-1) break;
into_SeqStack(s,m);
}while(1);
printf("\n当前出栈的顺序为:");
Printf_SeqStack(s);
}
Destory_SeqStack(s);
printf("Hello World!\n");
return 0;
}
再问: 编译器不能搞定stdafx.h,能修改下吗
再答: 你抄中间的代码