一、链队列
链队列即为队列的链式存储结构,简称链队列。
展示代码:
#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode
{
int data;
struct LinkNode* next;
}*LinkNodePtr;
typedef struct LinkQueue
{
LinkNodePtr front;
LinkNodePtr rear;
}*LinkQueuePtr;
LinkQueuePtr initQueue()
{
LinkQueuePtr resultPtr;
resultPtr = (LinkQueuePtr)malloc(sizeof(struct LinkQueue));
LinkNodePtr headerPtr;
headerPtr = (LinkNodePtr)malloc(sizeof(LinkNodePtr));
headerPtr->next = NULL;
resultPtr->front = headerPtr;
resultPtr->rear = headerPtr;
return resultPtr;
}
void outputLinkQueue(LinkQueuePtr paraQueuePtr)
{
LinkNodePtr tempPtr;
tempPtr = paraQueuePtr->front->next;
while (tempPtr != NULL)
{
printf("%d ", tempPtr->data);
tempPtr = tempPtr->next;
}
printf("\r\n");
}
void enqueue(LinkQueuePtr paraQueuePtr, int paraElement)
{
LinkNodePtr tempNodePtr;
tempNodePtr = (LinkNodePtr)malloc(sizeof(struct LinkNode));
tempNodePtr->data = paraElement;
tempNodePtr->next = NULL;
paraQueuePtr->rear->next = tempNodePtr;
paraQueuePtr->rear = tempNodePtr;
}
int dequeue(LinkQueuePtr paraQueuePtr)
{
int resultValue;
LinkNodePtr tempNodePtr;
if (paraQueuePtr->front == paraQueuePtr->rear)
{
printf("此队列为空.\r\n");
return -1;
}
tempNodePtr = paraQueuePtr->front->next;
resultValue = tempNodePtr->data;
paraQueuePtr->front->next = paraQueuePtr->front->next->next;
if (paraQueuePtr->rear == tempNodePtr)
{
paraQueuePtr->rear = paraQueuePtr->front;
}
tempNodePtr = NULL;
return resultValue;
}
void testLinkQueue()
{
LinkQueuePtr tempQueuePtr;
tempQueuePtr = initQueue();
enqueue(tempQueuePtr, 20);
enqueue(tempQueuePtr, 40);
enqueue(tempQueuePtr, 60);
enqueue(tempQueuePtr, 80);
enqueue(tempQueuePtr, 100);
outputLinkQueue(tempQueuePtr);
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
enqueue(tempQueuePtr, 15);
outputLinkQueue(tempQueuePtr);
}
int main()
{
testLinkQueue();
return 1;
}
代码运行结果:
二、 循环队列
为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular Queue)。循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。
展示代码:
#include <stdio.h>
#include <malloc.h>
#define TOTAL_SPACE 5
typedef struct CircleIntQueue
{
int data[TOTAL_SPACE];
int head;
int tail;
}*CircleIntQueuePtr;
CircleIntQueuePtr initQueue()
{
CircleIntQueuePtr resultPtr = (CircleIntQueuePtr)malloc(sizeof(struct CircleIntQueue));
resultPtr->head = 0;
resultPtr->tail = 0;
return resultPtr;
}
void enqueue(CircleIntQueuePtr paraPtr, int paraValue)
{
if ((paraPtr->tail + 1) % TOTAL_SPACE == paraPtr->head)
{
printf("队列已满.\r\n");
return;
}
paraPtr->data[paraPtr->tail % TOTAL_SPACE] = paraValue;
paraPtr->tail++;
}
int dequeue(CircleIntQueuePtr paraPtr)
{
int resultValue;
if (paraPtr->head == paraPtr->tail)
{
printf("队列无元素.\r\n");
return -1;
}
resultValue = paraPtr->data[paraPtr->head % TOTAL_SPACE];
paraPtr->head++;
return resultValue;
}
void outputLinkQueue(CircleIntQueuePtr paraPtr)
{
int i;
if (paraPtr->head == paraPtr->tail)
{
printf("队列为空.");
return;
}
printf("队列中的元素: ");
for (i = paraPtr->head; i < paraPtr->tail; i++)
{
printf("%d ", paraPtr->data[i % TOTAL_SPACE]);
if(i<paraPtr->tail-1)
{
printf(",");
}
}
printf("\r\n");
}
void testLinkQueue()
{
int i;
CircleIntQueuePtr tempPtr = initQueue();
for (i=5;i < 12;i ++)
{
enqueue(tempPtr, i);
}
outputLinkQueue(tempPtr);
for (i = 0; i < 8; i ++)
{
printf("dequeue gets %d\r\n", dequeue(tempPtr));
}
enqueue(tempPtr, 8);
outputLinkQueue(tempPtr);
}
int main(){
testLinkQueue();
return 1;
}
运行结果: