队列也是一种表,队列提供两种基本操作,即在队头(front)出队(Dequeue)与在队尾(rear)入队(Enqueue)。
与栈相同,队列也有种实现方式,链表实现与数组实现。
一.队列的链表实现
基本想法,持有一个header节点作为定位front的指针,并定义rear指针,同时为了在出队时方便获取上一个节点的指针,我们以时间换空间牺牲一点内存以双链表的实现。
头文件
#ifndef _Queue_H
#define _Queue_H
struct NodeStruct;
struct QueueStruct;
typedef int ElementType;
typedef struct NodeStruct *PtrToNode;
typedef struct QueueStruct *PtrToQueue;
typedef PtrToNode Node;
typedef PtrToQueue Queue;
Queue initQueue(int capacity);
void enQueue(Queue queue,ElementType element);
Node deQueue(Queue queue);
int isFull(Queue queue);
int isEmpty(Queue queue);
void destoryQueue(Queue queue);
void printQueue(Queue queue);
void checkNull(Queue queue);
#endif
struct NodeStruct{
ElementType element;
Node next;
Node prev;
};
/*保留一个header节点,使用header->next定位front*/
struct QueueStruct{
int size;
int capacity;
Node header;
Node rear;
};
实现
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#define DefaultCapacity ( 16 )
Queue initQueue(int capacity){
Queue queue;
Node header;
if(capacity > DefaultCapacity)
capacity = DefaultCapacity;
header = malloc(sizeof(struct NodeStruct));
if(header == NULL){
fprintf(stderr,"%s\n","OutofMemory!");
exit(1);
}
header->next = NULL;
header->prev = NULL;
queue = malloc(sizeof(struct QueueStruct));
if(queue == NULL){
fprintf(stderr,"%s\n","OutofMemory!");
exit(1);
}
queue->size = 0;
queue->capacity = capacity;
queue->header = header;
queue->rear = NULL;
}
void enQueue(Queue queue,ElementType element){
checkNull(queue);
if(isFull(queue)){
printf("%s","queue is full>>");
printQueue(queue);
return;
}
Node header,rear,newNode;
header = queue->header;
rear = queue->rear;
newNode = malloc(sizeof(struct NodeStruct));
if(newNode == NULL){
fprintf(stderr,"%s\n","queue is null");
exit(1);
}
newNode->element = element;
newNode->next=NULL;
if(rear == NULL){
newNode->prev = header;
header->next = newNode;
}else{
newNode->prev = rear;
rear->next = newNode;
}
queue->rear = newNode;
queue->size++;
}
Node deQueue(Queue queue){
Node header,front,result;
checkNull(queue);
if(queue->size == 0)
return NULL;
header = queue->header;
result = header->next;
header->next = front->next;
queue->size--;
return result;
}
void destoryQueue(Queue queue){
checkNull(queue);
Node header,temp;
header = queue->header;
while(header != NULL){
temp = header->next;
free(header);
header = temp;
}
free(queue);
queue = NULL;
}
void printQueue(Queue queue){
Node header,firstNode;
checkNull(queue);
if(queue->size == 0){
printf("%s\n","queue is empty!");
return;
}
header = queue->header;
firstNode = header->next;
printf("<-[");
while(firstNode != NULL){
printf("%d,",firstNode->element);
firstNode = firstNode->next;
}
if(queue->size >0){
printf("\b]<-\n");
}else{
printf("]<-\n");
}
}
void checkNull(Queue queue){
if(queue == NULL){
fprintf(stderr,"%s\n","queue is null");
exit(1);
}
}
int isFull(Queue queue){
checkNull(queue);
return queue->size == queue->capacity;
}
int isEmpty(Queue queue){
checkNull(queue);
return queue->size == 0;
}
二.关于Java提供的Queue
jdk提供的队列继承关系是这样的(只列常用的):