数据结构day03

链栈,自己实现一遍,但是节点存储不是整数,存储学生信息(年龄,分数,姓名)三级引用。

1、建立学生信息结构体,将data改为学生信息结构体类型。

2、循环入栈和入队

#include<myhead.h>
typedef struct//学生信息结构体
{
	char name[20];
	int age;
	int score;
}stu;
typedef struct node//节点类型结构体
{
	stu data;
	struct node*next;
}Node;
typedef struct//栈顶指针结点结构体
{
	int len;
	Node *top;
}Stack,*Pstack;
typedef struct//创建队列头结点结构体
{
	int len;
	Node *rear;
	Node *front;
}Queue,*Pqueue;
Pstack creat_stack()
{
	Pstack p=malloc(sizeof(Stack));
	if(p==NULL)
	{
		printf("创建失败\n");
		return NULL;
	}
p->len=0;
p->top=NULL;
return p;
}
int push_stack(Pstack L,stu s)
{
    if(L==NULL)
    {
        printf("链栈不存在\n");
        return -1;
    }
    Node *p = malloc(sizeof(Node));//生成正常的新节点

    p->data  =s;//将主函数中的stu类型赋值给新结点

    p->next = L->top;
    L->top = p;//栈顶指针指向新节点
    L->len++;//计数器+1
    printf("入栈成功\n");
    return 0;
}
 Pqueue creat_queue()
{
    Pqueue p = malloc(sizeof(Queue));
    if(NULL==p)
    {
        printf("申请队列失败\n");
        return NULL;
    }
    p->front = 0;
    p->rear = 0;
	p->len=0;
    return p;
}
int input_queue(Pqueue M,stu s)
{
   if(M==NULL)
    {
        printf("队列不存在\n");
        return -1;
    }
    Node * p = malloc(sizeof(Node));
    p->data = s;

    if(M->rear==NULL)//当只有一个节点时
    {
        M->rear=p;//队头指针和队尾指针都指向新节点
        M->front=p;
    }
    else
    {
        M->rear->next = p;//队尾节点指针域指向新节点
        M->rear = p;//队尾指针指向新节点
    }
    M->len++;
    printf("入队成功\n");
    return 0;
} 
int output_queue(Pqueue M)
{
    if(M==NULL||M->len==0)
    {
        printf("队列不存在或者为空\n");
        return -1;
    }
    int i;
    Node *t  = M->front;
    for(i = 0;i<M->len;i++)
    {
        printf("%s\t%d\t%d\n",t->data.name,t->data.age,t->data.score);
        t = t->next;
    }
    printf("\n");
    return 0;
}
int main()
{
	Pstack L=creat_stack();
	Pqueue M=creat_queue();
	stu s[3]={{"张三",20,87},
		      {"李四",21,76},
			  {"王五",24,87}};
	for(int i=0;i<3;i++)
	{
	push_stack(L,s[i]);
	input_queue(M,s[i]);
	}
	output_queue(M);
	return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值