队列的实现(基于链式结构)

本文围绕C语言中链式队列展开。介绍了链式队列基本概念,指出其采用链式存储结构,队大小可动态变化。还阐述了队列实现,包括结构体定义、初始化、入队和出队操作,最后给出完整测试用例及结果。

链式队列基本概念

队列是受限的线性表,使用链式存储结构的好处是,队的大小不固定可以动态变化。

队列的实现

结构体定义

typedef int ElemType;
typedef struct item{
    ElemType data;
    item* next;
};
typedef struct Queue{
    item* front;
    item* rear;   //首尾指针 
    int length;   //队列长度       
};

初始化操作

//初始化节点 
item* inititem(){
    item* i;
    i = (item*)malloc(sizeof(item));
    i->data = 0;
    i->next = NULL;
}

//初始化队列 
Queue* initQueue(){
    Queue* Q;
    Q = (Queue*)malloc(sizeof(Queue));
    Q->front = NULL;
    Q->rear = NULL;
    Q->length = 0;
    return Q;
}

入队操作

//入队操作 
void enQueue(Queue* Q,ElemType e){
    item* i = inititem();
    i->data = e;
    if(isEmpty(Q)){
        Q->front = i;
        Q->rear = i;
        Q->length++;
    }else{
        Q->rear->next = i;
        Q->rear = i;
        Q->length++;
    }
}

出队操作

//出队操作 
ElemType deQueue(Queue* Q){
    item* i;
    ElemType e;
    if(isEmpty(Q)){
        printf("队为空");
        return -1;
    }else{
        i = Q->front;
        Q->front = i->next;
        e = i->data;
        Q->length--;
        free(i);
        return e;
    }
}

完整的测试用例

/*此程序用以使用链式存储结构实现队列,主函数已作出测试。
若使用可将整数类型替换为指针来建立广义队列。
作者:姚帅   未经允许不得用作商业用途*/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef int ElemType;
typedef struct item{
    ElemType data;
    item* next;
};
typedef struct Queue{
    item* front;
    item* rear;   //首尾指针 
    int length;   //队列长度       
};

item* inititem();   //初始化化节点 
Queue* initQueue(); //初始化队列 
bool isEmpty(Queue* Q); //判断是否为空 
void enQueue(Queue* Q,ElemType e);  //入队操作 
ElemType deQueue(Queue* Q); //出队操作 
ElemType getHead(Queue* Q); //获得队首元素 
int getLong(Queue* Q);  //获得队长 
void printall(Queue* Q);    //输出队列中所有元素

int main(){
    Queue* Q = initQueue();
    enQueue(Q,11);
    enQueue(Q,22);
    enQueue(Q,33);
    printf("三个元素入队后:"); 
    printall(Q);
    deQueue(Q);
    printf("出队操作后:");
    printall(Q);
    getchar();
}

//初始化节点 
item* inititem(){
    item* i;
    i = (item*)malloc(sizeof(item));
    i->data = 0;
    i->next = NULL;
}

//初始化队列 
Queue* initQueue(){
    Queue* Q;
    Q = (Queue*)malloc(sizeof(Queue));
    Q->front = NULL;
    Q->rear = NULL;
    Q->length = 0;
    return Q;
}

//判断队列是否为空 
bool isEmpty(Queue* Q){
    if(Q->length == 0){
        return true;
    }else{
        return false;
    }
}

//入队操作 
void enQueue(Queue* Q,ElemType e){
    item* i = inititem();
    i->data = e;
    if(isEmpty(Q)){
        Q->front = i;
        Q->rear = i;
        Q->length++;
    }else{
        Q->rear->next = i;
        Q->rear = i;
        Q->length++;
    }
}

//出队操作 
ElemType deQueue(Queue* Q){
    item* i;
    ElemType e;
    if(isEmpty(Q)){
        printf("队为空");
        return -1;
    }else{
        i = Q->front;
        Q->front = i->next;
        e = i->data;
        Q->length--;
        free(i);
        return e;
    }
}

//获得队首元素 
ElemType getHead(Queue* Q){
    if(isEmpty(Q)){
        printf("队为空");
        return -1;
    }else{
        return Q->front->data;
    }
}

//获得队列长度 
int getLong(Queue* Q){
    return Q->length;
}

//输出队列 
void printall(Queue* Q){
    item* i = Q->front;
    while(i != NULL){
        printf("%d--",i->data);
        i = i->next;
    }
    printf("end\n");
}
        
    

测试结果
测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值