队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
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);
}
#include
#include
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;
}
循环队列就是将队列存储空间的最后一个位置绕到第一个位置,形成逻辑上的环状空间,供队列循环使用。在循环队列结构中,当存储空间的最后一个位置已被使用而再要进入队运算时,只需要存储空间的第一个位置空闲,便可将元素加入到第一个位置,即将存储空间的第一个位置作为队尾。 循环队列可以更简单防止伪溢出的发生,但队列大小是固定的。
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);
}
#include
#include
#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;
}