数据结构-链式队列(有头结点)(实现)

  • 头文件linkQueue.h

#ifndef _LINKQUEUE_H
#define _LINKQUEUE_H

#include <stdio.h>
#include <stdlib.h>

typedef int Data;
typedef struct LinkQueue//节点结构体
{
    Data data;
    struct LinkQueue *next;
}LinkQueue,*PLinkQueue;

typedef struct TH//头尾指针结构体
{
    PLinkQueue tail;
    PLinkQueue head;
}TH,*PTH;

PTH linkQueue_Create();//创建
int linkQueue_JudgeEmpty(PTH th);//判空
int linkQueue_In(PTH th,Data val);//入队
Data linkQueue_Out(PTH th);//出队
int linkQueue_Clean(PTH th);//清空
#endif
  • 实现文件linkQueue.c

#include "linkQueue.h"

PTH linkQueue_Create()//创建
{
    PLinkQueue headNode = (PLinkQueue)malloc(sizeof(LinkQueue));
    if(headNode==NULL)
    {
        perror("Create fail");
        return NULL;
    }
    headNode->next=NULL;
    PTH th = (PTH)malloc(sizeof(TH));
    if(th==NULL)
    {
        perror("Create fail");
        return NULL;
    }
    th->tail=headNode;
    th->head=headNode;
    return th;
}

int linkQueue_JudgeEmpty(PTH th)//判空
{
    if(th==NULL&&th->tail==NULL)
    {
        perror("linkQueue is NULL");
        return -1;
    }
    if(th->head==th->tail)
    {
        return 1;
    }else
    {
        return 0;
    }
}

int linkQueue_In(PTH th,Data val)//入队
{
    if(th==NULL&&th->tail==NULL)
    {
        perror("linkQueue is NULL");
        return -1;
    }
    PLinkQueue newNode = (PLinkQueue)malloc(sizeof(LinkQueue));
    newNode->data=val;
    newNode->next=NULL;
    th->next=newNode;
    th=th->next;
    return 1;
}

Data linkQueue_Out(PTH th)//出队
{
    if(th==NULL&&th->tail==NULL)
    {
        perror("linkQueue is NULL");
        return -1;
    }
    Data val = th->head->next->data;
    PLinkQueue p  = th->head;
    th->head=th->head->next;
    free(p);
    return val;
}

int linkQueue_Clean(PTH th)//清空
{
    if(th==NULL&&th->tail==NULL)
    {
        perror("linkQueue is NULL");
        return -1;
    }
    while(th->head!=th->tail)
    {
        linkQueue_Out(th);
    }
    return 1;
}

int linkQueue_Destroy(PTH *p)//摧毁
{
    if(linkQueue_JudgeEmpty(*p)!=1)
    {
        linkQueue_Clean(*p);
    }
    PLinkQueue q = *p->tail;
    free(q);
    q=NULL;
    free(*P);
    *P=NULL;
    return 1;
}

若有错误欢迎指正(*^▽^*)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值