數組實現循環隊列


隊列的特點數據先進先出;

需要一個指向對頭,一個指向隊尾的標志,這里使用的數組,那就用數組的下標

定義一個指向順序隊列的結構:

#define   max   1024

typedef struct seqqueue{

  int  data[max]; //存放數據的順序對列,數組

  int  fornt; //指向對頭,數組下標,指向出對數據的下標,此時對頭指向一個空數據區域

  int  rear;  //指向隊尾,數組下標,表示入隊數據的下表標

} seqqueue;

 

一個空的順序隊列在尾部插入一個數據

 

 

 

 一個擁有8個數據空間的順序隊列插入7個數據,此時要想在插入一個成員,對列頭和隊列尾就又指向同一個位置;

不方便判斷隊列是滿還是空,插入7個數據隊列已經滿了,滿足公式 :rear+1 = front,這只是一個循環,伴隨着入隊

出對,對頭和對尾的位置是一直變化的,且不止一次;

 

 

 多次入隊出對后,如下圖情況所示:此時在入隊數據,數組下標會越界;

   (rear+1) % max  此時數組下標又開始從0 開始,完成一個循環

 

讓一個數組實現隊列循環,需要讓數組下標對數組的長度取余運算,這樣不會讓數組下標越界

(1) 開始時頭和尾都指向同一個地方表示一個空的隊列,rear % max  =  front  %  max

(2)隊列滿  (rear+1)% max  =  front  %  max

 seqqueue.c文件:

#include "seqqueue.h"

//創建空順序隊列
seqQueue *create_queue(void)
{
    seqQueue *qe = NULL;
    qe = (seqQueue*)malloc(sizeof(seqQueue));
    if(qe == NULL)
    {printf("create_seqqueue malloc is null \n");return NULL;}

    qe->head = qe->tail = MAX-1;
    return qe;
}

//尾部插入隊列
int in_queue(seqQueue *qe, u16 value)
{
    if(qe == NULL)
    {printf("in queue is null\n");return -1;}

    if(is_full_queue(qe) ==1 )
    {printf("in queue is full\n");return -1;}

    qe->tail = (qe->tail+1) % MAX;//數組下標加1,對MAX 取余的目的是實現數組下標的循環
    qe->data[qe->tail] = value;
    return 1;
}

//判斷對列滿?
int is_full_queue(seqQueue *qe)
{
    if(qe == NULL)
    {printf("is full queue is null\n");return -1;}

    return ((qe->tail+1)%MAX == (qe->head%MAX) ? 1 : 0);
}

//判隊列空?
int is_empty_queue(seqQueue *qe)
{
    if(qe == NULL)
    {printf("is empty queue is null\n");return -1;}

    return ( qe->head%MAX == qe->tail%MAX ? 1 : 0 );    
}

//顯示棧內容,從隊列頭部開始顯示
void show_queue(seqQueue *qe)
{
    if(qe == NULL)
    {printf("show queue is null\n");return ;}

    if(is_empty_queue(qe)==1)
    {printf("show queue is empty \n");return ;}

    u16 h = qe->head;
    while( h != qe->tail)
    {//為什么先 h++ 因為head前的一個元素無效
        h++;
        h = h%MAX;
        printf("%d\n",qe->data[h]);
    }
}

//頭部出隊列
int out_queue(seqQueue *qe)//出對
{
    if(qe == NULL)
    {printf("out queue is null\n");return -1;}

    if(is_empty_queue(qe)==1)
    {printf("out queue is empty \n");return -1;}
    
    qe->head++;
    qe->head = (qe->head)%MAX;
    printf("out queue data = %d\n",qe->data[qe->head]);//此處只是通過打印顯示,可以通過傳遞形參地址形式實現
    return 1;
}

seqqueue.h文件:

#ifndef __SEQQUEUE_H
#define __SEQQUEUE_H

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

#define MAX 10 //實際只能用 9 個空間存放數據

typedef int u16;

//插入數據,尾部入對,頭部出對
typedef struct seqqueue{
    u16 data[MAX];
    u16 head; //隊列頭,對應當前數據下標
    u16 tail; //隊列尾,對應當前數據下標
}seqQueue;

seqQueue *create_queue(void);
int in_queue(seqQueue *qe, u16 value);    //入對
int is_full_queue(seqQueue *qe);          //判滿
int is_empty_queue(seqQueue *qe);         //判空
void show_queue(seqQueue *qe);            //顯示棧內容
int out_queue(seqQueue *qe);//出對

#endif

main.c測試文件

#include "seqqueue.h"

int main(int argc, const char *argv[])
{
    seqQueue *s=NULL;
    s=create_queue();

    in_queue(s,1);
    in_queue(s,2);
    in_queue(s,3);
    in_queue(s,4);
    in_queue(s,5);
    in_queue(s,6);
    in_queue(s,7);
    in_queue(s,8);
    in_queue(s,9);
    in_queue(s,0);

    show_queue(s);

    printf("out queue kaishi \n");
    out_queue(s);

    out_queue(s);
    return 0;
}

測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM