DataStructure-用数组实现List
来源:互联网 发布:监测 移动数据造假 编辑:程序博客网 时间:2024/04/29 16:46
表的基本概念
表(线性表),是一种非常灵活的结构,可以根据自己的需要改变表的长度,也可以在其中任何位置对元素进行访问、插入、删除等操作。另外还可以将多个表连接成一个表,或者把一个表拆分多个表。
表的图示结构
用数组实现表
#include <stdio.h>#include <malloc.h>typedef int ListItem;//将int类型取别名为ListItem,让代码便于读typedef struct alist *List;typedef struct alist{ int n;//表的长度 int maxsize;//表的最大长度 ListItem *table;//表元素数组}Alist;List ListInit(int size){//初始化List,参数为表长度 List L=malloc(sizeof *L); L->table=malloc(size*sizeof(ListItem));//分配内存 L->maxsize=size; L->n=0; return L;}int ListEmpty(List L){//判断表是否为空 return L->n==0;}int ListLength(List L){//返回当前表长度 return L->n;}int ListLocate(ListItem x,List L){//返回元素x在表中的位置 int i; for(i=0;i<L->n;i++){ if(L->table[i]==x) return ++i; } return 0;}int ListRetrieve(int k,List L){//返回表L中k处的元素 if(k<1 || k>L->n) printf("out of bounds"); return L->table[k-1];}void ListInsert(int k,ListItem x,List L){//插入函数,将元素x插入位置k处,k+1之后位置的元素向右移 int i; if(k<0 || k>L->n+1) { printf("out of bounds!"); } if(L->n==L->maxsize) printf("out of memory!"); for(i=L->n-1;i>=k;i++){ L->table[i+1]=L->table[i]; } L->table[k-1]=x; L->n++;}ListItem ListDelete(int k,List L){//删除位置K处的元素,将K+1之后的向左移 int i; ListItem x; if(k<1||k>L->n) printf("out of bounds!"); x=L->table[k-1]; for(i=k;i<L->n;i++) L->table[i-1]=L->table[i]; L->n--; return x;}void printList(List L){//遍历列表L,打印 int i; for(i=0;i<L->n;i++){ printf("%d\n",L->table[i] ); }}void main(){ List L=ListInit(10); ListInsert(1,1,L); ListInsert(2,2,L); ListInsert(3,3,L); ListInsert(4,4,L); ListInsert(5,5,L); ListInsert(6,6,L); ListInsert(7,7,L); ListDelete(3,L); printList(L); printf("element %d -> %d\n",5,ListLocate(5,L) ); printf("locate %d -> %d\n",5,ListRetrieve(5,L) ); printf("ListLength ->%d\n",ListLength(L) );}
0 0
- DataStructure-用数组实现List
- DataStructure-用指针实现List
- C Datastructure 1 ---- linear list
- js用数组实现List
- 【DataStructure】Descriptioin and usage of List
- 【DataStructure】Another usage of List: Polynomial
- C Datastructure 2 ---- link list(1)
- C Datastructure 3 ---- link list(2)
- C Datastructure 4 ---- link list(3)
- 数据结构之list(用数组实现)
- DataStructure----LinerList,线性表C实现
- DataStructure---LinkList 链表C语言实现代码
- Java 数据结构实现(DataStructure)1
- Java 数据结构实现(DataStructure)2
- 【DataStructure】 Five methods to init the List in java
- 利用List来实现动态的数组。
- python实现list数组转置
- 超级数组,list底层实现模仿
- linux 操作系统中pwd命令的使用
- MYSQL事务的开启与提交
- PPPOE + FreeRADIUS + MySQL 配置记录
- iOS程序的启动过程和应用程序生命周期
- nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
- DataStructure-用数组实现List
- Python第三方包cx_Oracle的配置
- hdu1664 Different Digits (搜索)
- server. MapPath 问题
- maven开发入门
- jsp遍历map集合
- node.js 使用createServer创建最基础的node应用。
- 考察一下你对js继承链的理解
- 1012. 数字分类 (20)-PAT乙级真题