数据结构--链队代码实现

头文件

#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_

#include <stdlib.h>
typedef int data_t;
typedef struct lkqueue{
    data_t data;
    struct lkqueue *next;
}lk_queue;
typedef struct {
    lk_queue *head;
    lk_queue *tail;
}lk;

//创建
lk *create_lkq();

//判空
int empty_lkq(lk *lq);

//入队
int en_lkq(lk *lq,data_t data);

//出队
int de_lkq(lk *lq);

//打印
int show_lkq(lk *lq);

//求长度
int length_lkq(lk *lq);

//清空
int clear_lkq(lk *lq);

//摧毁
int destory_lkq(lk **lq);

#endif

功能函数

#include <stdio.h>

#include "lkqueue.h"

//创建
lk *create_lkq()
{
    lk_queue *link = (lk_queue *)malloc(sizeof(lk_queue));
    if(NULL == link)
        return NULL;
    link->next = NULL;
    lk *lq = (lk *)malloc(sizeof(lk));
    if(NULL == lq)
        return NULL;
    lq->head = link;
    lq->tail = link;
    return lq;
}

//判空
int empty_lkq(lk *lq)
{
    return lq->head == lq->tail;
}

//入队
int en_lkq(lk *lq,data_t data)
{
    lk_queue *new = (lk_queue *)malloc(sizeof(lk_queue));
    if(NULL == new)
    {
        printf("new is error\n");
        return -1;
    }
    new->data = data;
    new->next = NULL;
    lq->tail->next = new;
    lq->tail = new;
    return 0;
}
//出队
int de_lkq(lk *lq)
{
    if(1 == empty_lkq(lq))
    {
        printf("lq is empty\n");
        return -1;
    }
    lk_queue *q = lq->head;
    data_t temp =q->next->data;
    lq->head = q->next;
    free(q);
    return temp;
}

//打印
int show_lkq(lk *lq)
{
    if(1 == empty_lkq(lq))
    {
        printf("lq is empty\n");
        return -1;
    }
    lk_queue *p = lq->head->next;
    while(p)
    {
        printf("%d--",p->data);
        p = p->next;
    }
    printf("\n");
    return 0;
}

//求长度
int length_lkq(lk *lq)
{
    int len = 0;
    lk_queue *p = lq->head->next;
    while(p)
    {
        len++;
        p = p->next;
    }
    return len;
}

//清空
int clear_lkq(lk *lq)
{
    lk_queue *p = lq->head->next;
    while(p)
    {
        de_lkq(lq);
        p = p->next;
    }
    return 0;
}

//摧毁
int destory_lkq(lk **lq)
{
    clear_lkq(*lq);
    free(*lq);
    *lq = NULL;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值