C语言创建顺序表并插入元素 详细注释 您所在的位置:网站首页 c语言数组添加元素 C语言创建顺序表并插入元素 详细注释

C语言创建顺序表并插入元素 详细注释

2024-04-04 07:07| 来源: 网络整理| 查看: 265

顺序表是用一组地址连续的存储单元依次存储数据元素的数据结构。顺序表是线性表的一种,线性表是最常用且最简单的一种数据结构,一个线性表是 n 个数据元素的有限序列。我们使用 c 语言来创建顺序表并插入元素。

IDE : Code::Blocks 17.12

Compiler : GNU GCC Compiler

/*创建顺序表并插入元素*/ #include #include #define Listsize 100 //顺序表可容纳最大值 //声明顺序表 typedef struct sqlist{ int data[Listsize]; //存储顺序表中的元素 int length; //顺序表中含有的元素个数 }; //在顺序表中插入元素 void InsertList(struct sqlist * list,int t,int i){ //插入位置为i,插入值为t int j; if(ilist->length){ //插入位置不合法 printf("位置错误!"); exit(1); } if(list->length>=Listsize){ //超出顺序表范围,溢出 printf("溢出"); exit(1); } for(j=list->length-1;j>=i;--j){ //腾出位置i以供插入数据t list->data[j+1]=list->data[j]; } list->data[i]=t; //在位置i插入数据t list->length++; //顺序表长度加1 } int main() { struct sqlist * sq; //创建顺序表sq int i,n,t; sq=(struct sqlist *)malloc(sizeof(struct sqlist)); //分配空间 sq->length=0; //初始化顺序表长度为0 printf("输入顺序表的大小:"); scanf("%d",&n); printf("请输入顺序表的元素:\n"); //在顺序表中插入n个元素 for(i=0;idata[i]); } return 0; }

------不比别人聪明,就比别人更加努力------



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有