队列---学生信息输入输出

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

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

2、循环入栈和入队。

#include <myhead.h>
#define N 4
typedef struct//重定义数据结构 
{
    int age;
    int score;
    char name[20];
}stu;
typedef struct node
{
    stu data;
    struct node *next;
}Node;
typedef struct
{
    int len;
    Node *rear;
    Node *front;
}Queue,*pqueue;
pqueue creat_queue()
{
    pqueue p=malloc(sizeof(Queue));//创建新队列
    if(p==NULL)//判断是否创建成功
    {
        printf("队列创建失败\n");
    }
    p->rear=NULL;//队头指针指向空指针
    p->front=NULL;//同上
    p->len=0;//队列长度置零;
    return p;//返回队头地址
}
void in_queue(pqueue L)
{
    if(L==NULL)//判断
    {
        printf("队列不存在\n");
    }
    Node *p=malloc(sizeof(Node));//创建新节点
    printf("请输入学生信息:");
    scanf("%d%s%d",&p->data.age,p->data.name,&p->data.score);
    if(L->rear==NULL)//判断是否是第一个节点
    {
        L->rear=p;
        L->front=p;
    }
    else
    {
        L->rear->next=p;
        L->rear=p;
    }
    L->len++;
    printf("输入成功\n");
}
void output_queue(pqueue L)
{
    if(L==NULL||L->len==0)//判断
    {
        printf("队列不存在或为空\n");
    }
    Node *p=L->front;
    for(int i=0;i<L->len;i++)//循环输出
    {
        printf("%d\t%s\t%d\n",p->data.age,p->data.name,p->data.score);
        p=p->next;
    }
}
void out_queue(pqueue L)
{
    if(L==NULL)//判断
    {
        printf("队列不存在\n");
    }
    Node *p=L->front;
    L->front=L->front->next;//指向下一个节点
    printf("弹出:\n");
    printf("%d\t%s\t%d\n",p->data.age,p->data.name,p->data.score);
    free(p);//销毁弹出节点
    p=NULL;
    L->len--;
}
int main(int argc, const char *argv[])
{
    pqueue L=creat_queue();
    for(int i=0;i<N;i++)//循环输入
    {
        in_queue(L);
    }
    output_queue(L);
    out_queue(L);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值