7、队列

1、链队

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

#define ElemType int

typedef struct QueueNode{
    ElemType data;
    struct QueueNode* next;
}QueueNode; 

typedef struct LinkQueue{
    QueueNode* front;//队头
    QueueNode* tail;//队尾 
}LinkQueue;

QueueNode* malloc_QueueNode(ElemType e){
    QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    assert(node != NULL);
    node->data = e;
    node->next = NULL;
}

void initQueue(LinkQueue* queue){
    //构造虚拟头结点
    QueueNode* p = malloc_QueueNode(-1);
    queue->front = queue->tail = p;
}

//入队
void enQueue(LinkQueue* queue,ElemType e){
    QueueNode* p = malloc_QueueNode(e);
    queue->tail->next = p;
    
    //修改队尾 
    queue->tail = p; 
}

//出队
int deQueue(LinkQueue* queue){
    if(queue->front == queue->tail){
        printf("当前链队为空,不可删除.\n");
        return;
    }
    QueueNode* p = queue->front->next;
    queue->front->next = p->next;
    if(p == queue->tail){
        queue->tail = queue->front;
    }
    free(p);
}

//得到队头元素
void getHead(LinkQueue* queue,ElemType* e){
    if(queue->front == queue->tail){
        printf("当前链队为空.\n");
        return;
    }
    QueueNode* p = queue->front->next;
    *e = p->data;
}

//长度
int length(LinkQueue* queue){
    int len = 0;
    QueueNode* p = queue->front->next;
    while(p != NULL){
        p = p->next;
        len++;
    }
    return len;
}

//清空链队
void clear(LinkQueue* queue){
    QueueNode* p = queue->front->next;
    while(p != NULL){
        queue->front->next = p->next;
        free(p);
        p = queue->front->next;
    }
    queue->tail = queue->front;
}

//销毁
void destroy(LinkQueue* queue){
    clear(queue);
    free(queue->front);
    queue->front = queue->tail = NULL;
}
//打印链队
void showQueue(LinkQueue* queue){
    QueueNode* p = queue->front->next;
    while(p != NULL){
        printf("%d->",p->data);
        p = p->next;
    }
    printf("NULL.\n");
}
int main(){
    LinkQueue queue;
    initQueue(&queue);
    int i = 1;
    for(i;i <= 5;i++){
        enQueue(&queue,i); 
    }
    showQueue(&queue);
    deQueue(&queue);
    showQueue(&queue);

    return 0;
}

2、顺序队

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

#define ElemType int

#define MAXSIZE 8
//顺序队列 
typedef struct SeqQueue{
    ElemType* base;
    int front;//队头 
    int rear;//队尾 
}SeqQueue;

//初始化顺序队列 
void initSeqQueue(SeqQueue* Q){
    Q->base = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
    assert(Q->base != NULL);
    Q->front = 0;
    Q->rear = 0;
}

//入队
void enQueue(SeqQueue* Q,ElemType e){
    if(Q->rear >= MAXSIZE){
        printf("队列空间已满,%d 不可入队.\n",e);
        return;
    }
    Q->base[Q->rear++] = e;
} 

//出队
void DeQueue(SeqQueue* Q){
    if(Q->front == Q->rear){
        printf("队列为空,不可出队.\n");
        return;
    }
    Q->front++;
}
//打印队列
void showSeqQueue(SeqQueue* Q){
    int i = Q->front;
    for(i;i < Q->rear;i++){
        printf("%d ",Q->base[i]);
    }
    printf("\n");
} 

void getHead(SeqQueue* Q,ElemType* e){
    if(Q->front == Q->rear){
        printf("队列为空,无法获取队头元素.\n");
        return;
    }
    *e = Q->base[Q->front];
}

int length(SeqQueue* Q){
    return Q->rear - Q->front;
}

void clear(SeqQueue* Q){
    Q->front = Q->rear = 0;
}

void destroy(SeqQueue* Q){
    free(Q->base);
    Q->base = NULL;
}
int main(){
    SeqQueue Q;
    initSeqQueue(&Q);
    
    int i = 1;
    for(i;i <= 10;i++){
        enQueue(&Q,i);
    }
    showSeqQueue(&Q);
    DeQueue(&Q);
    showSeqQueue(&Q);

    return 0;
} 

3、循环队列

 

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

#define ElemType int

#define MAXSIZE 8
//顺序队列 
typedef struct SeqQueue{
    ElemType* base;
    int front;//队头 
    int rear;//队尾 
}SeqQueue;

//初始化顺序队列 
void initSeqQueue(SeqQueue* Q){
    Q->base = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
    assert(Q->base != NULL);
    Q->front = 0;
    Q->rear = 0;
}

//入队
void enQueue(SeqQueue* Q,ElemType e){
    if(((Q->rear + 1) % MAXSIZE) == Q->front){
        printf("队列空间已满,%d 不可入队.\n",e);
        return;
    }
    Q->base[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MAXSIZE;
} 

//出队
void DeQueue(SeqQueue* Q){
    if(Q->front == Q->rear){
        printf("队列为空,不可出队.\n");
        return;
    }
    Q->front = (Q->front + 1) % MAXSIZE;
}
//打印队列
void showSeqQueue(SeqQueue* Q){
    int i = Q->front;
    for(i;i != Q->rear;i = (i + 1) % MAXSIZE){
        printf("%d ",Q->base[i]);
    }
    printf("\n");
} 

void getHead(SeqQueue* Q,ElemType* e){
    if(Q->front == Q->rear){
        printf("队列为空,无法获取队头元素.\n");
        return;
    }
    *e = Q->base[Q->front];
}

int length(SeqQueue* Q){
    return Q->rear - Q->front;
}

void clear(SeqQueue* Q){
    Q->front = Q->rear = 0;
}

void destroy(SeqQueue* Q){
    free(Q->base);
    Q->base = NULL;
}
int main(){
    SeqQueue Q;
    initSeqQueue(&Q);
    
    int i = 1;
    for(i;i <= 10;i++){
        enQueue(&Q,i);
    }
    showSeqQueue(&Q);
    DeQueue(&Q);
    showSeqQueue(&Q);

    return 0;
} 

注:上诉使用空一个空间来方便判断空和判满,如果不想浪费这一个空间,可通过设置一个标志位来用于判空和判满。

 

posted @ 2024-09-16 15:48  颜欢兮  阅读(2)  评论(0编辑  收藏  举报