//循环顺序队列的基本操作 #include <iostream> #include <string.h> using namespace std; #define MAXLEN 8 #define datatype char #define Status int typedef struct{ datatype s[MAXLEN]; int front; //记录队首的指针 int rear; //记录队尾的指针 }SeqQueue; //初始化 Status InitSeqQueue(SeqQueue &sq) { sq.front=-1; sq.rear=-1; return 1; } //判空 Status IsEmpty(SeqQueue sq) { //若空,返回1;否则返回0 if(sq.front==sq.rear) return 1; else return 0; } //判满 Status IsFull(SeqQueue sq) { //若队满,返回1; if(sq.front==(sq.rear+1)%MAXLEN) return 1; else return 0; } //入栈 Status InQueue(SeqQueue &sq,datatype x) { //判断队列满 if(sq.front==(sq.rear+1)%MAXLEN) return 0; sq.rear++; sq.s[sq.rear]=x; return 1; } //出栈 Status OutQueue(SeqQueue &sq,datatype &x) { //若对空,返回0;出队成功返回1; //判断对空 if(sq.front==sq.rear) return 0; //出队 sq.front++; x=sq.s[sq.front]; return 1; } //显示栈元素 Status ShowQueue(SeqQueue &sq) { if(sq.front==sq.rear) return 0; int i=sq.front; while(i!=sq.rear) { i++; cout<<sq.s[i]<<" "; } cout<<endl; return 1; } //读栈首元素 Status ReadQueue(SeqQueue &sq,datatype &x) { if(IsEmpty(sq)) return 0; //若队列空,返回0 sq.front++; x=sq.s[sq.front]; return 1; } //求队列长度 int main() { cout<<"队列初始化"<<endl; SeqQueue SQ; InitSeqQueue(SQ); cout<<"入队"<<endl; InQueue(SQ,‘a‘); InQueue(SQ,‘b‘); InQueue(SQ,‘c‘); cout<<"显示队列元素:"<<endl; ShowQueue(SQ); datatype x; cout<<"出队:"<<endl; OutQueue(SQ,x); cout<<"显示队列元素:"<<endl; ShowQueue(SQ); cout<<"读取队首元素:"<<endl; ReadQueue(SQ,x); cout<<x<<endl; return 0; } ---------------------------------------------------------- 输出结果: 队列初始化 入队 显示队列元素: a b c 出队: 显示队列元素: b c 读取队首元素: b Press any key to continue . . .
鲜少伟
2016-4-18
时间: 2024-10-08 10:13:21