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__
#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));//置为0
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 "seqlist.h" typedef struct _Teacher
{
char name[];
int age ;
}Teacher; void main()
{
int ret = , i = ;
SeqList * list; Teacher t1, t2, t3;
t1.age = ;
t2.age = ;
t3.age = ; list = SeqList_Create(); //思考1: 如何实现 链表的api(链表的算法) 和 具体的数据分离
//思考2: 链表库(业务逻辑) 测试程序的业务逻辑 结点的生命周期 归谁管? ret = SeqList_Insert(list, (SeqListNode *)&t1, ); //头插法
ret = SeqList_Insert(list, (SeqListNode *)&t2, ); //头插法
ret = SeqList_Insert(list, (SeqListNode *)&t3, ); //头插法 //遍历链表
for (i=; i<SeqList_Length(list); i++ )
{
Teacher *tmp = (Teacher *)SeqList_Get(list, i); //获取链表结点
if (tmp == NULL)
{
printf("func SeqList_Get() err:%d \n ", ret);
return ;
}
printf("age:%d \n", tmp->age);
} //销毁链表
while (SeqList_Length(list) > )
{
Teacher *tmp = (Teacher *)SeqList_Delete(list, );//
if (tmp == NULL)
{
printf("func SeqList_Get() err:%d \n ", ret);
return ;
}
printf("age:%d \n", tmp->age);
} SeqList_Destroy(list); system("pause"); return ;
}
C _数据结构 _线性表的顺序存储的更多相关文章
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- 【Java】 大话数据结构(1) 线性表之顺序存储结构
本文根据<大话数据结构>一书,实现了Java版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...
- c语言数据结构之线性表的顺序存储结构
线性表,即线性存储结构,将具有“一对一”关系的数据“线性”地存储到物理空间中,这种存储结构就称为线性存储结构,简称线性表. 注意:使用线性表存储的数据,要求数据类型必须一致,线性表存储的数据,要么全不 ...
- 数据结构之线性表的顺序存储结构的实现--C语言版
#include <stdio.h> #include <stdlib.h> #include <time.h> #define INIT_SIZE 100 #de ...
- 2.2_线性表的顺序存储结构_参考集合ArrayList
[线性表的顺序存储从结构] 指的是用一段连续的存储单元一次储存线性表的数据元素. [线性表的顺序存储的结构代码 C语言版] #define MAXSIZE 20 /*存储空间初始分配量*/ typed ...
- C 数据结构1——线性表分析(顺序存储、链式存储)
之前是由于学校工作室招新,跟着大伙工作室招新训练营学习数据结构,那个时候,纯碎是小白(至少比现在白很多)那个时候,学习数据结构,真的是一脸茫然,虽然写出来了,但真的不知道在干嘛.调试过程中,各种bug ...
- 数据结构4:顺序表(线性表的顺序存储结构)及C语言实现
逻辑结构上呈线性分布的数据元素在实际的物理存储结构中也同样相互之间紧挨着,这种存储结构称为线性表的顺序存储结构. 也就是说,逻辑上具有线性关系的数据按照前后的次序全部存储在一整块连续的内存空间中,之间 ...
- c数据结构 -- 线性表之 顺序存储结构 于 链式存储结构 (单链表)
线性表 定义:线性表是具有相同特性的数据元素的一个有限序列 类型: 1:顺序存储结构 定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构 算法: #include <stdio. ...
- javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现
线性表(linear list)是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成. 其中: 数据元素的个数n定义为 ...
- javascript实现数据结构:线性表--线性链表(链式存储结构)
上一节中, 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单,直观的公式来表示.然后,另一方面来看,这个特点也造成这种存储 ...
随机推荐
- bzoj1003: [ZJOI2006]物流运输
dp+最短路.暴力枚举就可以了.O(n3logn).样例中m=n然后测样例过了.然后 54行习惯性的dis[n]然后就WA了!!!. #include<cstdio> #include&l ...
- 【spring-boot】快速构建spring-boot微框架
spring-boot是一个快速构建环境的一套框架,其设计理念是尽可能的减少xml的配置,用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义 ...
- linux SPI bus demo hacking
/********************************************************************** * linux SPI bus demo hacking ...
- 【转】android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创)
原文网址:http://www.cnblogs.com/zdz8207/archive/2012/11/27/android-ndk-install.html android 最新 NDK r8 在w ...
- (七)学习MVC之CodeFirst迁移更新数据库
1.首先在程序包管理控制台输入:enable-migrations -force ,然后回车: 问题1: The EntityFramework package is not installed on ...
- java设计模式—Adapter模式
1.核心意图: 将一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的类可以一起工作. 该模式的目标是通过一个代理(这里是Adapter),在原来的类(Adap ...
- 制作动态链接库给opencv程序使用(使用QtCreator)
新建一个c++库项目 pro文件 #------------------------------------------------- # # Project created by QtCreator ...
- 用JDK自带的工具生成客户端调用Webservice的代码
JAVA下客户端调用Webservice代码简直是让人心生畏惧,今日尝试,做记录如下,参考网上的众多解决方案,下面这种方式是比较简单的. 在jdk的bin目录下有一个wsimport.exe的工具,使 ...
- mac中viso的兼容工具
http://www.orsoon.com/Mac/77738.html破解教程 http://www.pc6.com/mac/111747.html下载地址 很强大,很爽.
- VS2010手动添加外部工具和快捷键
一.添加外部工具 1.选择工具->外部工具 2.选择添加,然后填相应的程序路径和传给程序的参数. 标题:所创建的工具名字 命令:工具路径 参数:工具命令行参数 初始目录:工具工作目录,有些工具可 ...