方法一、通过对顺序表代码的复用实现队列
一、SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(int capacity);
void SeqList_Destroy(SeqList* list);
void SeqList_Clear(SeqList* list);
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
SeqListNode* SeqList_Get(SeqList* list, int pos);
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif
二、SeqList.c
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode;
typedef struct _tag_SeqList
{
int capacity;
int length;
TSeqListNode* node;
} TSeqList;
SeqList* SeqList_Create(int capacity) // O(1)
{
TSeqList* ret = NULL;
if( capacity >= 0 )
{
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
}
if( ret != NULL )
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret + 1);
}
return ret;
}
void SeqList_Destroy(SeqList* list) // O(1)
{
free(list);
}
void SeqList_Clear(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
if( sList != NULL )
{
sList->length = 0;
}
}
int SeqList_Length(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->length;
}
return ret;
}
int SeqList_Capacity(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->capacity;
}
return ret;
}
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL);
int i = 0;
ret = ret && (sList->length + 1 <= sList->capacity);
ret = ret && (0 <= pos);
if( ret )
{
if( pos >= sList->length )
{
pos = sList->length;
}
for(i=sList->length; i>pos; i--)
{
sList->node[i] = sList->node[i-1];
}
sList->node[i] = (TSeqListNode)node;
sList->length++;
}
return ret;
}
SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
if( (sList != NULL) && (0 <= pos) && (pos <= sList->length) )
{
ret = (SeqListNode*)(sList->node[pos]);
}
return ret;
}
SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = SeqList_Get(list, pos);
int i = 0;
if( ret != NULL )
{
for(i=pos+1; i<sList->length; i++)
{
sList->node[i-1] = sList->node[i];
}
sList->length--;
}
return ret;
}
三、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 "SeqList.h"
#include "SeqQueue.h"
SeqQueue* SeqQueue_Create(int capacity) // O(1)
{
return SeqList_Create(capacity);
}
void SeqQueue_Destroy(SeqQueue* queue) // O(1)
{
SeqList_Destroy(queue);
}
void SeqQueue_Clear(SeqQueue* queue) // O(1)
{
SeqList_Clear(queue);
}
int SeqQueue_Append(SeqQueue* queue, void* item) // queue为队列的地址,item为插入元素的地址
{
return SeqList_Insert(queue, item, SeqList_Length(queue)); //从队尾插入
}
void* SeqQueue_Retrieve(SeqQueue* queue) // O(n)
{
return SeqList_Delete(queue, 0); //从对头删除
}
void* SeqQueue_Header(SeqQueue* queue) // O(1)
{
return SeqList_Get(queue, 0);
}
int SeqQueue_Length(SeqQueue* queue) // O(1)
{
return SeqList_Length(queue);
}
int SeqQueue_Capacity(SeqQueue* queue) // O(1)
{
return SeqList_Capacity(queue);
}
五、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(20);
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));
}
SeqQueue_Destroy(queue);
return 0;
}