logo制作免费版,wp建站优化,做销售网站,电子商务网站实例代码
#include stdio.h
#include stdlib.htypedef int E; //这里我们的元素类型就用int为例吧#xff0c;先起个别名//定义结构体
struct List{E * array;int capacity; //数组的容量int size;
};//给结构体指针起别名
typedef struct List * ArrayLis…代码
#include stdio.h
#include stdlib.htypedef int E; //这里我们的元素类型就用int为例吧先起个别名//定义结构体
struct List{E * array;int capacity; //数组的容量int size;
};//给结构体指针起别名
typedef struct List * ArrayList; //因为是数组实现所以就叫ArrayList这里直接将List的指针起别名//初始化
_Bool initList(ArrayList list){list-array malloc(sizeof (E) * list-capacity); //使用malloc函数申请10个int大小的内存空间作为底层数组使用if(list-array NULL) return 0; //需要判断如果申请的结果为NULL的话表示内存空间申请失败list-capacity 10; //直接将数组的容量设定为10即可list-size0;return 1;
}//插入数据操作
_Bool insertList(ArrayList list, E element, int index){if(index 1 || index list-size 1) return 0; //判断插入位置转换成位序也就是[1, size 1]这个闭区间if(list-size list-capacity) { //如果size已经到达最大的容量了肯定是插不进了那么此时就需要扩容了int newCapacity list-capacity (list-capacity 1); //我们先计算一下新的容量大小这里我取1.5倍原长度当然你们也可以想扩多少扩多少E * newArray realloc(list-array, newCapacity * sizeof(E)); //这里我们使用新的函数realloc重新申请更大的内存空间if(newArray NULL) return 0; //如果申请失败那么就确实没办法插入了只能返回0表示插入失败了list-array newArray;list-capacity newCapacity;}for (int i list-size; i index - 1; --i)list-array[i] list-array[i - 1];list-array[index - 1] element;list-size;return 1;
}//打印数据
void printList(ArrayList list){ //编写一个函数用于打印表当前的数据for (int i 0; i list-size; i) //表里面每个元素都拿出来打印一次printf(%d , list-array[i]);printf(\n);
}//删除操作
_Bool deleteList(ArrayList list, int index){if(index 1 || index list-size) return 0;for (int i index - 1; i list-size - 1; i)list-array[i] list-array[i 1]; //实际上只需要依次把后面的元素覆盖到前一个即可list-size--; //最后别忘了size - 1return 1;
}//获取size的大小
int sizeList(ArrayList list){return list-size;
}//按位置获取元素
E * getList(ArrayList list, int index){if(index 1 || index list-size) return NULL;return list-array[index - 1];
}int findList(ArrayList list, E element){for (int i 0; i list-size; i) { //一直遍历如果找到那就返回位序if(list-array[i] element) return i 1;}return -1; //如果遍历完了都没找到那么就返回-1
}int main(){struct List list; //创建新的结构体变量if(initList(list)){ //对其进行初始化如果失败就直接结束for (int i 0; i30; i){insertList(list, i*10, i1);}deleteList(list, 10);deleteList(list, 10);printList(list);printf(获取数组容量%d\n, list.capacity); //获取数组容量printf(按位置获取元素%d\n, *getList(list, 3)); //按位置获取元素printf(按值查找元素位置%d\n, findList(list, 30)); //按值查找元素位置} else{printf(顺序表初始化失败无法启动程序);}
}
运行效果
文章转载自: http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.jbqwb.cn.gov.cn.jbqwb.cn http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn