前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言线性表(实现线性表里面的函数)

C语言线性表(实现线性表里面的函数)

作者头像
贵哥的编程之路
发布2022-05-13 13:03:27
5390
发布2022-05-13 13:03:27
举报
文章被收录于专栏:用户7873631的专栏
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>

#define KSIZE 10//定义常量:线性表的长度
#define FALSE 0
#define TRUE 1

/************************************************************************/
/* 线性表(linear list)
线性表是一个相当灵活的数据结构,它的长度可以根据需要增长和缩短,即对线性表的数据元素不仅可以进行访问,还可以进行插入和删除等。
抽象定义的线性表如下:
ADT:Abstract Data Type 抽象数据类型

ADT LIST

L:LIST简称,即线性表本身
i:索引
e:element简称,即元素
cur_:current简称,即当前元素
pre_:previous简称,即前一个元素
next_:next,即下一个元素
visit:对元素访问的方式

InitList(&L)   &L你可以想象成一个容器(数组)   :初始化线性表
DestroyList(&L)  &L你可以想象成一个容器(数组)              :销毁线性表
ClearList(&L) &L你可以想象成一个容器(数组)                 :清空线性表
ListEmpty(L)   L你可以想象成一个容器(数组)                :线性表是否为空
ListLength(L)  L你可以想象成一个容器(数组)               :线性表中元素个数
GetElem(L, i, &e)L你可以想象成一个容器(数组) i代表索引  &e代表获取的元素是啥?             :获取线性表中指定的元素
LocateElem(L, e, compare())L你可以想象成一个容器(数组),e代表想从数组里面查找有没有这个e元素	   :给定元素获取第一次出现的索引位置
PriorElem(L, cur_ e, &pre_ e) 代表获取L代表数组  cur_e代表指定元素 &pre_ e代表指定元素的上一个元素   :给定元素获取其前一个元素
NextElem(L, cur_ e, &next_ e)  代表获取L代表数组  cur_e代表指定元素  &next_ e代表指定元素的下一个元素  :给定元素获取其后一个元素
ListInsert(&L, i, e)&L你可以想象成一个容器(数组)  i指定位置  e插入的元素是啥?         :将元素插入链表中指定位置
ListDelete(&L, i, &e)&L你可以想象成一个容器(数组)  i指定元素  &e删除的元素是啥?       :从链表中指定位置删除元素
ListTraverse(L, visit())   遍历数组   :遍历元素

简单线性表--C语言实现

	线性表组成类型:int数组*/

/************************************************************************/
/*---------------------------------------
InitList(&L);
DestroyList(&L);
ClearList(&L);
ListEmpty(L);
ListLength(L);
GetElem(L, i, &e);
LocateElem(L, e, compare());
ListInsert(&L, i, e);
ListDelete(&L, i, &e);
ListTraverse(L, visit());


PriorElem(L, cur_ e, &pre_ e);
NextElem(L, cur_ e, &next_ e);
-----------------------------------------*/


int count;
void InitList(int *list);//InitList(&L) &代表*
void DestroyList(int *list);
void ClearList(int *list);
int ListEmpty(int *list);
int ListLength(int *list);
int GetElem(int *list, int i, int *e);
int LocateElem(int *list, int e);
int ListInsert(int *list, int i, int e);
int ListDelete(int *list, int i, int *e);
void ListTraverse(int *list);

int main(void)
{
	int arr[KSIZE];
	int e = 0;

	InitList(arr);
	ListInsert(arr, 0, 5);
	ListInsert(arr, 0, 8);
	ListInsert(arr, 1, 7);
	ListTraverse(arr);
	ListDelete(arr, 0, NULL);
	ListTraverse(arr);
	

	GetElem(arr, 1, &e);
	printf("e = %d\n", e);

	printf("5的索引是%d\n", LocateElem(arr, 5));

	ClearList(arr);

	if(ListEmpty(arr))
	{
		printf("线性表为空\n");
	}
	else
	{
		printf("线性表不为空\n");
	}

	printf("线性表的长度为%d\n", ListLength(arr));
	DestroyList(arr);
	
	system("pause");
	return 0;
}

void InitList(int *list)// &L你可以想象成一个容器(数组)   :初始化线性表
{
	int i = 0;
	count = 0;
	for(i = 0; i < KSIZE; i++)//遍历线性表,赋值为0,相当于初始化
	{
		list[i] = 0;
	}
}

void DestroyList(int *list)
{}

void ClearList(int *list)//&L你可以想象成一个容器(数组)                 :清空线性表
{
	int i = 0;
	count = 0;
	for(i = 0; i < KSIZE; i++)
	{
		list[i] = 0;//和初始化一样,相当于清空线性表
	}
}

int ListEmpty(int *list)//L你可以想象成一个容器(数组)                :线性表是否为空
{
	if(count == 0)//判断线性表是否为空,如果==0代表为空,就为true.代表是的,为空!
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

int ListLength(int *list)//返回线性表的长度(看这里,你会懂)#define KSIZE 10 int arr[KSIZE] ListLength(arr)
{
	return count;
}

int GetElem(int *list, int i, int *e)//L你可以想象成一个容器(数组) i代表索引  &e代表获取的元素是啥?             :获取线性表中指定的元素
{
	if(i < count && i >= 0)//索引必须大于或者等于0,因为索引是从零开始的。 i必须小于count,因为不能大过数组
	{
		*e = list[i];//获取的元素在索引1的位置,赋值给*e,代表*e哪里知道获取哪一个元素
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

int LocateElem(int *list, int e)//L你可以想象成一个容器(数组),e代表想从数组里面查找有没有这个e元素	   :给定元素获取第一次出现的索引位置
{
	int i = 0;
	for(i = 0; i < count; i++)//遍历线性表数组,
	{
		if(list[i] == e)//如果等于e这个元素的数组元素,就输出其索引位置.
		{
			return i;
		}
	}

	return -1;
}

/************************************************************************/
/*

   -------------------------
   | 0 | 1 | 2 | 3 | 4 |   |
   -------------------------
*/
/************************************************************************/
int ListInsert(int *list, int i, int e)//&L你可以想象成一个容器(数组)  i指定位置  e插入的元素是啥?         :将元素插入链表中指定位置
{
	if(i <= count && i >= 0)//为什么(i <= count,因为插入的位置要+1啊
	{
		int k = 0;
		for(k = count-1; k >= i; k--)
		{
			list[k+1] = list[k];//以上是所有的元素往后退一位
		}
		list[i] = e;//然后e代表list[0]了呀
		count++;//然后count++代表又满了,又的++count又扩大
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

int ListDelete(int *list, int i, int *e)//&L你可以想象成一个容器(数组)  i指定元素  &e删除的元素是啥?       :从链表中指定位置删除元素
{
	if(i < count && i >= 0)//删除不用扩大空间
	{
		int k = 0;
		if(e != NULL)//e赋值给*e代表*e是要删除的元素是啥?
		{
			*e = list[i];
		}
		
		for(k = i; k < count - 1; k++)//往前进一步
		{
			list[k] = list[k+1];
		}
		count--;//然后count--,代表删除成功了呀
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void ListTraverse(int *list)
{
	int i = 0;
	for(i = 0; i < count; i++)
	{
		printf("%d ", list[i]);//遍历元素输出即可
	}
	printf("\n");
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档