链队列:
代码:
#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 LinkNodePtr));
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) {
LinkNodePtr 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("The queue is empty.\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, 3);
enqueue(tempQueuePtr, 4);
enqueue(tempQueuePtr, 5);
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;
}
结果:
3 4 5
dequeue gets 3
dequeue gets 4
dequeue gets 5
The queue is empty.
dequeue gets -1
8
循环队列
代码
#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("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;
}
void outputLinkQueue(CircleIntQueuePtr paraPtr){
int i;
if (paraPtr->head == paraPtr->tail) {
printf("Empty queue.");
return;
}
printf("Elements in the queue: ");
for (i = paraPtr->head; i < paraPtr->tail; i++) {
printf("%d, ", paraPtr->data[i % TOTAL_SPACE]);
}
printf("\r\n");
}
void testLinkQueue(){
int i = 9;
CircleIntQueuePtr tempPtr = initQueue();
for (; i < 16; i ++) {
enqueue(tempPtr, i);
}
outputLinkQueue(tempPtr);
for (i = 0; i < 6; i ++) {
printf("dequeue gets %d\r\n", dequeue(tempPtr));
}
enqueue(tempPtr, 8);
outputLinkQueue(tempPtr);
}
int main(){
testLinkQueue();
return 1;
}
结果
Queue full.
Queue full.
Queue full.
Elements in the queue: 9, 10, 11, 12,
dequeue gets 9
dequeue gets 10
dequeue gets 11
dequeue gets 12
No element in the queue.
dequeue gets -1
No element in the queue.
dequeue gets -1
Elements in the queue: 8,
--------------------------------