天天看点

Linux C++ 循环队列

#include <stdio.h>
#include <string.h>


#define QUEUE_LEN 16
#define ARRAR_SIZE (QUEUE_LEN+1)

typedef struct student
{
    int math;
    int English;
    char name[32];
}student;


#define QUEUE_TYPE student





static student studentTable[ARRAR_SIZE];   //定义结构体数组
static unsigned int front;   //指向队列头部
static unsigned int tail;    //指向队列尾部


bool IsQueueEmpty(void)
{
    return (front == tail);
}

bool IsQueueFull()
{
    return ((tail + 1) % ARRAR_SIZE == front);
}

bool queueInsert(QUEUE_TYPE value)  //插入
{
    if(IsQueueFull())
            return false;
    studentTable[tail]=value;
    tail=(tail+1) % ARRAR_SIZE;  //尾部

    return true;
}

bool queueDelete()      //删除
{
    if(IsQueueEmpty())
        return false;

    front=(front+1)%ARRAR_SIZE;   //头部
     return true;
}


int main(int argc, char *argv[])
{
    student stu;
    stu.math=99;
    stu.English=98;
    char name[32]="salman1";

    memcpy(stu.name,name,sizeof(name));
    queueInsert(stu);
    stu.math = 61;
    stu.English = 60;
    memset(name,0,sizeof(name));
    sprintf(name,"xiaohong",sizeof(name));
    memcpy(stu.name,name,sizeof(name));
    queueInsert(stu);
    printf("front = %d,tail = %d,name = %s\n",front,tail,studentTable[front].name);
    queueDelete();
    printf("front = %d,tail = %d,name = %s\n",front,tail,studentTable[front].name);
    return 0;
    


}