sqqueue.h
#include<stdio.h>
#include<iostream>
using namespace std;
typedef int datatype;
const int maxsize = 100;
typedef struct {
datatype data[maxsize];
int front, rear;
}sqqueue;
void init_sqqueue(sqqueue* sq);//初始化
int empty_sqqueue(sqqueue* sq);//判队空
int gethead_sqqueue(sqqueue* sq, datatype* x);//取队头
int en_sqqueue(sqqueue* sq, datatype x);//入队
int de_sqqueue(sqqueue* sq, datatype* x);//出队
sqqueue.cpp
#include"sqqueue.h"
void init_sqqueue(sqqueue* sq) {
sq->front = sq->rear = 0;//不能为-1
}
int empty_sqqueue(sqqueue* sq) {
if (sq->rear == sq->front) return 1;
else return 0;
}
int gethead_sqqueue(sqqueue* sq, datatype* x) {//队头元素值由参数返回
if (sq->rear == sq->front) { cout << "队空,无队头可取!\n"; return 0; }
*x = sq->data[(sq->front + 1) % maxsize];//头指针的下一个位置才是队头
return 1;
}
int en_sqqueue(sqqueue* sq, datatype x) {
if ((sq->rear + 1) % maxsize == sq->front)
{
cout << "队满,不能入队!\n"; return 0;//队满上溢
}
sq->rear = (sq->rear + 1) % maxsize;
sq->data[sq->rear] = x;
return 1;
}
int de_sqqueue(sqqueue* sq, datatype* x) {//队头元素值由参数返回
if (sq->rear == sq->front) { cout << "队空,不能出队!\n"; return 0; }//队空下溢
sq->front = (sq->front + 1) % maxsize;
*x = sq->data[sq->front];
return 1;
}