考研数据结构第三章队列的顺序存储定义操作及代码实现
1.初始化队列
void InitQueue(SqQueue &Q){
Q.front=0;
Q.rear=0;
}
2.判断队空
int QueueEmpty(SqQueue Q){
if(Q.front==Q.rear)\
return 1;
else
return 0;
}
3.进队操作
int EnQueue(SqQueue &Q,ElemType e){
if((Q.rear+1)%MAXSIZE==Q.front)
return 0;
Q.rear = (Q.rear+1)%MAXSIZE;
Q.data[Q.rear]=e;
return 1;
}
4.出队操作
int DeQueue(SqQueue &Q,ElemType &e){
if(Q.front==Q.rear)
return 0;
e = Q.data[Q.front];
Q.front = (Q.front-1)%MASIZE
}
完整代码
#include<iostream>
using namespace std;
typedef int ElemType;
#define MAXSIZE 50
typedef struct SqQueue{
ElemType data[MAXSIZE];
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue &Q){
Q.front=0;
Q.rear=0;
}
int QueueEmpty(SqQueue Q){
if(Q.front==Q.rear)\
return 1;
else
return 0;
}
int EnQueue(SqQueue &Q,ElemType e){
if((Q.rear+1)%MAXSIZE==Q.front)
return 0;
Q.rear = (Q.rear+1)%MAXSIZE;
Q.data[Q.rear]=e;
return 1;
}
int DeQueue(SqQueue &Q,ElemType &e){
if(Q.front==Q.rear)
return 0;
e = Q.data[Q.front];
Q.front = (Q.front-1)%MASIZE
}
int main(){
return 0;
}
|