由于通过顺序表代码的复用实现队列的过程中,进队列要从队列的最后一个元素进入,所以造成时间复杂度加大,现通过引进front、rear优化队列实现方法
front:代表头元素的下标
rear:代表队尾下一个元素的下标
一、SeqQueue.h
#ifndef _SEQQUEUE_H_
#define _SEQQUEUE_H_
typedef void SeqQueue;
SeqQueue* SeqQueue_Create(int capacity); //创建队列
void SeqQueue_Destroy(SeqQueue* queue); //销毁队列
void SeqQueue_Clear(SeqQueue* queue); //清空队列
int SeqQueue_Append(SeqQueue* queue, void* item); //进队列
void* SeqQueue_Retrieve(SeqQueue* queue); //出队列
void* SeqQueue_Header(SeqQueue* queue); //获取队列的头元素
int SeqQueue_Length(SeqQueue* queue); //获取队列当前的长度
int SeqQueue_Capacity(SeqQueue* queue); //获取当前的容量
#endif
二、SeqQueue.c
#include <stdio.h>
#include <malloc.h>
#include "SeqQueue.h"
typedef unsigned int TSeqQueueNode;
typedef struct _tag_SeqQueue //定义一个结构体
{
int capacity;
int length;
int front; //始终代表队头的下标
int rear; //队尾下一个元素的下标
TSeqQueueNode* node;
} TSeqQueue;
SeqQueue* SeqQueue_Create(int capacity) // O(1)
{
TSeqQueue* ret = NULL;
if( capacity >= 0 )
{
ret = (TSeqQueue*)malloc(sizeof(TSeqQueue) + sizeof(TSeqQueueNode) * capacity);
}
if( ret != NULL )
{
ret->capacity = capacity;
ret->length = 0;
ret->front = 0;
ret->rear = 0;
ret->node = (TSeqQueueNode*)(ret + 1);
}
return ret;
}
void SeqQueue_Destroy(SeqQueue* queue) // O(1)
{
free(queue);
}
void SeqQueue_Clear(SeqQueue* queue) // 清空队列
{
TSeqQueue* sQueue = (TSeqQueue*)queue;
if( sQueue != NULL ) //当队列为空时,front与rear都为0
{
sQueue->length = 0;
sQueue->front = 0;
sQueue->rear = 0;
}
}
int SeqQueue_Append(SeqQueue* queue, void* item) // O(1)
{
TSeqQueue* sQueue = (TSeqQueue*)queue;
int ret = (sQueue != NULL) && (item != NULL);
ret = ret && (sQueue->length + 1 <= sQueue->capacity); //判断队列是否满了
if( ret )
{
sQueue->node[sQueue->rear] = (TSeqQueueNode)item;//将入队列的元素赋值给rear下标的元素node
sQueue->rear = (sQueue->rear + 1) % sQueue->capacity; //将rear下标加一
sQueue->length++; //长度加一
}
return ret;
}
void* SeqQueue_Retrieve(SeqQueue* queue) // O(1)
{
TSeqQueue* sQueue = (TSeqQueue*)queue;
void* ret = SeqQueue_Header(queue);
if( ret != NULL ) //判断队列的头一个元素是否存在
{
sQueue->front = (sQueue->front + 1) % sQueue->capacity;//将front加一的元素赋值给front元素
sQueue->length--; //长度减一
}
return ret;
}
void* SeqQueue_Header(SeqQueue* queue) // O(1)
{
TSeqQueue* sQueue = (TSeqQueue*)queue;
void* ret = NULL;
if( (sQueue != NULL) && (sQueue->length > 0) )
{
ret = (void*)(sQueue->node[sQueue->front]);
}
return ret;
}
int SeqQueue_Length(SeqQueue* queue) // O(1)
{
TSeqQueue* sQueue = (TSeqQueue*)queue;
int ret = -1;
if( sQueue != NULL )
{
ret = sQueue->length;
}
return ret;
}
int SeqQueue_Capacity(SeqQueue* queue) // O(1)
{
TSeqQueue* sQueue = (TSeqQueue*)queue;
int ret = -1;
if( sQueue != NULL )
{
ret = sQueue->capacity;
}
return ret;
}
三、main.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqQueue.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
SeqQueue* queue = SeqQueue_Create(6);
int a[10] = {0};
int i = 0;
for(i=0; i<10; i++)
{
a[i] = i + 1;
SeqQueue_Append(queue, a + i);
}
printf("Header: %d\n", *(int*)SeqQueue_Header(queue));
printf("Length: %d\n", SeqQueue_Length(queue));
printf("Capacity: %d\n", SeqQueue_Capacity(queue));
while( SeqQueue_Length(queue) > 0 )
{
printf("Retrieve: %d\n", *(int*)SeqQueue_Retrieve(queue));
}
printf("\n");
for(i=0; i<10; i++)
{
a[i] = i + 1;
SeqQueue_Append(queue, a + i);
printf("Retrieve: %d\n", *(int*)SeqQueue_Retrieve(queue));
}
SeqQueue_Destroy(queue);
return 0;
}
原文地址:http://blog.csdn.net/liuchuangjin/article/details/46237649