实现方式一
1、头文件LinkQueue.h
#include <stdio.h>
typedef struct Node
{
struct Node* next;
}Node;
typedef struct LinkQueue
{
Node* front;
Node* rear;
int length;
}LinkQueue;
void InitQueue(LinkQueue*);
int QueueEmpty(LinkQueue*);
int QueueLength(LinkQueue*);
void GetFront(LinkQueue*, Node**);
void EnQueue(LinkQueue*, Node*);
void DeQueue(LinkQueue*, Node**);
void ClearQueue(LinkQueue*);
2、相关操作函数文件LinkQueue.c
#include <stdio.h>
#include "LinkQueue.h"
void InitQueue(LinkQueue* lq)
{
lq->front = NULL;
lq->rear = NULL;
lq->length = 0;
}
int QueueEmpty(LinkQueue* lq)
{
if(lq->length == 0)
return 1;
return 0;
}
int QueueLength(LinkQueue* lq)
{
return lq->length;
}
void GetFront(LinkQueue* lq, Node** e)
{
if(lq->length == 0)
{
printf("空队列!\n");
return;
}
*e = lq->front;
}
void EnQueue(LinkQueue* lq, Node* e)
{
if(lq->length == 0)
{
lq->front = lq->rear = e;
}
else
{
lq->rear->next = e;
lq->rear = lq->rear->next;
}
lq->length++;
}
void DeQueue(LinkQueue* lq, Node** e)
{
if(lq->length == 0)
{
printf("空队列!\n");
return;
}
*e = lq->front;
lq->front = lq->front->next;
lq->length--;
if(lq->length == 0)
{
lq->rear = NULL;
}
}
void ClearQueue(LinkQueue* lq)
{
while(lq->length)
{
Node* tmp;
DeQueue(lq, (Node**)&tmp);
}
}
3、主函数测试文件main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"
typedef struct stu
{
Node p;
int id;
int age;
}student;
int main()
{
LinkQueue lq;
InitQueue(&lq);
student s[10];
int i = 0;
for(i = 0; i < 10; i++)
{
s[i].id = i;
s[i].age = i + 20;
EnQueue(&lq, &s[i].p);
}
printf("当前队列长度为:%d\n", QueueLength(&lq));
while(!QueueEmpty(&lq))
{
Node* tmp;
GetFront(&lq, (Node**)&tmp);
student* tmp1 = (student*)tmp;
printf("当前队头元素值为:id = %d, age = %d\n", tmp1->id, tmp1->age);
DeQueue(&lq, (Node**)&tmp);
tmp1 = (student*)tmp;
printf("当前出队元素值为:id = %d, age = %d\n", tmp1->id, tmp1->age);
printf("\n");
}
return 0;
}
实现方式二
1、头文件LinkQueue.h
#include <stdio.h>
typedef struct Node
{
struct Node* next;
}Node;
typedef struct QueueNode
{
Node P;
void* data;
}QueueNode;
typedef struct LinkQueue
{
int length;
QueueNode* front;
QueueNode* rear;
}LinkQueue;
void InitQueue(LinkQueue*);
int QueueEmpty(LinkQueue*);
int QueueLength(LinkQueue*);
void GetFront(LinkQueue*, void**);
void EnQueue(LinkQueue*, void*);
void DeQueue(LinkQueue*, void**);
void ClearQueue(LinkQueue*);
2、相关操作函数main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"
void InitQueue(LinkQueue* lq)
{
lq->length = 0;
lq->front = NULL;
lq->rear = NULL;
}
int QueueEmpty(LinkQueue* lq)
{
if(lq->length == 0)
return 1;
return 0;
}
int QueueLength(LinkQueue* lq)
{
return lq->length;
}
void GetFront(LinkQueue* lq, void** e)
{
if(lq->length == 0)
{
printf("空队列!\n");
return;
}
*e = lq->front->data;
}
void EnQueue(LinkQueue* lq, void* e)
{
QueueNode* pNew = (QueueNode*)malloc(sizeof(QueueNode));
pNew->data = e;
if(lq->length == 0)
{
lq->front = lq->rear = pNew;
}
else
{
lq->rear->P.next = &pNew->P;
lq->rear = pNew;
}
lq->length++;
}
void DeQueue(LinkQueue* lq, void** e)
{
if(lq->length == 0)
{
printf("空队列!\n");
return;
}
QueueNode* pDel;
pDel = lq->front;
*e = pDel->data;
lq->front = (QueueNode*)pDel->P.next;
free(pDel);
lq->length--;
if(lq->length == 0)
{
lq->rear = NULL;
}
}
void ClearQueue(LinkQueue* lq)
{
while(lq->length)
{
void* tmp;
DeQueue(lq, (void**)&tmp);
}
}
3、主函数main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"
typedef struct stu
{
int id;
int age;
}student;
int main()
{
LinkQueue lq;
InitQueue(&lq);
student s[10];
int i = 0;
for(i = 0; i < 10; i++)
{
s[i].id = i;
s[i].age = i + 20;
EnQueue(&lq, &s[i]);
}
printf("当前队列的长度为:%d\n", QueueLength(&lq));
while(!QueueEmpty(&lq))
{
student* tmp;
GetFront(&lq, (void**)&tmp);
printf("当前队头元素值为:id = %d, age = %d\n", tmp->id, tmp->age);
DeQueue(&lq, (void**)&tmp);
printf("当前出队元素值为:id = %d, age = %d\n", tmp->id, tmp->age);
printf("\n");
}
printf("当前队列的长度为:%d\n", QueueLength(&lq));
EnQueue(&lq, &s[0]);
ClearQueue(&lq);
printf("当前队列的长度为:%d\n", QueueLength(&lq));
return 0;
}