2.3 链队列

2.3 链队列_哔哩哔哩_bilibili 

 

//链队列的实现(带头结点) C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//链队列结点定义
typedef struct LinkNode{
    int data;
    struct LinkNode *next;
}LinkNode;
//链队列定义
typedef struct{
    LinkNode *front, *rear; //头指针,尾指针
}LinkQueue;

//初始化队列
void InitQueue(LinkQueue &Q){
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode)); 
    Q.front->next=NULL;
}

//判断队列是否为空
bool QueueEmpty(LinkQueue Q){
    return Q.front==Q.rear;
}

//队尾入队
void EnQueue(LinkQueue &Q, int e){
    LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
}

//队头出队
void DeQueue(LinkQueue &Q, int &e){
    if(QueueEmpty(Q)) return;
    LinkNode *p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(Q.rear==p) Q.rear=Q.front;
    free(p);
}

//读队头
int GetHead(LinkQueue Q){
    if(!QueueEmpty(Q))
        return Q.front->next->data;
}
int main(){
    LinkQueue Q; //声明一个队列
    InitQueue(Q);
    int n=3; int a[]={5,2,0};
    for(int i=0;i<n;i++){
        EnQueue(Q,a[i]);
    }
    for(int i=0,e;i<n;i++){
        DeQueue(Q,e);
        printf("%d ",e);
    }
    return 0;
}

 

//链队列的实现(不带头结点) C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//链队列结点定义
typedef struct LinkNode{
    int data;
    struct LinkNode *next;
}LinkNode;
//链队列定义
typedef struct{
    LinkNode *front, *rear; //头指针,尾指针
}LinkQueue;

//初始化队列
void InitQueue(LinkQueue &Q){
    Q.front=NULL;
    Q.rear=NULL;
}

//判断队列是否为空
bool QueueEmpty(LinkQueue Q){
    return Q.front==NULL;
}

//队尾入队
void EnQueue(LinkQueue &Q, int e){
    LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));  
    p->data=e;   
    p->next=NULL;
    if(Q.front==NULL){ //处理第1个元素入队
        Q.front=p;    
        Q.rear=p; 
    }
    else{
        Q.rear->next=p;
        Q.rear=p;
    }
}

//队头出队
void DeQueue(LinkQueue &Q, int &e){
    if(QueueEmpty(Q)) return;
    LinkNode *p=Q.front;
    e=p->data;
    if(Q.front==Q.rear) //只有1个结点
        Q.front=Q.rear=NULL;
    else
        Q.front=Q.front->next;
    free(p);
}

//读队头
int GetHead(LinkQueue Q){
    if(!QueueEmpty(Q))
        return Q.front->data;
}
int main(){
    LinkQueue Q; //声明一个队列
    InitQueue(Q);
    int n=3; int a[]={5,2,0};
    for(int i=0;i<n;i++){
        EnQueue(Q,a[i]);
    }
    for(int i=0,e;i<n;i++){
        DeQueue(Q,e);
        printf("%d ",e);
    }
    return 0;
}

 

//循环链队列的实现 C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//链队列结点定义
typedef struct LinkNode{
    int data;
    struct LinkNode *next;
}LinkNode;
//链队列定义
typedef struct{
    LinkNode *front, *rear; //头指针,尾指针
}LinkQueue;

//初始化队列
void InitQueue(LinkQueue &Q){
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode)); //空闲结点
    Q.rear->next=Q.front;
}

//判断队列是否为空
bool QueueEmpty(LinkQueue Q){
    return Q.front==Q.rear;
}

//判断队列是否为满
bool QueueFull(LinkQueue Q){
    return Q.front==Q.rear->next;
}

//队尾入队
void EnQueue(LinkQueue &Q, int e){
    if(QueueFull(Q)){ //若队满
        LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
        p->next=Q.rear->next;
        Q.rear->next=p;
    }
    Q.rear->data=e;
    Q.rear=Q.rear->next;
}

//队头出队
void DeQueue(LinkQueue &Q, int &e){
    if(QueueEmpty(Q)) return;
    e=Q.front->data;
    Q.front=Q.front->next;
}

//读队头
int GetHead(LinkQueue Q){
    if(!QueueEmpty(Q))
        return Q.front->data;
}
int main(){
    LinkQueue Q; //声明一个队列
    InitQueue(Q);
    int n=3; int a[]={5,2,6};
    for(int i=0;i<n;i++){
        EnQueue(Q,a[i]);
    }
    int e;
    DeQueue(Q,e);
    printf("%d ",e); //5
    printf("%d ",GetHead(Q)); //2
    printf("%d ",Q.rear->next->data); //5
    EnQueue(Q,7);
    printf("%d ",Q.rear->data); //5
    return 0;
}

 

.

posted @ 2026-09-13 07:12  董晓  阅读(11)  评论(0)    收藏  举报