码迷,mamicode.com
首页 > 其他好文 > 详细

5.队列

时间:2017-08-01 10:16:57      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:实现   typedef   概念   com   不能   current   class   node   pac   

一.Queue 基本概念

队列是一种特殊的线性表

队列仅在线性表的两端进行操作

队头(Front):取出数据元素的一端

队尾(Rear):插入数据元素的一端

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

技术分享

常用操作

  • 销毁队列
  • 清空队列
  • 进队列
  • 出队列
  • 获取队头元素
  • 获取队列的长度

二.队列的顺序存储设计与实现

seqlist.h

#ifndef __MY_SEQLIST_H__
#define __MY_SEQLIST_H__

#define DLL_API  __declspec(dllexport)    //_declspec(dllexport):导出标志

typedef void SeqList;
typedef void SeqListNode;


// 创建线性表
DLL_API SeqList* SeqList_Create(int capacity);

// 销毁线性表
DLL_API void SeqList_Destroy(SeqList *list);

// 清空线性表
DLL_API void SeqList_Clear(SeqList *list);

// 获得线性表的长度
DLL_API int SeqList_Length(SeqList *list);

// 获得线性表的容量
DLL_API int SeqList_Capacity(SeqList *list);

// 向线性表中插入一个元素
DLL_API int SeqList_Insert(SeqList *list, SeqListNode *node, int pos);

// 获取线性表中某一个位置的元素
DLL_API SeqListNode* SeqList_Get(SeqList *list, int pos);

// 删除线性表中某一个位置的元素
DLL_API SeqListNode* SeqList_Delete(SeqList *list, int pos);



#endif

seqlist.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"


typedef struct _tag_SeqList
{
    int capacity;
    int length;
    unsigned int *node; // unsigned int array[capacity]
}TSeqList;



// 创建线性表
SeqList* SeqList_Create(int capacity)
{
    TSeqList *ret = NULL;
    if (capacity < 0)
    {
        return NULL;
    }
    ret = malloc(sizeof(TSeqList) + sizeof(unsigned int)*capacity);
    if (ret == NULL)
    {
        return NULL;
    }
    memset(ret, 0, sizeof(TSeqList)+sizeof(unsigned int)*capacity);
    ret->node = (unsigned int *)(ret + 1); // ret 向后跳转sizeof(TSeqList)
    ret->capacity = capacity;
    ret->length = 0;

    return ret;
}


// 销毁线性表
void SeqList_Destroy(SeqList *list)
{
    if (list != NULL)
    {
        free(list);
    }

}


// 清空线性表
void SeqList_Clear(SeqList *list)
{
    TSeqList *tList = NULL;
    if (list == NULL)
    {
        return;
    }
    tList = (SeqList*)list;
    tList->length = 0;
}


// 获得线性表的长度
int SeqList_Length(SeqList *list)
{
    TSeqList *tList = NULL;
    tList = (TSeqList*)list;
    if (tList == NULL)
    {
        return -1;
    }
    return tList->length;
}



// 获得线性表的容量
int SeqList_Capacity(SeqList *list)
{
    TSeqList *tList = NULL;
    tList = (TSeqList*)list;
    if (tList == NULL)
    {
        return -1;
    }
    return tList->capacity;
}

// 向线性表中插入一个元素
DLL_API int SeqList_Insert(SeqList *list, SeqListNode *node, int pos)
{
    int i = 0;
    TSeqList *tList = NULL;
    tList = (TSeqList*)list;

    // 保证传入的线性表和元素节点不能为NULL
    if (list == NULL || node == NULL)
    {
        return -1;
    }

    // 判断该线性表是否已满
    if (tList->length >= tList->capacity)
    {
        return -2;
    }

    // 判断插入索引是否合法
    if (pos < 0 || pos >= tList->capacity)
    {
        return -3;
    }

    // 若索引值大于线性表当前长度,则将元素插入到线性表的末尾
    if (pos >= tList->length)
    {
        pos = tList->length;
    }

    // 插入算法
    // 将pos位置后的元素移次向后移
    for (i = tList->length; i > pos; i--)
    {
        // 更新后移元素的值
        tList->node[i] = tList->node[i - 1];
    }

    // 元素后移完毕后,将元素放到指定的位置
    tList->node[pos] = (unsigned int)node;
    tList->length ++;
    return 0;
}



// 获取线性表中某一个位置的元素
SeqListNode* SeqList_Get(SeqList *list, int pos)
{
    SeqListNode *ret = NULL;
    TSeqList *tList = NULL;
    tList = (TSeqList*)list;
    
    // 过滤非法参数
    if (list == NULL || pos < 0 || pos >= tList->length)
    {
        return NULL;
    }

    ret = (SeqListNode*)tList->node[pos];
    return ret;
}


// 删除线性表中某一个位置的元素
SeqListNode* SeqList_Delete(SeqList *list, int pos)
{
    int i = 0;
    TSeqList *tList = NULL;
    SeqListNode *ret = NULL;
    tList = (TSeqList*)list;

    if (list == NULL || pos < 0 || pos >= tList->length)
    {
        return NULL;
    }

    ret = (SeqListNode*)tList->node[pos];
    // 删除算法
    for (i=pos+1; i<tList->length; i++)
    {
        tList->node[i - 1] = tList->node[i];
    }
    tList->length--;

    return ret;
}

seqqueue.h

#ifndef __MY_SEQ_QUEUE_H__
#define __MY_SEQ_QUEUE_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* header);

// 获取队列长度
int SeqQueue_Length(SeqQueue* length);

// 获取队列容量
int SeqQueue_Capacity(SeqQueue* queue);



#endif

seqqueue.c

#include "seqqueue.h"
#include "seqlist.h"


// 创建队列
SeqQueue* SeqQueue_Create(int capacity)
{
    return SeqList_Create(capacity);
}

// 销毁队列
void SeqQueue_Destroy(SeqQueue* queue)
{
    SeqList_Destroy(queue);
}

// 清空队列
void SeqQueue_Clear(SeqQueue* queue)
{
    SeqList_Clear(queue);
}

// 向队尾添加元素,相当于向线性表的尾部插入元素
int SeqQueue_Append(SeqQueue* queue, void* item)
{
    return SeqList_Insert(queue, item, SeqList_Length(queue));
}


// 移除队列头部元素
void* SeqQueue_Retrieve(SeqQueue* queue)
{
    return SeqList_Delete(queue, 0);
}


// 获取队列头部元素
void* SeqQueue_Header(SeqQueue* queue)
{
    return SeqList_Get(queue, 0);
}

// 获取队列长度
int SeqQueue_Length(SeqQueue* queue)
{
    return SeqList_Length(queue);
}

// 获取队列容量
int SeqQueue_Capacity(SeqQueue* queue)
{
    return SeqList_Capacity(queue);
}

运行结果:

current queue capacity is: 10

current queue length is: 6

current student age is: 12
current new length is: 0
请按任意键继续. . .

 

5.队列

标签:实现   typedef   概念   com   不能   current   class   node   pac   

原文地址:http://www.cnblogs.com/yongdaimi/p/7266983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!