数据结构实现链式队列(C语言)

//头文件

#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;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值