链队列的基本操作
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR -1
typedef int QElemType;
typedef int Status;
typedef struct QNode
{
QElemType data;
struct QNode * next;
} QNode;
typedef QNode* QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
Status InitQueue ( LinkQueue * Q)
{
( * Q) . front = ( * Q) . rear = ( QueuePtr) malloc ( sizeof ( QNode) ) ;
if ( ! ( * Q) . front)
exit ( 0 ) ;
( * Q) . front-> next = NULL ;
return OK;
}
void ClearQueue ( LinkQueue * Q)
{
( * Q) . rear = ( * Q) . front-> next;
while ( ( * Q) . rear)
{
( * Q) . front-> next = ( * Q) . rear-> next;
free ( ( * Q) . rear) ;
( * Q) . rear = ( * Q) . front-> next;
}
( * Q) . rear = ( * Q) . front;
}
void DestroyQueue ( LinkQueue * Q)
{
while ( ( * Q) . front)
{
( * Q) . rear = ( * Q) . front-> next;
free ( ( * Q) . front) ;
( * Q) . front = ( * Q) . rear;
}
}
Status QueueEmpty ( LinkQueue Q)
{
if ( Q. front== Q. rear)
return true;
else
{
return false;
}
}
Status QueueLength ( LinkQueue Q)
{
int count = 0 ;
QueuePtr p = Q. front;
while ( p!= Q. rear)
{
count++ ;
p = p-> next;
}
return count;
}
Status GetHead_Queue ( LinkQueue Q, QElemType * e)
{
QueuePtr p;
if ( Q. front== Q. rear)
{
return ERROR;
}
p = Q. front-> next;
* e = p-> data;
return OK;
}
Status In_Queue ( LinkQueue * Q, QElemType e)
{
QueuePtr p;
p = ( QueuePtr) malloc ( sizeof ( QNode) ) ;
if ( ! p)
exit ( 0 ) ;
p-> data = e;
p-> next = NULL ;
( * Q) . rear-> next = p;
( * Q) . rear = p;
return OK;
}
Status Out_Queue ( LinkQueue * Q, QElemType * e)
{
QueuePtr p;
if ( ( * Q) . front== ( * Q) . rear)
return ERROR;
p = ( * Q) . front-> next;
* e = p-> data;
( * Q) . front-> next = p-> next;
if ( ( * Q) . rear== p)
( * Q) . rear = ( * Q) . front;
free ( p) ;
return OK;
}
void QueueTraverse ( LinkQueue Q, void ( Visit) ( QElemType) )
{
QueuePtr p;
p = Q. front-> next;
while ( p)
{
Visit ( p-> data) ;
p = p-> next;
}
} ```