赞
踩
为区分队空队满有3种处理方式:
①牺牲一个单元
②增设表示元素个数的数据成员
③增设tag数据成员
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int front, rear;
}SqQueue;
void InitQueue(SqQueue &Q) {
Q.rear = Q.front = 0;
}
bool isEmpty(SqQueue Q) {
if(Q.rear = Q.front)
return true;
else
return false;
}
bool EnQueue(SqQueue &Q, ElemType x) {
if((Q.rear + 1) % MaxSize == Q.front)//队满报错
return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if(Q.rear = Q.front)//队空报错
return false;
x = Q.data[Q.front];
Q.front= (Q.front+ 1) % MaxSize;
return true;
}
bool GetHead(SqQueue &S, ElemType &x) {
if(Q.rear = Q.front)//队空报错
return false;
x = Q.data[Q.front];
return true;
}
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int front, rear;
int size;
}SqQueue;
void InitQueue(SqQueue &Q) {
Q.rear = Q.front = 0;
Q.size = 0;
}
bool isEmpty(SqQueue Q) {
if(Q.size == 0)
return true;
else
return false;
}
bool EnQueue(SqQueue &Q, ElemType x) {
if(Q.size == MaxSize)//队满报错
return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if(Q.size == 0)//队空报错
return false;
x = Q.data[Q.front];
Q.front= (Q.front+ 1) % MaxSize;
return true;
}
bool GetHead(SqQueue &S, ElemType &x) {
if(Q.size == 0)//队空报错
return false;
x = Q.data[Q.front];
return true;
}
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int front, rear;
int tag;
}SqQueue;
void InitQueue(SqQueue &Q) {
Q.rear = Q.front = 0;
Q.tag = 0;
}
bool isEmpty(SqQueue Q) {
if(Q.rear = Q.front && Q.tag == 0)
return true;
else
return false;
}
bool EnQueue(SqQueue &Q, ElemType x) {
if(Q.rear = Q.front && Q.tag == 1)//队满报错
return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
tag = 1;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if(Q.rear = Q.front && Q.tag == 0)//队空报错
return false;
x = Q.data[Q.front];
Q.front= (Q.front+ 1) % MaxSize;
tag = 1;
return true;
}
bool GetHead(SqQueue &S, ElemType &x) {
if(Q.rear = Q.front && Q.tag == 0)//队空报错
return false;
x = Q.data[Q.front];
return true;
}
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int front, rear;
}SqQueue;
void InitQueue(SqQueue &Q) {
Q.front = 0;
Q.rear = MaxSize - 1;
}
bool isEmpty(SqQueue Q) {
if((Q.rear + 1) % MaxSize == Q.front)
return true;
else
return false;
}
bool EnQueue(SqQueue &Q, ElemType x) {
if((Q.rear + 2) % MaxSize == Q.front)//队满报错
return false;
Q.rear = (Q.rear + 1) % MaxSize;
Q.data[Q.rear] = x;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if((Q.rear + 1) % MaxSize == Q.front)//队空报错
return false;
x = Q.data[Q.front];
Q.front= (Q.front+ 1) % MaxSize;
return true;
}
bool GetHead(SqQueue &S, ElemType &x) {
if((Q.rear + 1) % MaxSize == Q.front)//队空报错
return false;
x = Q.data[Q.front];
return true;
}
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int front, rear;
int size;
}SqQueue;
void InitQueue(SqQueue &Q) {
Q.front = 0;
Q.rear = MaxSize - 1;
Q.size = 0;
}
bool isEmpty(SqQueue Q) {
if(Q.size == 0)
return true;
else
return false;
}
bool EnQueue(SqQueue &Q, ElemType x) {
if(Q.size == MaxSize)//队满报错
return false;
Q.rear = (Q.rear + 1) % MaxSize;
Q.data[Q.rear] = x;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if(Q.size == 0)//队空报错
return false;
x = Q.data[Q.front];
Q.front= (Q.front+ 1) % MaxSize;
return true;
}
bool GetHead(SqQueue &S, ElemType &x) {
if(Q.size == 0)//队空报错
return false;
x = Q.data[Q.front];
return true;
}
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int front, rear;
int tag;
}SqQueue;
void InitQueue(SqQueue &Q) {
Q.front = 0;
Q.rear = MaxSize - 1;
Q.tag = 0;
}
bool isEmpty(SqQueue Q) {
if((Q.rear + 1) % MaxSize == Q.front && Q.tag == 0)
return true;
else
return false;
}
bool EnQueue(SqQueue &Q, ElemType x) {
if((Q.rear + 1) % MaxSize == Q.front && Q.tag == 1)//队满报错
return false;
Q.rear = (Q.rear + 1) % MaxSize;
Q.data[Q.rear] = x;
tag = 1;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if((Q.rear + 1) % MaxSize == Q.front && Q.tag == 0)//队空报错
return false;
x = Q.data[Q.front];
Q.front= (Q.front+ 1) % MaxSize;
tag = 1;
return true;
}
bool GetHead(SqQueue &S, ElemType &x) {
if((Q.rear + 1) % MaxSize == Q.front && Q.tag == 0)//队空报错
return false;
x = Q.data[Q.front];
return true;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。