链队

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int type;
typedef struct Node
{
    type info;
    struct Node*next;
}node;
//队列声明,保存头指针,尾指针
typedef struct
{
    node *front;
    node *rear;
}queue;
//初始化一个空队列并返回其指针
queue* init()
{
    queue *head=(queue*)malloc(sizeof(queue));
    head->front=head->rear=NULL;
}
//打印一个队列
void display(queue*head)
{
    node *p=head->front;
    while(p){
        printf("%5d",p->info);
        p=p->next;
    }
    printf("\n");
}
//入队
void insert(queue*head)
{
    node*pre;
    pre=(node*)malloc(sizeof(node));
    pre->next=NULL;
    scanf("%d",&pre->info);
    if(!head->front)//如果是空队列,则front和rear指针指向该节点
        head->front=head->rear=pre;
    else{//否则,在尾部插入
        head->rear->next=pre;
        head->rear=pre;
    }
}
//销毁一个队列
void destory(queue*head)
{
    node*pre=head->front,*p;
    while(pre)
    {
        p=pre->next;
        free(pre);
        pre=p;
    }
    free(head);
}
//出队
void dele(queue *head)
{
    node*p;
    if(!head->front)
        printf("NULL\n");
    else{//非空队列
       p=head->front;
       head->front=p->next;
       free(p);
       if(!head->front)//如果是删除了最后一个节点,队列置空
        head->rear=NULL;
    }
}
//建立一个长度为n的队列
void create(queue *head,int n)
{
    int i;
    for(i=1;i<=n;i++)
        insert(head);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值