数据结构作业

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值