头文件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;
}
若有错误欢迎指正(*^▽^*)