1.队头(front):允许删除的一端
队尾(rear):允许插入的一端
2.空队列:不含任何元素的空表
3.队列的特点:先进先出
链队列:
#include<stdio.h>
#include<malloc.h>
typedef struct LinkNode{
int data;
LinkNode* next;
}*LinkNodePtr;
typedef struct LinkQueue{
LinkNodePtr front;
LinkNodePtr rear;
}*LinkQueuePtr;
LinkQueuePtr initQueue(){
LinkQueuePtr resultPtr = (LinkQueuePtr)malloc(sizeof(struct LinkQueue));
LinkNodePtr headerPtr = (LinkNodePtr)malloc(sizeof(struct LinkNode));
headerPtr->next = NULL;
resultPtr->front = headerPtr;
resultPtr->rear = headerPtr;
return resultPtr;
}
void outputLinkQueue(LinkQueuePtr paraQueuePtr){
LinkNodePtr tempPtr = paraQueuePtr->front->next;
while(tempPtr !=NULL){
printf("%d",tempPtr->data);
tempPtr = tempPtr->next;
}
printf("\r\n");
}
void enqueue(LinkQueuePtr paraQueuePtr,int paraElement){
//step 1.create a new node
LinkNodePtr tempNodePtr = (LinkNodePtr)malloc(sizeof(struct LinkNode));
tempNodePtr->data = paraElement;
tempNodePtr->next = NULL;
//step 2.limk to the existing rear
paraQueuePtr->rear->next = tempNodePtr;
//step 3.it is the new rear
paraQueuePtr->rear = tempNodePtr;
}//of enqueue
int dequeue(LinkQueuePtr paraQueuePtr){
int resultValue;
LinkNodePtr tempNodePtr;
//step 1.is the queue empty?
if(paraQueuePtr->front == paraQueuePtr->rear){
printf("The queue is empty.\r\n");
return -1;
}
//step 2.change the queue
tempNodePtr = paraQueuePtr->front->next;
resultValue = tempNodePtr->data;
paraQueuePtr->front->next = paraQueuePtr->front->next->next;
if(paraQueuePtr->rear == tempNodePtr){
paraQueuePtr->rear == paraQueuePtr->front;
}
//step 3.free space
tempNodePtr = NULL;
//step 4.return.
return resultValue;
}
void testLinkQueue(){
LinkQueuePtr tempQueuePtr;
tempQueuePtr = initQueue();
enqueue(tempQueuePtr,10);
enqueue(tempQueuePtr,30);
enqueue(tempQueuePtr,50);
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));
enqueue(tempQueuePtr,8);
outputLinkQueue(tempQueuePtr);
}
int main()
{
testLinkQueue();
return 1;
}//of main
运行截图
循环队列
#include<stdio.h>
#include<malloc.h>
#define TOTAL_SPACE 5
/*
*circle int queue
*/
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("Queue full.\r\n");
return;
}
paraPtr->data[paraPtr->tail%TOTAL_SPACE] = paraValue;
paraPtr->tail++;
}
int dequeue(CircleIntQueuePtr paraPtr){
int resultValue;
if(paraPtr->head == paraPtr->tail){
printf("no element in the queue.\r\n");
return -1;
}
resultValue = paraPtr->data[paraPtr->head%TOTAL_SPACE];
paraPtr->head++;
return resultValue;
}//of dequeue
/*
*output the queue
*/
void outputLinkQueue(CircleIntQueuePtr paraPtr){
int i;
if(paraPtr->head == paraPtr->tail){
printf("empty queue.");
return;
}//of it
printf("element in the queue:");
for(i=paraPtr->head;i<paraPtr->tail;i++){
printf("%d",paraPtr->data[i%TOTAL_SPACE]);
}//of for it
printf("\r\n");
}//of outputlinkquue
void testLinkQueue(){
int i=10;
CircleIntQueuePtr tempPtr = initQueue();
for(;i<16;i++){
enqueue(tempPtr,i);
}//of for it
outputLinkQueue(tempPtr);
for(i=0;i<6;i++){
printf("dequeue gets %d\r\n",dequeue(tempPtr));
}//of for it
enqueue(tempPtr,8);
outputLinkQueue(tempPtr);
}
int main(){
testLinkQueue();
return 1;
}//of mian
运行结果
队空状态:qu.rear==qu.front。
队满状态:(qu.rear+1)%maxSize==qu.front