第22课 - 队列的定义及实现
1. 定义
队列是一种特殊的线性表。
队列仅仅在线性表的两端进行操作。
队头:取出数据元素的一端。
队尾:插入数据元素的一端。
队列允许在中间部位进行操作。
2. 队列的操作
创建队列
销毁队列
清空队列
进队列
出队列
获取队头元素
获取队列长度
3. 示例
(1)LinkQueue
main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkQueue.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
LinkQueue* queue = LinkQueue_Create();
int a[10] = {0};
int i = 0;
for(i=0; i<10; i++)
{
a[i] = i + 1;
LinkQueue_Append(queue, a + i);
}
printf("Header: %d ", *(int*)LinkQueue_Header(queue));
printf("Length: %d ", LinkQueue_Length(queue));
while( LinkQueue_Length(queue) > 0 )
{
printf("Retrieve: %d ", *(int*)LinkQueue_Retrieve(queue));
}
LinkQueue_Destroy(queue);
return 0;
}
LinkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef void LinkList;
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
LinkListNode* next;
};
LinkList* LinkList_Create();
void LinkList_Destroy(LinkList* list);
void LinkList_Clear(LinkList* list);
int LinkList_Length(LinkList* list);
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
LinkListNode* LinkList_Get(LinkList* list, int pos);
LinkListNode* LinkList_Delete(LinkList* list, int pos);
#endif
LinkList.c
#include <stdio.h>
#include <malloc.h>
#include "LinkList.h"
typedef struct _tag_LinkList
{
LinkListNode header;
int length;
} TLinkList;
LinkList* LinkList_Create() // O(1)
{
TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
if( ret != NULL )
{
ret->length = 0;
ret->header.next = NULL;
}
return ret;
}
void LinkList_Destroy(LinkList* list) // O(1)
{
free(list);
}
void LinkList_Clear(LinkList* list) // O(1)
{
TLinkList* sList = (TLinkList*)list;
if( sList != NULL )
{
sList->length = 0;
sList->header.next = NULL;
}
}
int LinkList_Length(LinkList* list) // O(1)
{
TLinkList* sList = (TLinkList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->length;
}
return ret;
}
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)
{
TLinkList* sList = (TLinkList*)list;
int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
int i = 0;
if( ret )
{
LinkListNode* current = (LinkListNode*)sList;
for(i=0; (i<pos) && (current->next != NULL); i++)
{
current = current->next;
}
node->next = current->next;
current->next = node;
sList->length++;
}
return ret;
}
LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
{
TLinkList* sList = (TLinkList*)list;
LinkListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
LinkListNode* current = (LinkListNode*)sList;
for(i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
}
return ret;
}
LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
{
TLinkList* sList = (TLinkList*)list;
LinkListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
LinkListNode* current = (LinkListNode*)sList;
for(i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
current->next = ret->next;
sList->length--;
}
return ret;
}
LinkQueue.h
#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_
typedef void LinkQueue;
LinkQueue* LinkQueue_Create();
void LinkQueue_Destroy(LinkQueue* queue);
void LinkQueue_Clear(LinkQueue* queue);
int LinkQueue_Append(LinkQueue* queue, void* item);
void* LinkQueue_Retrieve(LinkQueue* queue);
void* LinkQueue_Header(LinkQueue* queue);
int LinkQueue_Length(LinkQueue* queue);
#endif
LinkQueue.c
#include <malloc.h>
#include "LinkList.h"
#include "LinkQueue.h"
typedef struct _tag_LinkQueueNode
{
LinkListNode header;
void* item;
} TLinkQueueNode;
LinkQueue* LinkQueue_Create() // O(1)
{
return LinkList_Create();
}
void LinkQueue_Destroy(LinkQueue* queue) // O(n)
{
LinkQueue_Clear(queue);
LinkList_Destroy(queue);
}
void LinkQueue_Clear(LinkQueue* queue) // O(n)
{
while( LinkQueue_Length(queue) > 0 )
{
LinkQueue_Retrieve(queue);
}
}
int LinkQueue_Append(LinkQueue* queue, void* item) // O(n)
{
TLinkQueueNode* node = (TLinkQueueNode*)malloc(sizeof(TLinkQueueNode));
int ret = (item != NULL) && (node != NULL);
if( ret )
{
node->item = item;
ret = LinkList_Insert(queue, (LinkListNode*)node, LinkList_Length(queue));
}
if( !ret )
{
free(node);
}
return ret;
}
void* LinkQueue_Retrieve(LinkQueue* queue) // O(1)
{
TLinkQueueNode* node = (TLinkQueueNode*)LinkList_Delete(queue, 0);
void* ret = NULL;
if( node != NULL )
{
ret = node->item;
free(node);
}
return ret;
}
void* LinkQueue_Header(LinkQueue* queue) // O(1)
{
TLinkQueueNode* node = (TLinkQueueNode*)LinkList_Get(queue, 0);
void* ret = NULL;
if( node != NULL )
{
ret = node->item;
}
return ret;
}
int LinkQueue_Length(LinkQueue* queue) // O(1)
{
return LinkList_Length(queue);
}
(2)SeqQueue
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 ", *(int*)SeqQueue_Header(queue));
printf("Length: %d ", SeqQueue_Length(queue));
printf("Capacity: %d ", SeqQueue_Capacity(queue));
while( SeqQueue_Length(queue) > 0 )
{
printf("Retrieve: %d ", *(int*)SeqQueue_Retrieve(queue));
}
SeqQueue_Destroy(queue);
return 0;
}
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) // O(1)
{
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);
}
小结:
队列是一种特殊的线性表。
对列只允许在线性表的头部和尾部进行操作。
队列的实现方式有两种:顺序结构实现,链式结构实现。