//头文件
#ifndef LINKQUENE_H_INCLUDED
#define LINKQUENE_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElemType ;
typedef struct QNode
{
ElemType data ;
struct QNode* next ;
} QNode ;
typedef struct
{
QNode* front ;
QNode* rear ;
}LinkQuene ;
int InitQuene( LinkQuene* Q ) ;
int DestroyQuene( LinkQuene* Q ) ;
int IsEmptyQuene( LinkQuene* Q ) ;
int ClearQuene( LinkQuene* Q ) ;
int GetHeadQuene( LinkQuene* Q , ElemType* e ) ;
int GetLengthQuene( LinkQuene* Q ) ;
int InsertQuene( LinkQuene* Q , ElemType e ) ;
int DeleteQuene( LinkQuene* Q , ElemType* e ) ;
int TraverseQuene( LinkQuene* Q ) ;
#endif // LINKQUENE_H_INCLUDED
// 函数的实现
#include "linkquene.h"
int InitQuene( LinkQuene* Q )
{
Q->front = ( QNode* )malloc( sizeof( QNode ) ) ;
if( !Q->front )
{
printf( "OVERFLOW!\n") ;
exit( 1 ) ;
}
Q->front->next = NULL ;
Q->rear = Q->front ;
return 0 ;
}
int DestroyQuene( LinkQuene* Q )
{
while( Q->front )
{
Q->rear = Q->front->next ;
free( Q->front ) ;
Q->front = Q->rear ;
}
return 0 ;
}
int IsEmptyQuene( LinkQuene* Q )
{
if( Q->front == Q->rear )
{
return 1 ;
}
return 0 ;
}
int ClearQuene( LinkQuene* Q )
{
QNode* p =NULL ;
QNode* q =NULL ;
Q->rear = Q->front ;
p = Q->front->next ;
Q->front->next = NULL ;
while( p )
{
q = p->next ;
free( p ) ;
p = q ;
}
return 0 ;
}
int GetHeadQuene( LinkQuene* Q , ElemType* e )
{
if(IsEmptyQuene( Q ) )
{
printf( "OVERFLOW!\n" ) ;
exit( 1 ) ;
}
*e = Q->front->next->data ;
return 0 ;
}
int GetLengthQuene( LinkQuene* Q )
{
int length = 0 ;
QNode* p = Q->front->next ;
while( p )
{
p = p->next ;
length++ ;
}
return length ;
}
int InsertQuene( LinkQuene* Q , ElemType e )
{
QNode* p = NULL ;
p = ( QNode* )malloc( sizeof( QNode ) ) ;
if( !p )
{
printf( "OVERFLOWINSERT!\n" ) ;
exit( 1 ) ;
}
p->next = NULL ;
p->data = e ;
Q->rear->next = p ;
Q->rear = Q->rear->next ;
return 0 ;
}
int DeleteQuene( LinkQuene* Q , ElemType* e )
{
QNode* p = NULL ;
p = Q->front->next ;
*e = p->data ;
Q->front->next = p->next ;
free( p ) ;
return 0 ;
}
int TraverseQuene( LinkQuene* Q )
{
QNode* p = NULL ;
// Q->rear->next = NULL ;
p = Q->front->next ;
while( p )
{
printf("%d\t" , p->data ) ;
p = p->next ;
}
printf( "\n" ) ;
return 0 ;
}
//主函数测试
#include "quene.h"
int main()
{
CirculQuene Q ;
ElemType e = 0 ;
InitQuene( &Q ) ;
InsertQuene( &Q , 3 ) ;
InsertQuene( &Q , 5 ) ;
InsertQuene( &Q , 7 ) ;
DeleteQuene( &Q , &e ) ;
DeleteQuene( &Q , &e ) ;
InsertQuene( &Q , 9 ) ;
InsertQuene( &Q , 9 ) ;
//InsertQuene( &Q , 5 ) ;
printf( "%d\n" , e ) ;
TraverseQuene( &Q ) ;
return 0;
}