数据结构实现链式队列(C语言)
2012-07-18 23:56
721 查看
//头文件
#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;
}
#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;
}
相关文章推荐
- 数据结构 c语言实现链式循环队列(输入数字入队 输入字母出队)
- 数据结构C语言实现之链式队列的6种算法代码
- C语言 队列的链式结构的实现与表示 数据结构 队列的实现与表示
- 数据结构 - 队列简介 及 1个简单的c语言链式队列代码实现
- 数据结构---C语言之队列的链式表示和实现(链队列)
- (C语言)队列的链式实现(数据结构十一)
- 数据结构学习笔记-队列的链式存储(C语言实现)
- (C语言)队列的链式实现(数据结构十一)
- C语言数据结构之实现顺序存储结构队列
- 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
- 数据结构:循环队列(C语言实现)
- C语言数据结构队列实现舞伴匹配(数据结构第二次实验)
- 数据结构之---C语言实现链式队列
- 数据结构-队列-顺序表实现-C语言
- 对链式队列的实现(C语言)
- 数据结构:栈的链式实现(C语言描述)
- 数据结构:循环队列(C语言实现)
- 数据结构学习之队列的链式存储的C++实现
- 数据结构之C++实现链式队列(LinkQueue)(无主函数)
- 数据结构:循环队列(C语言实现)