线性表1:顺序表的增 删 改 查


此系列文主要用于自查、复习。所以行文、逻辑、思路是按照个人的思考方式来的,但也希冀能帮到一二三位初学者。

线性表的特点

线性表的特点:
1.表头无前驱,表尾无后继
2.其他元素有且仅有一个直接前驱和一个直接后继

线性表有顺序存储结构和链式存储结构,下面介绍顺序存储结构的顺序表。

顺序表的描述

优点: 存储密度高、按位置修改和查找比较方便
缺点: 需要连续的空间,增删比较麻烦

描述: 数组+last尾元素下标

#define MAXSIZE 100

//给表中存储的数据类型取个别名,方便换
typedef int datatype;

//给顺序表结构体类型也取个别名
typedef struct list {
	datatype data[MAXSIZE];
	int last;//记录表尾元素下标
}seqlist, *seqlist_t;

基本操作

创建空顺序表

用malloc动态申请空间

seqlist_t createSeqList()
{
	seqlist_t p = NULL;

	//1.申请空间
	p = (seqlist_t)malloc(sizeof(seqlist));
	if(NULL == p)	//malloc函数有可能会失败,需要判断一下是否失败
	{
		perror("malloc");//perror将上一个函数的错误原因输出到标准设备
		return NULL;
	}

	//2.last = -1;
	p->last = -1;	//链表刚申请,内容为空,先给尾标志赋-1

	//3.返回这片空间的地址
	return p;
}

判空

表为空返回1,不为空返回0

int isEmpty(seqlist_t L)
{
	return (L->last == -1)?1:0;
}

判满

表满返回1,不满返回0

int isFull(seqlist_t L)
{
	return ((L->last + 1) == MAXSIZE) ? 1:0;
}

在pos位增加元素,原先pos处及之后所有元素后移,这是顺序表的缺点所在,时间复杂度为O(n)

int insertSeqList(seqlist_t L, int pos, datatype x)
{
	int i;

	//表是否为满,以及位置合法不?
	if(isFull(L) || pos < 0 || pos > L->last+1)
	{
		printf("插入失败:表为满或者位置不合法\n");
		return -1;
	}

	//在pos处插入数据,pos及之后的数据皆向后移一位
	for(i = L->last; i >= pos; i--)
	{
		L->data[i+1] = L->data[i];
	}
	
	L->data[pos] = x;
	L->last++;

	return 0;
}

删除pos位,后面所有元素前移,这是顺序表的缺点所在,时间复杂度为O(n)

int deleteSeqList(seqlist_t L, int pos)
{
	int i = -1;

	//位置合法不?是否为空
	if(isEmpty(L) || pos < 0 || pos > L->last)
	{
		printf("删除失败:表为空或位置不合法\n");
		return -1;
	}

	//删除
	for(i = pos+1; i <= L->last; i++)
		L->data[i-1] = L->data[i];

	L->last--;

	return 0;
}

和改数组元素没什么区别,这是顺序表的优点

int changeSeqList(seqlist_t L, int pos, datatype x)
{
	//位置合法不?
	if(pos < 0 || pos > L->last)
		return -1;

	L->data[pos] = x;//时间复杂度为O(1)
		return 0;
}

第一种方式: 按数据元素的值查找

int searchSeqList(seqlist_t L, datatype x)
{
	int pos;
	for(pos=0; pos<=L->last; pos++)//顺序查找
	{
		if( L->data[pos] == x)
			break;
	}
   
    if(pos >last)
        return -1
        
	return pos;
}

第二种方式: 按位置查找
这种查找实在太简单,这里仅给出函数原型作为参考

datatype searchSeqList(seqlist_t L, int pos);

遍历输出

和数组遍历输出没什么区别

void showSeqList(seqlist_t L)
{
	int i;
	
	for(i = 0; i <= L->last; i++)
		printf("%d ", L->data[i]);

	printf("\n");
}

求表长

表长就是尾标志加1,这是顺序表的优点

int getLengthSeqList(seqlist_t L)
{
	if(NULL != L)
		return L->last+1;

	return -1;
}

逆序输出

void reverse(seqList_t L)
{
	int i, temp;
	for(i=0; i< L->last/2; i++)
	{
		temp = L->data[i];
		L->data[i] = L->data[L->last - i];
		L->data[L->last - i] = temp;
	}
}

排序

void order(seqList_t L)
{
	int i, j, min, temp;
	for(i=0; i<= L->last-1; i++)
	{
		min = i;

		for(j=i+1; j<= L->last; j++)
		{
			if(L->data[min] > L->data[j])
				min = j;
		}
	
		if(min != i)
		{
			temp = L->data[i];
			L->data[i] = L->data[min];
			L->data[min] = temp;
		}
	}
}

清空表

表里的数据不要了

void clearSeqlist(seqlist_t L)
{
	if(NULL != L)
		L->last = -1;
}

表中元素不必人为清除,因为last置为-1后就代表这些数据都无意义了,就把它们看作是垃圾数据就行

销毁表

这张表我都不要了

void destorySeqList(seqlist_t L)
{
	if(NULL != L)
	{
		free(L);
		L = NULL;
	}	
}

将malloc申请的这篇空间free掉,L记得赋上NULL,避免悬空

main.c //测试功能

#include"seqlist.h"

int main()
{
	seqlist_t L = createSeqList();
	if(NULL == L)
	{
		printf("创建顺序表失败\n");
		return -1;
	}

	//printf("%d\n", isEmpty(L));
	//printf("%d\n", isFull(L));

	insertSeqList(L, 0, 1);
	insertSeqList(L, 0, 2);
	insertSeqList(L, 0, 3);
	insertSeqList(L, 0, 4);
	//insertSeqList(L, 9, 5);
	//insertSeqList(L, -1, 6);
	showSeqList(L);

	printf("**************************\n");
	deleteSeqList(L, 0);
	deleteSeqList(L, 0);
	showSeqList(L);
	
	printf("**************************\n");
	changeSeqList(L, 1, 666);
	showSeqList(L);
	
	printf("**************************\n");
	int pos = searchSeqList(L, 3);
	if(-1 == pos)
		printf("NOT FOUND\n");
	else
		printf("pos = %d\n", pos+1);

#if 0
	destorySeqList(L);
#endif
    return 0;
}

拓展阅读
线性表1:顺序表的增 删 改 查
线性表2:链表的基本操作
线性表3:单向循环链表
线性表4:双向循环链表

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值