顺序队列的定义与方法


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

#define MaxSize 100
#define bool char
#define True 1
#define False 0

typedef int ElemType;

typedef struct SeqQueue {
    ElemType data[MaxSize];
    int front, rear; // 队头、队尾指针
} SQ;


void initSeqQueue(SQ *sq) {
    sq->front = 0;
    sq->rear = 0;
}

bool isEmptySeqQueue(SQ *sq) {
    if (sq->rear == sq->front) {
        return True;
    }
    return False;
}

bool isFullSeqQueue(SQ *sq) {
    if (sq->rear == MaxSize) {
        return True;
    }
    return False;
}

// 进入队列
void append(SQ *sq, ElemType x) {
    if (isFullSeqQueue(sq)) {
        printf("队列已满");
        exit(1);  //入队失败,退出函数运行
    }
    sq->data[sq->rear] = x;
    sq->rear++;
}

// 出队操作
ElemType shift(SQ *sq) {
    if (isEmptySeqQueue(sq)) {
        printf("队列已空,不能进行出队操作");
        exit(1);  //出队失败,退出函数运行
    }
    ElemType x = sq->data[sq->front];
    for (int i = sq->front; i < sq->rear; i++) {
        sq->data[i] = sq->data[i + 1];
    }
    sq->rear--;
    return x;
}
//
// 循环队列
//

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

#define MaxSize 100
#define bool char
#define True 1
#define False 0

typedef int ElemType;

typedef struct SeqQueue {
    ElemType data[MaxSize];
    int front, rear; // 队头、队尾指针
} SQ;


void initSeqQueue(SQ *sq) {
    sq->front = 0;
    sq->rear = 0;
}

bool isEmptySeqQueue(SQ *sq) {
    if (sq->rear == sq->front) {
        return True;
    }
    return False;
}

bool isFullSeqQueue(SQ *sq) {
    if ((sq->rear + 1) % MaxSize == sq->front) {
        return True;
    }
    return False;
}

// 进入队列
void append(SQ *sq, ElemType x) {
    if (isFullSeqQueue(sq)) {
        printf("队列已满");
        exit(1);  //入队失败,退出函数运行
    }
    sq->data[sq->rear] = x;
    sq->rear = (sq->rear + 1) % MaxSize;
}

// 出队操作
ElemType shift(SQ *sq) {
    if (isEmptySeqQueue(sq)) {
        printf("队列已空,不能进行出队操作");
        exit(1);  //出队失败,退出函数运行
    }
    ElemType x = sq->data[sq->front];
    sq->front = (sq->front + 1) % MaxSize;
    return x;
}

// 获取队列头元素
ElemType getQueue(SQ *sq) {
    return sq->data[sq->front];
}