#include<iostream>
#include<cstdio>
#include<fstream>
#include<ctime>
#include<cstdlib>
using namespace std;
typedef int SElemtype;
//创建栈结点
typedef struct StackNode{
SElemtype data; //数据域
struct StackNode *next;//指针域
}StackNode,*LinkStack;
void InitStack(LinkStack &S){
S=NULL; //构造一个空栈,栈顶指针置为空;
}
int StackEmpty(LinkStack S){
if(S==NULL) return 1;
return 0;
}
int Push(LinkStack &S,SElemtype &e){
StackNode *p=new StackNode; //生成新结点p
p->data=e; //将新结点数据域置为e
p->next=S; //将新结点插入栈顶
S=p; //修改栈顶指针
return 1;
}
void TraverseStack(LinkStack S)
{ ofstream out;
out.open("out.txt");
while(S!=NULL){
out<<S->data<<" ";
S=S->next;
}
out.close();
cout<<"OK"<<endl;
}
int main(){
srand(time(0));//生成随机数种子
ofstream output;
output.open("in.txt");
int T = 100+4;
while (T--)
{
int num = rand() % 100;
output << num << " ";
}
output.close();
ifstream input;
input.open("in.txt");
LinkStack S;
InitStack(S);
int h;
while(input>>h){
Push(S,h);
}
TraverseStack(S);
return 0;
}
111
最新推荐文章于 2022-07-07 23:22:40 发布