链式队列

队列是一种先进先出线性表,队列是线性表的特化 也具有线性表的性质分为:顺序队列与链式队列 链式队列与线性表的单链表相似只不过链式队列只 允许从头部进行删除、尾部进行插入.需要为链式队列 创建一个头结点包括两个指针,指向队头的指针(front) 与指向队尾的指针(rear).当两个指针相等时队列为空

这里写图片描述

lqueue.h

//链式队列
typedef int ElemType;  //队列的数据类型  

typedef struct node{  
    ElemType data; //队列的数据类型  
    struct node *next; //指向下一个结点  
}QueNode,*QuePtr;  

typedef struct{  
    QuePtr front; //指向队头的指针  
    QuePtr rear; //指向队尾的指针  
}LinkQueue;  



//函数声明
void InitQueue(LinkQueue *q);  //链式队列初始化 
bool IsEmpty(LinkQueue *q);  //判队空 
void printQueue(LinkQueue *q);  //打印队列 
void EnQueue(LinkQueue *q,ElemType e);  //入队操作
ElemType DeQueue(LinkQueue *q);  //出队操作 


lqueue.cpp

链式队列(带头结点)

#include "lqueue.h" 
#include <stdlib.h>  //含有 malloc 函数的头文件 
#include <stdio.h>


int main()
{
    int n=1;
    LinkQueue q;

    InitQueue(&q);
    printQueue(&q);
     while(n!=0)
    {
         printf("\t\t/\n");
         printf("\t\tinput your choice \n");
         printf("\t\t1.En Queue \n");
         printf("\t\t2.De Queue \n");
         printf("\t\t/\n");
         scanf("%d",&n);
        switch(n)
        {
            int m,p;
            case 1: printf("1.input the elem you want to push\n");
                    scanf("%d",&m);
                    EnQueue(&q,m); 
                    printQueue(&q);
                break;
            case 2: p=DeQueue(&q);
                    printf(" 元素 %d 出队列! \n",p);
                    printQueue(&q);
                    break;
            default:
                break;
        }
    }
    return 0;
}


void InitQueue(LinkQueue *q)  //链式队列初始化 
{
     q->front=q->rear=(QueNode *)malloc(sizeof(QueNode));
        if(q->front==NULL) printf("分配空间失败!\n");
        else{
                 q->front->next=NULL;   //初始为空 
        }
}

bool IsEmpty(LinkQueue *q) //判队空 
{
    if(q->front==q->rear) return 1;
    else return 0;
} 


void EnQueue(LinkQueue *q,ElemType e)//入队操作(尾插法)
{
    QueNode *p=(QueNode *)malloc(sizeof(QueNode));
    if(p==NULL) printf("分配空间失败!\n");
    else{
    p->data=e;
    p->next=NULL;
    q->rear->next=p;
    q->rear=p;
    }

}


void printQueue(LinkQueue *q)  //打印队列 
{
    QueNode *p,*h;
    printf("打印队列如下!\n");
    if(IsEmpty(q)==1)   printf("队列为空!\n");
    else    {
            h=q->front;
    for(p=h->next;p!=NULL;p=p->next)
    {
        printf("%d ",p->data);
    }
    }
    printf("\n");
}

ElemType DeQueue(LinkQueue *q) //出队操作
{
    QueNode *p;
    int f;
    //判断队空
    if(IsEmpty(q)==1){
        printf("队列为空!\n"); 
        return 0;
    }   
    else{
//若出队为最后一个结点 ,将尾指针指向头结点防止其悬空 
        if(q->front->next==q->rear) q->rear=q->front; 
        p=q->front->next;
        f=p->data;
        q->front->next=q->front->next->next;
        free(p);
        return f;
    } 
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值