(一)队列的链式结构体
#define EleType int
#define Status int
typedef struct QueueNode
{
EleType e;
struct QueueNode* next;
}QueueNode,*LinkQueuePoi;
typedef struct LinkQueue
{
LinkQueuePoi front;
LinkQueuePoi rear;
}LinkQueue;
(二)创建一个空队列
Status InitLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
node->next = NULL;
queue->front = queue->rear = node;
return OK;
}
(三)入栈
Status CleaerLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
if (queue->front == queue->rear)
{
return ERROR;
}
QueueNode* node = queue->front->next;
while (node)
{
queue->front->next = node->next;
if (queue->rear == node)
{
queue->rear = queue->front;
}
free(node);
node = queue->front->next;
}
return OK;
}
(四)判空
Status EmptyLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
if (queue->front == queue->rear)
{
return TRUE;
}
return FALSE;
}
(五)队长
int LengthLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
if (queue->front == queue->rear)
{
return 0;
}
QueueNode* node = queue->front->next;
int num = 0;
while (node)
{
node = node->next;
num++;
}
return num;
}
(六)创建一个空队列
Status AddQueue(LinkQueue* queue,EleType e)
{
if (!queue)
{
return ERROR;
}
QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
if (!node)
{
return ERROR;
}
node->next = NULL;
node->e = e;
queue->rear->next = node;
queue->rear = node;
return OK;
}
(七)出队
Status DelQueue(LinkQueue* queue, EleType *e)
{
if (!queue)
{
return ERROR;
}
QueueNode* node = queue->front->next;
*e = node->e;
queue->front->next = node->next;
if (node = queue->rear)
{
queue->rear = queue->front;
}
return OK;
}
(八)输出
void PrintfLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return;
}
QueueNode* node = queue->front->next;
while (node)
{
printf("%d,", node->e);
node = node->next;
}
printf("\n");
return;
}
(九)调用的主函数
int main(int argc, char *argv[])
{
LinkQueue queue;
InitLinkQueue(&queue);
AddQueue(&queue, 1);
AddQueue(&queue, 2);
AddQueue(&queue, 3);
AddQueue(&queue, 4);
printf("链队列元素个数:%d\n",LengthLinkQueue(&queue));
printf("展示元素:\n");
PrintfLinkQueue(&queue);
int e1, e2;
DelQueue(&queue, &e1);
DelQueue(&queue, &e2);
printf("删除元素:%d,%d\n", e1, e2);
printf("展示元素:\n");
PrintfLinkQueue(&queue);
printf("链队列元素个数:%d\n", LengthLinkQueue(&queue));
CleaerLinkQueue(&queue);
printf("清空元素后,长度为%d,rear = %p,front=%p",LengthLinkQueue(&queue), queue.rear,queue.front);
printf("\n");
return 0;
}
全部的组合代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define QUEUESIZE 10
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
#define EleType int
#define Status int
typedef struct QueueNode
{
EleType e;
struct QueueNode* next;
}QueueNode,*LinkQueuePoi;
typedef struct LinkQueue
{
LinkQueuePoi front;
LinkQueuePoi rear;
}LinkQueue;
Status InitLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
node->next = NULL;
queue->front = queue->rear = node;
return OK;
}
Status CleaerLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
if (queue->front == queue->rear)
{
return ERROR;
}
QueueNode* node = queue->front->next;
while (node)
{
queue->front->next = node->next;
if (queue->rear == node)
{
queue->rear = queue->front;
}
free(node);
node = queue->front->next;
}
return OK;
}
Status EmptyLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
if (queue->front == queue->rear)
{
return TRUE;
}
return FALSE;
}
int LengthLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return ERROR;
}
if (queue->front == queue->rear)
{
return 0;
}
QueueNode* node = queue->front->next;
int num = 0;
while (node)
{
node = node->next;
num++;
}
return num;
}
s AddQueue(LinkQueue* queue,EleType e)
{
if (!queue)
{
return ERROR;
}
QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
if (!node)
{
return ERROR;
}
node->next = NULL;
node->e = e;
queue->rear->next = node;
queue->rear = node;
return OK;
}
Status DelQueue(LinkQueue* queue, EleType *e)
{
if (!queue)
{
return ERROR;
}
QueueNode* node = queue->front->next;
*e = node->e;
queue->front->next = node->next;
if (node = queue->rear)
{
queue->rear = queue->front;
}
return OK;
}
void PrintfLinkQueue(LinkQueue* queue)
{
if (!queue)
{
return;
}
QueueNode* node = queue->front->next;
while (node)
{
printf("%d,", node->e);
node = node->next;
}
printf("\n");
return;
}
int main(int argc, char *argv[])
{
LinkQueue queue;
InitLinkQueue(&queue);
AddQueue(&queue, 1);
AddQueue(&queue, 2);
AddQueue(&queue, 3);
AddQueue(&queue, 4);
printf("链队列元素个数:%d\n",LengthLinkQueue(&queue));
printf("展示元素:\n");
PrintfLinkQueue(&queue);
int e1, e2;
DelQueue(&queue, &e1);
DelQueue(&queue, &e2);
printf("删除元素:%d,%d\n", e1, e2);
printf("展示元素:\n");
PrintfLinkQueue(&queue);
printf("链队列元素个数:%d\n", LengthLinkQueue(&queue));
CleaerLinkQueue(&queue);
printf("清空元素后,长度为%d,rear = %p,front=%p",LengthLinkQueue(&queue), queue.rear,queue.front);
printf("\n");
return 0;
}