#include <myhead.h>
typedef struct
{
int age;
float score;
char name[20];
}stu;
typedef struct node
{
stu data;
struct node *next;
}zch,*Pzch;
typedef struct
{
Pzch top;
Pzch next;
}Stack,*Pstack;
Pstack create()
{
Pstack S=malloc(sizeof(Stack));
if(S==NULL)
{
printf("申请失败\n");
return NULL;
}
S->top=NULL;
S->next=NULL;
return S;
}
int push(Pstack S,stu e)
{
if(S==NULL)
{
printf("入栈失败\n");
return -1;
}
Pzch p=malloc(sizeof(zch));
p->data=e;
p->next=S->next;
S->next=p;
S->top=p;
}
int pop(Pstack S)
{
if(S==NULL||S->top==NULL)
{
printf("出栈失败\n");
return -1;
}
printf("出栈元素:%d\t%.2f\t%s\n",S->top->data.age,S->top->data.score,S->top->data.name);
Pzch Q=S->top;
S->top=Q->next;
S->next=S->top;
free(Q);
Q=NULL;
}
void output(Pstack S)
{
Pzch t=S->next;
printf("学生信息如下:\n");
while(t!=NULL)
{
printf("age:\tscore\tname\n");
printf("%d\t%.2f\t%s\n",t->data.age,t->data.score,t->data.name);
t=t->next;
}
}
int main(int argc, const char *argv[])
{ int i,n,m;
stu e;
Pstack S=create();
printf("请输入学生人数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("age\tscore\tname\n");
scanf("%d%f%s",&e.age,&e.score,e.name);
push(S,e);
}
printf("请输入出栈学生人数:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
pop(S);
}
output(S);
return 0;
}
数据结构作业
于 2024-11-04 08:29:30 首次发布