链队列——出入队列

首先就是定义一个结构结构体 ,用来装队列的头和尾

#include<stdio.h>
#include<iostream>
using namespace std;
struct node
{
    int data;
    node *nex;
};
struct Queue
{
    node *head=new node;
    node *rear=new node;
}Q;/*队列的头和尾*/
void get_link(int x,Queue *Q)
{
    node *tail=Q->rear;/*队列尾部*/

    while(x--)
    {
        node *q=new node;
        scanf("%d",&q->data);
        tail->nex=q;
        q->nex=NULL;
        tail=q;
        //Q->rear=q;
        /*或者这样记录尾部*/
    }
    Q->rear=tail;/*更新尾部*/
}
void out_link(node *head)
{
    node *q=head->nex;
    while(q)/*队列不为空*/
    {
        printf("%d\n",q->data);
        q=q->nex;
    }
    head->nex=q;
    Q.head=head;
}
int main()
{
    int x;
    scanf("%d",&x);
    node *q = new node; /*表头*/
    Q.head=Q.rear=q;/*表头没有数据,尾部有*/

    get_link(x,&Q);/*传进去头和尾*/

    out_link(Q.head);

    if(!Q.head->nex)/*判断是否空*/
        printf("1\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值