数据结构链队

 /* 本程序是链队的基本用法 
	 创建空队、 进队、 出队、 判空、 清空、 获取队列长度、 销毁队
  */
  /* main.c */
#include<stdio.h>
#include"linkqueue.h"
int main()
{
    
    linkqueue lq = CreateQueue();
    EnQueue(lq,100);
    EnQueue(lq,200);
    EnQueue(lq,300);

   // ClearQueue(lq);    // 清空队列

    //GetLenth(lq);      // 队列长度

    while(FALSE == EmptyQueue(lq))
    {
        printf("%d\n",DeQueue(lq));
    }

    return 0;
}
/* linkqueue.c */
#include<stdio.h>
#include<stdlib.h>
#include"linkqueue.h"



// 创建空队
linkqueue CreateQueue() 
{
    linklist list = malloc(sizeof(Lnode));
    linkqueue   Lq = malloc(sizeof(Lqnode));

    // 判断是否malloc到空间
    if ( list == NULL )
    {
        printf("malloc error!\n");
    }

    // 判断是否malloc到空间
    if ( Lq == NULL )
    {
        printf("malloc error!\n");
    }

    list->data = -1;
    list->next = NULL;

    Lq->front = list;
    Lq->rear = list;
    return Lq;
}

// 清空队列
int ClearQueue(linkqueue LQ)      
{
    // 如果链队不为空
    while ( FALSE == EmptyQueue(LQ) ) 
    {
        DeQueue(LQ);
    }
    return OK;
}

//获取队列长度
int GetLenth(linkqueue LQ)         
{
    int len = 0;
    
    while ( LQ->front != NULL )
    {   
        LQ->front = LQ->front->next;
        len++;
    }
    printf("len = %d\n",len);
    return len;
}

// 入队
data_t EnQueue(linkqueue LQ,data_t val)
{
    // 如果链队为空
    if ( TRUE == EmptyQueue(LQ) ) 
    {
        //注意:如果是空队,注意修改尾指针指向
        LQ->rear=LQ->front;
    }

    //生成一个新的节点
    linklist s = malloc(sizeof(Lnode)); 
    s->data = val;                       

    s->next = NULL;                      //插入到末尾
    LQ->rear->next = s;

    LQ->rear = s;                        //修改尾指针
    return val;

}

// 判断是否为空队
int EmptyQueue(linkqueue LQ)
{
    //没有有效数据,只剩头节点
    if ( LQ->front->next == NULL )
    {
        return TRUE;
    }
    else 
    {
        return FALSE;
    }
}


// 出队
data_t DeQueue(linkqueue LQ)
{
    // 如果链队为空
    if ( TRUE == EmptyQueue(LQ) )  
    {
        printf("queue is empty\n");
        return ERROR;
    }

    linklist d = LQ->front->next;  //记录删除节点位置
    data_t val = d->data;             //取值
    
    LQ->front->next  = d->next;    //删除
    free(d);                       //释放

    /*
    if(TRUE == EmptyQueue(LQ)) // 判断是否为空
    {
        LQ -> rear = LQ -> front; //队尾 指向 队头
    }
    */ 
    return val;
}

// 销毁
void DestroyQueue(linkqueue LQ)
{
    linklist d = LQ->front;
    while ( d != NULL )
    {   
        d = d->next;
        free(d);
    }
}

/* linkqueue.h */
#ifndef _LINKQUEUE_H
#define _LINKQUEUE_H

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1

typedef int data_t;

struct node{
    data_t data;
    struct node *next;
};

typedef struct node Lnode;
typedef struct node *linklist;


//将头尾指针包在一起
struct qnode{   
    linklist front;
    linklist rear;
};
typedef struct qnode Lqnode;
typedef struct qnode * linkqueue;

int EmptyQueue(linkqueue LQ);              // 清空队
data_t DeQueue(linkqueue LQ);              // 出队
linkqueue CreateQueue();                   // 创建空队
int ClearQueue(linkqueue LQ);              // 清空队列
int GetLenth(linkqueue LQ);                // 获取队列长度
data_t EnQueue(linkqueue LQ,data_t val);   // 进队
void DestroyQueue(linkqueue LQ);           // 销毁


#endif



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值