您的位置:首页 > 理论基础 > 数据结构算法

数据结构线性表的顺序存储实现(C语言)

2020-06-29 05:00 204 查看

这是我根据上课,作业要求填写的代码整理而来的。
代码涵盖了与线性表基本算法:初始化、遍历、元素插入,取数据、定位、删除以及注销。

#include <iostream>
#include <stdio.h>
#include  <stdlib.h>

#define ok 1
#define error 0
#define listinitsize   50
#define listincrement 10

typedef int  status;
typedef char  elemtype;
typedef struct{
elemtype  *elem;
int length;
int listsize;
}sqlist;

status initlist(sqlist &L)
{	// 0 初始化
L.elem = (elemtype * )malloc(listinitsize*sizeof(elemtype));
L.length = 0;
L.listsize = listinitsize;
return ok;

}

status listinsert(sqlist &L, int i, elemtype e)
{  	// 1 插入
int j;
if(i<1&&i>L.length+1) return error;
for(j=L.length;j>=i;j--){
L.elem[j]=L.elem[j-1];
}
L.elem[i-1]=e;
L.length++;
return ok;

}

status  listtraverse(sqlist L)
{ 	// 2 遍历
printf("The length of the sqlist: %d\n",L.length);
printf("The data of the sqlist: ");
for(int i=0;i<L.length;i++)
printf("%c ",L.elem[i]);
printf("\n");
return ok;

}

status   getelem(sqlist L,int i, elemtype &e)
{	// 3 取数据
if(i<1||i>L.length)
return error;
e=L.elem[i-1];
return ok;

}

int locateelem(sqlist L,elemtype  e)
{  // 4 定位
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e)
return i+1;

}

status listdelete(sqlist &L,int i,elemtype &e)
{   // 5 删除
if(i<1||i>L.length) return error;
elemtype *q;
e=L.elem[i-1];
for(q=&L.elem[i-1];q<=&L.elem[L.length-1];q++)
*q=*(q+1);
L.length--;
return ok;

}

status destroylist(sqlist &L)
{ 	// 6 注销
delete []L.elem;
L.length=0;
L.listsize=0;
return ok;

}

int main()
{	 // 线性表的顺序存储实现
elemtype e;
sqlist L;

int TestCase;
scanf("%d", &TestCase);

printf("(1)初始化线性表L:\n");
initlist(L);

printf("(2)依次在线性表L尾插入字符a,b,c,d,e:\n");
listinsert(L,1,'a');
listinsert(L,2,'b');
listinsert(L,3,'c');
listinsert(L,4,'d');
listinsert(L,5,'e');
printf("(3)在线性表L的第2个位置之前插入字符f:\n");
listinsert(L,2,'f');

switch( TestCase ){
case 1:		// 遍历
printf("(4)输出线性表L:\n");
listtraverse(L);
break;

case 2:		// 取元素
printf("(5)输出线性表L的第3个元素:");
getelem(L,3,e);
printf("%c\n",e);
break;

case 3:		// 定位
printf("(6)输出元素d在线性表L的位置:%d\n",locateelem(L,'d'));
break;

case 4:		// 删除
printf("(7)删除线性表L的第4个元素:\n");
listdelete(L,4,e);
printf("(8)输出线性表L:\n");
listtraverse(L);
break;

default:	// 综合 (遍历、取数、定位、删除、遍历)
printf("(4)输出线性表L:\n");
listtraverse(L);

printf("(5)输出线性表L的第3个元素:");
getelem(L,3,e);
printf("%c\n",e);

printf("(6)输出元素d在线性表L的位置:%d\n",locateelem(L,'d'));

printf("(7)删除线性表L的第4个元素:\n");
listdelete(L,4,e);

printf("(8)输出线性表L:\n");
listtraverse(L);
}

printf("(9)释放线性表L。\n");
destroylist(L);

return ok;
}

这是代码运行结果:

最后,小生不才,感谢浏览。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: