【数据结构 - C语言】队列

【数据结构 - C语言】队列,第1张

队列

特点:先进先出

顺序队列

定义front为头指针,rear为尾指针,MAXSIZE为队列可拥有的最大元素

队空条件: front = rear

队满条件: (rear - front + 1) = MAXSIZE

代码参考
#include
#include

#define MAXSIZE 10

typedef int DataType;

typedef struct{
    DataType num[MAXSIZE];
    int front,rear;
}SeqQueue;

//初始化
void Init(SeqQueue *q){
    q->rear = 0;
    q->front = 0;
}

//状态判断
int GetStatus(const SeqQueue *q){
    if((q->rear - q->front) == MAXSIZE) return 1;
    else if(q->rear == q->front) return -1;
    return 0;
}

//移位 *** 作,数据规模大时很浪费,所以规模大时选用链式存储更合适
//当尾指针已经达到最大,但实际队并没有满时,进行移位 *** 作
void Move(SeqQueue *q){
    int i;
    for(i = 0;i < (q->rear - q->front + 1);i++){
        q->num[i] = q->num[q->front + i - 1];
    }

    q->front = 1;
    q->rear = i;
}

//入队
void Insert(SeqQueue *q,DataType val){
    if((q->rear - q->front + 1)  == MAXSIZE){
        printf("Queue is full!\n");
        return;
    }

    if(q->rear == MAXSIZE) Move(q);

    if(q->front == 0) q->front = 1; //让头指针指向第一个元素
    q->rear += 1;
    q->num[q->rear - 1] = val;
}

//出队
void Delete(SeqQueue *q){
    if(q->rear == q->front){
        printf("The Queue is empty!\n");
        return;
    }

    q->front += 1;
}

//打印
void Print(const SeqQueue *q){
    int i;
    unsigned char count = 0;
    for(i = q->front-1;i < q->rear;i++){
        printf("%d ",q->num[i]);
        if(++count == 5){
            count = 0;
            printf("\n");
        }
    }
}

int main(){

    SeqQueue *q;
    int i;

    q = (SeqQueue*)malloc(sizeof(SeqQueue));

    Init(q);

    printf("Insert:\n");
    for(i = 0;i < 10;i++){
        Insert(q,i);
    }
    Print(q);
    printf("\n");

    printf("Delete 1:\n");
    Delete(q);
    Print(q);
    printf("\n");

    printf("Delete 2:\n");
    Delete(q);
    Print(q);
    printf("\n");

    printf("Insert:");
    Insert(q,57);
    Print(q);
    printf("\n");

    printf("num[0]:%d\n",q->num[0]);

    return 0;
}
循环队列

将队列看成一个环状

队空: front = rear

队满: front = (rear + 1) % MAXSIZE

代码参考
#include
#include

#define MAXSIZE 10

typedef int DataType;

typedef struct{
    DataType num[MAXSIZE];
    int front,rear;
}cirQueue;

//初始化
void Init(cirQueue* q){
    q->front = 0;
    q->rear = 0;
}

//状态判断
int GetStatus(const cirQueue *q){
    if(q->front == (q->rear + 1) % MAXSIZE) return 1;
    else if(q->front == q->rear) return -1;
    return 0;
}

//入队
void Insert(cirQueue* q,DataType val){
    if(q->front == (q->rear + 1) % MAXSIZE){
        printf("The queue is full!\n");
        return ;
    }

    if(q->front == 0) q->front = 1;

    q->rear += 1;
    if(q->rear == MAXSIZE + 1) q->rear = 1;

    q->num[q->rear - 1] = val;
}

//出队
void Delete(cirQueue *q){
    if(q->front == q->rear){
        printf("The queue is empty!\n");
        return;
    }

    q->front += 1;
    if(q->front == MAXSIZE + 1) q->front = 1;
}

//打印
//打印需要分情况
void Print(const cirQueue *q){
    int i;
    unsigned char count = 0;
    
    if(q->front < q->rear){
        for(i = q->front - 1;i < q->rear;++i){
            printf("%d ",q->num[i]);
            if(++count == 5){
                count = 0;
                printf("\n");
            }
        }
    }
    else{
        for(i = q->front - 1;i < MAXSIZE;i++){
            printf("%d ",q->num[i]);
            if(++count == 5){
                count = 0;
                printf("\n");
            }
        }

        for(i = 0;i < q->rear;i++){
            printf("%d ",q->num[i]);
            if(++count == 5){
                count = 0;
                printf("\n");
            }
        }
        
    }
    
    
}

int main(){

    cirQueue *q;
    int i;

    q = (cirQueue*)malloc(sizeof(cirQueue));
    
    Init(q);

    printf("Insert:\n");
    for(i = 0;i < 10;i++){
        Insert(q,i);
    }
    Print(q);
    printf("\n");

    printf("Delete 1:\n");
    Delete(q);
    Print(q);
    printf("\n");

    printf("Delete 2:\n");
    Delete(q);
    Print(q);
    printf("\n");

    Insert(q,56);
    Print(q);
    printf("\n");

    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/676186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存