线性表的实现

时间:2026-02-15 18:26:30

1、代码如下:

2、#include<stdlib.h>

#include<iostream.h>

#include<malloc.h>

#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.length;

cout<<"\n当前分配空间:"<<L.allocate_size;

cout<<"\n";

if(L.length == 0)

cout<<"\n空表或不存在该表\n";

else{

int i;

for(i=0;i<L.length;i++)

{

cout<<"\n表中的第"<<i+1<<"数据为: "<<L.data[i];

}

}

cout<<"\n此次遍历完成\n";

}

//分配内存出错

void err()

{

cout<<"分配内存失败";

exit(-1);

}

//初始化链表

void initList(List &L)

{

//实现:

//    1)为链表中的data非配内存

//    2)为表长 和  初始分配空间赋初值

L.data=(DataType *)malloc(INITSIZE*sizeof(DataType));

if(L.data == NULL)

err();

L.length=0;

L.allocate_size= INITSIZE;

}

int insertList(List &L,int position,int value)

{

//前提条件:

// 1) 插入位置正却

// 2) 如果表满,则从以INCREACESIZE 为增量增加

//注意:   先判断是否表满,再判断位置是否满足

//解释:   先由长度 与 当前分配空间的大小   来确定是否进行分配.

if(L.length >= 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;i++)

{

if(value == L.data[i])

return i;

}

return  -1;

}

//删除指定的元素

bool removeList(List &L,int position,DataType  &value)

{

//第一个角标为1

//实现:

// 1)判断位置是否合理

// 2)value存放要删除的值

// 3)节点前移

// 4)长度减一

// 5)返回状态

if(position <1 || position > 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<<"删除的元素为:"<<value<<"\n";

ditalList(* L);

cout<<"表长为:"<<lengthList(*L)<<"\n";

if(isFull(*L))

cout<<"表满\n";

else

cout<<"表未满\n";

cout<<"该元素是表中的第"<<searchList(*L,3)<<"个元素"<<"\n";

}

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

© 2026 一点资料
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com