数据结构定义
struct LinkNode
{
struct LinkNode* next;
};
struct LQueue
{
struct LinkNode head;
int nSize;
struct LinkNode* tail;
};
typedef void* LinkQueue;
头文件定义
#ifndef LINK_QUEUE_H
#define LINK_QUEUE_H
//初始化
LinkQueue initial_LinkQueue();
//入队列
void push_LinkQueue(LinkQueue lQueue,void* data);
//返回队头
void* front_LinkQueue(LinkQueue lQueue);
//返回队尾
void* back_LinkQueue(LinkQueue lQueue);
//是否为空
int empty_LinkQueue(LinkQueue lQueue);
//弹出对头
void pop_LinkQueue(LinkQueue lQueue);
//返回队列大小
int size_LinkQueue(LinkQueue lQueue);
//销毁队列
void destroy_LinkQueue(LinkQueue lQueue);
#endif
头文件实现
#include "CLinkQueue.h"
#include "stdlib.h"
//初始化
LinkQueue initial_LinkQueue()
{
struct LQueue* lqueue = (struct LQueue*)malloc(sizeof(struct LQueue));
if(lqueue == NULL)
{
return NULL;
}
lqueue->head.next = NULL;
lqueue->nSize = 0;
lqueue->tail = &lqueue->head;
return lqueue;
}
//入队列
void push_LinkQueue(LinkQueue lQueue,void* data)
{
if(lQueue == NULL)
{
return;
}
if (data == NULL)
{
return;
}
struct LQueue* lq = (struct LQueue*)lQueue;
struct LinkNode* newNode = (struct LinkNode*)data;
newNode->next = NULL;
lq->tail->next = newNode;
lq->tail = newNode;
lq->nSize++;
}
//返回队头
void* front_LinkQueue(LinkQueue lQueue)
{
if(lQueue == NULL)
{
return NULL;
}
struct LQueue* lq = (struct LQueue*)lQueue;
return lq->head.next;
}
//返回队尾
void* back_LinkQueue(LinkQueue lQueue)
{
if(lQueue == NULL)
{
return NULL;
}
struct LQueue* lq = (struct LQueue*)lQueue;
return lq->tail;
}
//是否为空
int empty_LinkQueue(LinkQueue lQueue)
{
if(lQueue == NULL)
{
return -1;
}
struct LQueue* lq = (struct LQueue*)lQueue;
if(lq->nSize==0)
{
return 1;
}
return 0;
}
//弹出对头
void pop_LinkQueue(LinkQueue lQueue)
{
if(lQueue == NULL)
{
return;
}
struct LQueue* lq = (struct LQueue*)lQueue;
if (lq->nSize == 1)//size为1时,队首队尾指针重置
{
lq->nSize--;
lq->tail = &lq->head;
return;
}
lq->head.next = lq->head.next->next;
lq->nSize--;
}
//返回队列大小
int size_LinkQueue(LinkQueue lQueue)
{
if(lQueue == NULL)
{
return -1;
}
struct LQueue* lq = (struct LQueue*)lQueue;
return lq->nSize;
}
//销毁队列
void destroy_LinkQueue(LinkQueue lQueue)
{
if(lQueue == NULL)
{
return;
}
free(lQueue);
lQueue = NULL;
}
测试代码
struct Person
{
void* data;
char name[64];
int nAge;
};
void MyPrint(void* data)
{
Person* person = (Person*)data;
printf("name:%s,age:%d\n",person->name,person->nAge);
}
struct Person p1 = {NULL,"张三",12};
struct Person p2 = {NULL,"李四",15};
struct Person p3 = {NULL,"王麻子",18};
struct Person p4 = {NULL,"张五",19};
struct Person p5 = {NULL,"张飞",22};
printf("-------------------------------------\n");
//linkQueue
LinkQueue lq = initial_LinkQueue();
push_LinkQueue(lq,&p5);
push_LinkQueue(lq,&p4);
push_LinkQueue(lq,&p3);
push_LinkQueue(lq,&p2);
push_LinkQueue(lq,&p1);
printf("LinkQueue size:%d\n",size_LinkQueue(lq));
while(!empty_LinkQueue(lq))
{
Person* person = (Person*)front_LinkQueue(lq);
printf("head name:%s,age:%d\n",person->name,person->nAge);
Person* personb = (Person*)back_LinkQueue(lq);
printf("tail name:%s,age:%d\n",personb->name,personb->nAge);
pop_LinkQueue(lq);
}
printf("LinkQueue size:%d\n",size_LinkQueue(lq));
destroy_LinkQueue(lq);
printf("-------------------------------------\n");
测试结果