//-------------------队列的顺序存储结构--------------
#define MAXSIZE 100
typedef int QElemType;
typedef int Status;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
//-------------初始化-------------------
Status InitQueue(SqQueue Q)
{
if(!Q.base) exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
//-----------Q的元素个数----------------
int QueueLength(SqQueue Q)
{
return(Q.rear - Q.front + MAXSIZE)%MAXSIZE;
}
//---------入队----------------
Status EnQueue(SqQueue Q,QElemType e)
{
if((Q.rear+1)%MAXSIZE == Q.front)
return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAXSIZE;
return OK;
}
//------------出队-----------------
Status DeQueue(SqQueue Q,QElemType e)
{
if(Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front+1)%MAXSIZE;
return OK;
}
//-------------取队头元素--------------
SElemType GetHead(SqQueue Q)
{
if(Q.front != Q.rear)
return Q.base[Q.front];
}
//-----------队列的链式存储结构---------------
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//-------------------初始化-----------------------
Status InitQueue(LinkQueue Q)
{
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return OK;
}
//--------------入队-------------------------
Status EnQueue(LinkQueue Q, QElemType e)
{
QNode q;
q = (QNode *)malloc(sizeof(struct QNode));
q ->data = e;
q ->next = NULL;
Q.rear->next = q;
Q.rear = q;
}
//--------------出队----------------------
Status DeQueue(LinkQueue Q, QElemType e)
{
if(Q.front == Q.rear) return ERROR;
QNode p;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p) Q.rear = Q.front;
free(p);
return OK;
}
//----------------取队头元素------------------
QElemType GetHead(LinkQueue Q)
{
if(Q.rear != Q.front)
return Q.front->next->data;
}
08-07
128
08-07
105