C 队列顺序存储
- #ifndef __MY_SEQLIST_H__
- #define __MY_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 //__MY_SEQLIST_H__
- #ifndef _MY_SEQQUEUE_H_
- #define _MY_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 //_MY_SEQQUEUE_H_
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "seqlist.h"
- //用数组来模拟线性表
- typedef struct _tag_SeqList
- {
- int capacity;
- int length;
- //int *node[100];
- int **node; //int node[capacity] //
- //int *node[capacity];
- //int *node; // int node[i]===> *(node+i)
- }TSeqList;
- //链表 创建
- SeqList* SeqList_Create(int capacity) //O(1)
- {
- int ret;
- TSeqList *tmp = NULL;
- tmp = (TSeqList *)malloc(sizeof(TSeqList));
- if (tmp == NULL)
- {
- ret =;
- printf("func SeqList_Create() err :%d \n", ret);
- return NULL;
- }
- memset(tmp, , sizeof(TSeqList));
- tmp->capacity = capacity;
- tmp->length = ;
- tmp->node = (int **)malloc(sizeof(void *) * capacity);
- if (tmp->node == NULL)
- {
- ret = ;
- printf("func SeqList_Create() malloc err :%d \n", ret);
- return NULL;
- }
- memset(tmp->node, , sizeof(void *) * capacity);
- return tmp;
- }
- //链表 创建
- int SeqList_Create2(int capacity, SeqList**handle)
- {
- int ret = ;
- TSeqList *tmp = NULL;
- tmp = (TSeqList *)malloc(sizeof(TSeqList));
- if (tmp == NULL)
- {
- ret =;
- printf("func SeqList_Create2() err :%d \n", ret);
- return ret;
- }
- memset(tmp, , sizeof(TSeqList));
- tmp->capacity = capacity;
- tmp->length = ;
- tmp->node = (int **)malloc(sizeof(void *) * capacity);
- if (tmp->node == NULL)
- {
- ret = ;
- printf("func SeqList_Create2() malloc err :%d \n", ret);
- return ret;
- }
- *handle = tmp;
- return ret;
- }
- //链表 销毁
- void SeqList_Destroy(SeqList* list) //O(1)
- {
- TSeqList *tmp = NULL;
- if (list == NULL)
- {
- return ;
- }
- tmp = (TSeqList *)list;
- if (tmp->node != NULL)
- {
- free(tmp->node);
- }
- free(tmp);
- return ;
- }
- ////链表 清空
- void SeqList_Clear(SeqList* list) //O(1)
- {
- TSeqList *tmp = NULL;
- if (list == NULL)
- {
- return ;
- }
- tmp = (TSeqList *)list;
- tmp->length = ;
- memset(tmp->node, , (tmp->capacity * sizeof(void *)) );
- return ;
- }
- //链表 长度
- int SeqList_Length(SeqList* list) //O(1)
- {
- TSeqList *tmp = NULL;
- if (list == NULL)
- {
- return -;
- }
- tmp = (TSeqList *)list;
- return tmp->length;
- }
- //链表 容量
- int SeqList_Capacity(SeqList* list) //O(1)
- {
- TSeqList *tmp = NULL;
- if (list == NULL)
- {
- return -;
- }
- tmp = (TSeqList *)list;
- return tmp->capacity;
- }
- //链表 在某一个位置 插入元素
- int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //O(n)
- {
- TSeqList *tList = NULL;
- int i = ;
- if (list == NULL || node==NULL)
- {
- return -;
- }
- tList = (TSeqList *)list;
- //如果满了
- if (tList->length >= tList->capacity)
- {
- return -;
- }
- //pos位置的容错处理
- if (pos > tList->length )
- {
- pos = tList->length;
- }
- for (i=tList->length; i>pos; i--) //n
- {
- tList->node[i] = tList->node[i-];
- }
- tList->node[i] = (int* )node; //ok
- tList->length ++;
- return ;
- }
- //获取某一个位置的链表结点
- SeqListNode* SeqList_Get(SeqList* list, int pos) //O(1)
- {
- TSeqList *tList = NULL;
- SeqListNode *tmp = NULL;
- tList = (TSeqList *)list;
- if (list == NULL || pos< || pos >=tList->length )
- {
- return NULL;
- }
- tmp = tList->node[pos];
- return tmp;
- }
- //删除某一个位置的结点
- SeqListNode* SeqList_Delete(SeqList* list, int pos) ////O(n)
- {
- int i = ;
- TSeqList *tList = NULL;
- SeqListNode *tmp = NULL;
- tList = (TSeqList *)list;
- if (list == NULL || pos < || pos >= tList->length)
- {
- return NULL;
- }
- tmp = tList->node[pos];
- // pos = 3
- for (i=pos+; i<tList->length; i++)
- {
- tList->node[i-] = tList->node[i];
- }
- tList->length --;
- return tmp;
- }
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #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));
- }
- //提取队头元素 相当于 删除链表0号位置元素
- void* SeqQueue_Retrieve(SeqQueue* queue)
- {
- return SeqList_Delete(queue, ) ;
- }
- //获取队头元素 相当于 获取 链表0号位置元素
- void* SeqQueue_Header(SeqQueue* queue)
- {
- return SeqList_Get(queue, );
- }
- int SeqQueue_Length(SeqQueue* queue)
- {
- return SeqList_Length(queue);
- }
- int SeqQueue_Capacity(SeqQueue* queue)
- {
- return SeqList_Capacity(queue);
- }
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "seqqueue.h"
- void main()
- {
- int i = , a[];
- SeqQueue *queue = NULL;
- for (i=; i<; i++)
- {
- a[i] = i + ;
- }
- queue = SeqQueue_Create();
- //向队列的尾部 添加元素
- for (i=; i<; i++)
- {
- SeqQueue_Append(queue, a + i);
- }
- //获取队列的属性
- printf("capacity:%d \n", SeqQueue_Capacity(queue));
- printf("length:%d \n", SeqQueue_Length(queue));
- printf("队头: %d \n", *( (int *)SeqQueue_Header(queue) ));
- //销毁 队列
- while (SeqQueue_Length(queue) > )
- {
- int tmp;
- tmp = *( (int *)SeqQueue_Retrieve(queue) );
- printf("%d ", tmp);
- }
- SeqQueue_Destroy(queue);
- printf("hello...\n");
- system("pause");
- return ;
- }
C 队列顺序存储的更多相关文章
- [置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- 队列顺序存储 - 设计与实现 - API函数
队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! queue常用操作 销毁队列 清空队 ...
- 基于python实现顺序存储的队列代码
""" 队列-顺序存储 seqqueue.py 代码实现 """ # 自定义异常类 class QueueError(Exception): ...
- (转)java redis使用之利用jedis实现redis消息队列
应用场景 最近在公司做项目,需要对聊天内容进行存储,考虑到数据库查询的IO连接数高.连接频繁的因素,决定利用缓存做. 从网上了解到redis可以对所有的内容进行二进制的存储,而java是可以对所有对象 ...
- 队列链式存储 - 设计与实现 - API函数
队列相关基础内容参我的博文:队列顺序存储 - 设计与实现 - API函数 队列也是一种特殊的线性表:可以用线性表链式存储来模拟队列的链式存储. 主要代码: // linkqueue.h // 队列链式 ...
- 预热一下吧《实现Redis消息队列》
应用场景 为什么要用redis?二进制存储.java序列化传输.IO连接数高.连接频繁 一.序列化 这里编写了一个java序列化的工具,主要是将对象转化为byte数组,和根据byte数组反序列化成ja ...
- Java利用Redis实现消息队列
应用场景 为什么要用redis?二进制存储.java序列化传输.IO连接数高.连接频繁 一.序列化 这里编写了一个java序列化的工具,主要是将对象转化为byte数组,和根据byte数组反序列化成ja ...
- [PHP] 2018年终总结
去掉敏感信息后的不完整版 ==========================================================================2018年12月29日 记 ...
随机推荐
- POJ 3436 ACM Computer Factory (拆点+输出解)
[题意]每台计算机由P个零件组成,工厂里有n台机器,每台机器针对P个零件有不同的输入输出规格,现在给出每台机器每小时的产量,问如何建立流水线(连接各机器)使得每小时生产的计算机最多. 网络流的建图真的 ...
- HDU 5280 Senior's Array (暴力,水)
题意:给一个数列,再给一个数字p,要求p一定要替换掉数列中的一个元素,然后求最大连续子序列之和. 思路:1000*1000的复杂度,O(n*n) .就是每个都试,然后求和. #include < ...
- Azure 负载平衡器新分发模式
Yves Pitsch Azure 网络首席项目经理 Azure负载平衡器是一种第四层(TCP.UDP)类型的负载平衡器,它可以将传入流量分发到云服务中正常运行的服务实例上,或者分发到负载平衡器集内所 ...
- CSS布局中——导航是非常常见的
导航绝对是页面布局中最常见的,为了不用每次去写,稍微贴个简单的导航模版出来,方便以后使用. <title>CSS菜单</title> <style type=" ...
- 自定义Sharepoint的登陆页面
转:http://www.cnblogs.com/jecoso/archive/2008/05/25/1207151.html 原文作者:Damon Armstrong 原文地址:http://www ...
- ODAC访问oracle时,提示:由于以前的函数求值超时,函数求值被禁用,必须继续执行才能正常返回
这是因为调试时会自动对Local/Watch等窗口里面(或鼠标停留所在)的变量求值,为了防止用户写的程序错误(比如死循环),系统有一个超时限制,如果某个属性的get中做了很复杂的操作(而不是简单地返回 ...
- POJ 1236 Network of Schools 有向图强连通分量
参考这篇博客: http://blog.csdn.net/ascii991/article/details/7466278 #include <stdio.h> #include < ...
- bzoj 1835 [ZJOI2010]base 基站选址(DP+线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1835 [题意] 有n个村庄,每个村庄位于d[i],要求建立不多于k个基站,在第i个村庄 ...
- 6.1 CUDA: pinned memory固定存储
CPU和GPU内存交互 在CUDA编程中,内存拷贝是非常费时的一个动作. 从上图我们可以看出:1. CPU和GPU之间的总线bus是PCIe,是双向传输的. 2. CPU和GPU之间的数据拷贝使用DM ...
- HW7.10
public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...