【数据结构】队列-链队列的基本定义

本文详细介绍了链队列的基本定义,包括其逻辑结构,并列举了链队列的主要操作,如初始化、清空、判断队列是否为空、求队列长度、入队、出队、取队头元素和历遍操作。同时,提供了完整的调试代码供参考。

链队列的基本定义 :

逻辑结构定义:

typedef struct QNode{
    QElemType data;
    struct QNode *next;
    QNode (QElemType Data=inf,struct QNode *Next=NULL){
        data=Data;next=Next;
    }
}QNode , *QueuePtr;
typedef struct {
    QueuePtr Front,Rear;
}LinkQueue;

链队列的基本操作:

1、初始化一个队列

2、清空一个队列

3、判断一个队列是否为空

4、求队列的长度

5、入队列操作

6、出队列操作

7、取队头元素

8、历遍操作


1、初始化一个队列

void InitQueue(LinkQueue &Q){
    Q.Front=new QNode ;
    Q.Rear=Q.Front;
    Q.Front->next=NULL;
}

2、清空一个队列

void ClearQueue(LinkQueue &Q){
    QueuePtr p,s;
    p=Q.Front->next;
    Q.Front->next=NULL;
    while(p){
        s=p;p=p->next;
        delete s;
    }
    Q.Rear=Q.Front;
}

3、判断一个队列是否为空

bool QueueEmpty(LinkQueue Q){
    return Q.Front==Q.Rear?true:false;
}

4、求队列的长度

int QueueLength(LinkQueue Q){
    int len=0;
    QueuePtr p=Q.Front->next;
    while(p){
        len++;p=p->next;
    }
    return len;
}

5、入队列操作

void EnQueue(LinkQueue &Q,QElemType e){
    QueuePtr s=new QNode(e,NULL);
    Q.Rear->next=s;
    Q.Rear=s;
}

6、出队列操作

void DeQueue(LinkQueue &Q,QElemType &e){
    if(QueueEmpty(Q)){
        puts("error !!! queue is empty !!!");return ;
    }
    QueuePtr s=Q.Front->next;
    Q.Front->next=s->next;
    e=s->data;
    if(Q.Rear==s){
        Q.Rear=Q.Front;
    }
    delete s;
}

7、取队头元素

void GetHead(LinkQueue Q,QElemType &e){
    if(QueueEmpty(Q)){
        puts("error !!! queue is empty !!!");return ;
    }
    e=Q.Front->next->data;
}

8、历遍操作

void QueueTraverse(LinkQueue Q){
    for(QueuePtr p=Q.Front->next; p ;p=p->next){
        printf("%d%c",p->data,p==Q.Rear?'\n':' ');
    }
}

完整的调试代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define QueueSize 100
#define QElemType int
#define inf 0x3f3f3f3f
using namespace std;
typedef struct QNode{
    QElemType data;
    struct QNode *next;
    QNode (QElemType Data=inf,struct QNode *Next=NULL){
        data=Data;next=Next;
    }
}QNode , *QueuePtr;
typedef struct {
    QueuePtr Front,Rear;
}LinkQueue;
void InitQueue(LinkQueue &Q){
    Q.Front=new QNode ;
    Q.Rear=Q.Front;
    Q.Front->next=NULL;
}
void ClearQueue(LinkQueue &Q){
    QueuePtr p,s;
    p=Q.Front->next;
    Q.Front->next=NULL;
    while(p){
        s=p;p=p->next;
        delete s;
    }
    Q.Rear=Q.Front;
}
bool QueueEmpty(LinkQueue Q){
    return Q.Front==Q.Rear?true:false;
}
int QueueLength(LinkQueue Q){
    int len=0;
    QueuePtr p=Q.Front->next;
    while(p){
        len++;p=p->next;
    }
    return len;
}
void EnQueue(LinkQueue &Q,QElemType e){
    QueuePtr s=new QNode(e,NULL);
    Q.Rear->next=s;
    Q.Rear=s;
}
void DeQueue(LinkQueue &Q,QElemType &e){
    if(QueueEmpty(Q)){
        puts("error !!! queue is empty !!!");return ;
    }
    QueuePtr s=Q.Front->next;
    Q.Front->next=s->next;
    e=s->data;
    if(Q.Rear==s){
        Q.Rear=Q.Front;
    }
    delete s;
}
void GetHead(LinkQueue Q,QElemType &e){
    if(QueueEmpty(Q)){
        puts("error !!! queue is empty !!!");return ;
    }
    e=Q.Front->next->data;
}
void QueueTraverse(LinkQueue Q){
    for(QueuePtr p=Q.Front->next; p ;p=p->next){
        printf("%d%c",p->data,p==Q.Rear?'\n':' ');
    }
}
int main(){
    LinkQueue Q;
    InitQueue(Q);
    QElemType tmp,e;
    if(QueueEmpty(Q))
        for(int i=0;i<10;i++){
            EnQueue(Q,i);
        }
    QueueTraverse(Q);
    printf("Len : %d\n",QueueLength(Q));

    for(int i=0;i<9;i++){
        DeQueue(Q,e);
        printf("%d\n",e);
    }
    GetHead(Q,e);
    printf("Head :  %d\n",e);
    ClearQueue(Q);
    printf("%d\n",QueueLength(Q));
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值