数据结构之顺序队列

队列是一种特殊的线性表
队列仅在线性表的两端进行操作
队头(Front):取出数据元素的一端
队尾(Rear):插入数据元素的一端

队列不允许在中间部位进行操作!

数据结构之顺序队列_第1张图片

1、顺序队列
 (1)顺序队列的定义
   队列的顺序存储结构称为顺序队列,顺序队列实际上是运算受限的顺序表。

(2) 顺序队列的表示

  ①和顺序表一样,顺序队列用一个向量空间来存放当前队列中的元素。
  ②由于队列的队头和队尾的位置是变化的,设置两个指针front和rear分别指示队头元素和队尾元素在向量空间中的位置,它们的初值在队列初始化时均应置为0。

数据结构之顺序队列_第2张图片

队列的一些常用操作
 创建队列
 销毁队列
 清空队列
 进队列
 出队列
 获取队头元素
 获取队列的长度


typedef void SeqList;
typedef void SeqListNode;

#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;
}

然后对函数封装:

#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);
}

测试代码:

#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);
    system("pause");
	return 0;
	
}

数据结构之顺序队列_第3张图片



你可能感兴趣的:(数据结构)