多语言展示
当前在线:171今日阅读:75今日分享:44

线性表的实现

本次介绍链表的实现.包括插入,删除,求长度等等
工具/原料
1

VC++ 6.0

2

C语言基础知识(其中输出语句用到了C++)

方法/步骤
1

代码如下:

2

#include#include#include#define INITSIZE 2#define INCREACESIZE 1//数据抽象出来.目的:改一个全部改typedef int DataType;struct List{ DataType *data; int length; int allocate_size;}*L;/*说明:return -1 表示失败  return 1  表示成功  *///功能://    1)查看当前表的长度//    2)查看当前分配的空间//    3)遍历表void ditalList(List &L){ cout<<'**********************************************************************\n'; cout<<'\n当前长度:'<= L.allocate_size) { cout<<'从新非配内存中....\n'; int *newData=(DataType *)realloc(L.data,(INCREACESIZE + L.length)*sizeof(DataType)); if(newData== NULL) { err(); return -1; } L.allocate_size += INCREACESIZE; L.data=newData; } if(position <1 || position > L.allocate_size) { cout<<'插入位置不合法\n'; return -1; } //操作过程: // 1)获得要插入的位置的地址 // 2)从最后一个开始,一直到当前位置,依次后移 // 3)插入 // 4)表长加1 int *p,*q; p=&(L.data[position-1]); for(q=&(L.data[L.length-1]);q>=p;q--) { L.data[position + 1]=L.data[position]; } *p=value; L.length += 1;}//获取表的长度int lengthList(List &L){ return L.length;}//清空表void clearList(List &L){ L.length=0;}//表是否满bool isFull(List &L){ if(L.length>=L.allocate_size) return true; return false;}//查询指定值是否在表中int searchList(List &L,DataType value){ int i; for(i=0;i L.length) { cout<<'删除位置不合法'; return false; } value=L.data[position - 1]; int i; //如果发现想不出来,画图方能解决此问题 for(i=position;i<=L.length;i++) { L.data[i-1]=L.data[i]; } L.length -=1;}void main(){ L=(List *)malloc(sizeof(List)); initList(*L); ditalList(*L); insertList(*L,1,1); insertList(*L,2,2); insertList(*L,3,3); insertList(*L,4,4); insertList(*L,5,5); insertList(*L,6,6); insertList(*L,7,7); ditalList( *L); int value; removeList(*L,5,value); cout<<'删除的元素为:'<

3

以上代码已运行并且通过,可以直接复制测试或者改良

推荐信息