数据结构2——动态线性表的顺序存储

线性表:零个或多个数据元素的有限序列

线性表是一个序列,即元素之间是由顺序的,若元素存在多个,则第一个元素无前驱,最后一个元素无后继,其他每个元素有且只有一个前驱和后继(不能跳着进行存储)。其次线性表的元素个数是有限的。

线性表的顺序存储结构

线性表的顺序存储结构,指的是用一段地址连续的存储单元依此存储线性表的数据元素。

代码实现动态线性表的顺序存储

环境:VS2013

语言:c

//sqList.h文件

#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct
{
	ElemType* data;
	int length;
	int capacity;
}sqList;
//初始化顺序表
void initList(sqList* sl);
//增加容量
void addListCapacity(sqList* sl);
//判断顺序表是否为空
bool listEmpty(const sqList* sl);
//清空链表
void clearList(sqList* sl);
//定位“值”位置
int locateElem(const sqList* sl, ElemType value);
//插入
bool listInsert(sqList* sl, int loca, ElemType value);
//删除
bool listDetele(sqList* sl, int loca);
//计算链表长度
int lengthList(const sqList* sl);
//计算链表的容量
int getCapacity(const sqList* sl);
//展示链表的内容
void showList(const sqList* sl);
//并集
void listUnion(sqList* sl1, const sqList* sl2);
//交集
void listInList(sqList* sl1, const sqList* sl2);
//销毁空间
void freeList(sqList* sl);
//sqList.cpp文件

#include"sqList.h"

void initList(sqList* sl)
{
	sl->capacity = 0;
	sl->length = 0;
	sl->data = NULL;
	addListCapacity(sl);
}

void addListCapacity(sqList* sl)
{
	int newCapacity;
	if (0 == sl->capacity)
	{
		newCapacity = 1;
	}
	else
	{
		newCapacity = 2 * sl->capacity;
	}
	sl->data = (ElemType*)realloc(sl->data, newCapacity*sizeof(ElemType));
	if (NULL != sl->data)
	{
		sl->capacity = newCapacity;
	}
}

bool listEmpty(const sqList* sl)
{
	return (0==sl->length) ? true : false;
}

void clearList(sqList* sl)
{
	sl->length = 0;
}

int locateList(const sqList* sl, ElemType value)
{
	int len = sl->length, loca = -1;
	for (int i = 0; i < len; ++i)
	{
		if (value == sl->data[i])
		{
			loca = i;
		}
	}
	return loca;
}

int lengthList(const sqList* sl)
{
	return sl->length;
}

bool listInsert(sqList* sl, int loca, ElemType value)
{
	bool bRef = true;
	if (loca<0 || loca>sl->length)
	{
		bRef = false;
	}
	else
	{
		if (sl->capacity == sl->length)
		{
			addListCapacity(sl);
		}
		for (int j = sl->length-1; j >= loca; --j)
		{
			sl->data[j + 1] = sl->data[j];
		}
		sl->data[loca] = value;
		++sl->length;
	}
	return bRef;
}

bool listDetele(sqList* sl, int loca)
{
	bool bRef = true;
	if (loca < 0 || loca >= sl->length)
	{
		bRef = false;
	}
	else
	{
		int len = sl->length - 1;
		for (int j = loca; j < len; ++j)
		{
			sl->data[j] = sl->data[j + 1];
		}
		--sl->length;
	}
	return bRef;
}

int getCapacity(const sqList* sl)
{
	return sl->capacity;
}

void showList(const sqList* sl)
{
	int len = sl->length;
	for (int i = 0; i < len; ++i)
	{
		printf("%d,", sl->data[i]);
	}
	printf("\n");
}

void listUnion(sqList* sl1, const sqList* sl2)
{
	printf("并集:");
	int len = sl2->length;
	for (int i = 0; i < len; ++i)
	{
		if (locateList(sl1, sl2->data[i]) == -1)
		{
			listInsert(sl1, sl1->length, sl2->data[i]);
		}
	}
}

void listInList(sqList* sl1, const sqList* sl2)
{
	printf("交集:");
	//在这里需要注意sl1的长度是变化的
	for (int i = 0; i < sl1->length; ++i)
	{
		if (locateList(sl2, sl1->data[i]) == -1)
		{
			listDetele(sl1, i);
			//如果有元素被删除,它后面的元素就会向前移动一位
			--i;
		}
	}
}

void freeList(sqList* sl)
{
	if (NULL != sl->data)
	{
		free(sl->data);
		sl->data = NULL;
		sl->capacity = 0;
		sl->length = 0;
	}
}
//Main.cpp文件

#include "sqList.h"
int main()
{
	sqList list1,list2;
	initList(&list1);
	initList(&list2);
	for (int i = 0; i < 5; ++i)
	{
		listInsert(&list1, i, i);
		listInsert(&list2, i, i+4);
	}
	showList(&list1);
	showList(&list2);
	listUnion(&list1, &list2);
	//listInList(&list1, &list2);
	showList(&list1);
	showList(&list2);
	freeList(&list1);
	freeList(&list2);
	return 0;
}

 线性表顺序存储结构的优缺点:

优点:

(1)无须为表示中元素之间的逻辑关系而增加额外的存储空间。

(2)可以快速的存储表中的任一位置的元素

缺点:

(1)插入和删除操作需要移动大量元素。

(2)当线性表长度变化较大时,难以确定存储空间的容量。

(3)造成存储空间的“碎片”。

要解决这些不足就需要使用另一种线性表的存储结构——线性表的链式存储结构。

猜你喜欢

转载自blog.csdn.net/qq_39038983/article/details/86524413