顺序队列–数组实现
#define Maxsize 50
typedef struct {
ElemType data[Maxsize];
int front rear;
}SqQueue;
循环队列
入队
bool EnQueue &Q,ElemType x)
{
if ((Q.rear+1)%Maxsize==Q.front) return flase;
Q.data[Q.rear]=x;
Q.rear=(Q.rear+1)%Maxsize;
return true;
}
2.出队
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;
}
链式队列
typedef struct{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front *rear;
}LinkQueue;
1.入队
void EnQueue (LinkQueue &Q,ElemType x)
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=x;
s->next=NUll;
Q.rear->next=s;
}
2.出队
bool DeQueue(LinkQueue &Q,ElemType &x)
{
if (Q.front==Q.rear) return false;
p=Q.front->next;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
}