线性表
线性表的定义 :
线性表是最简单、最基本、最常用的数据结构。线性表是线性结构的抽象,线性结构的特点是结构中的数据元素之间存在一对一的线性关系。
这种一对一的关系是指数据元素之间的位置关系,即:
1、除第一个位置的数据元素之外,其他数据元素搁置的前面都只有一个数据元素;
2、除最后一个位的置数据元素外, 其他 数据元素位置的后面都只有一个元素。
也就是说,数据元素是一个接一个地排列的。因此,可以把线性表想象为一个数据元素序列的数据结构。
线性表(List)是由(n >= 0)个相同类型的数据元素构成的有限序列。对于这个定义应该注重两个概念:
1、“有限”,它是指线性表中的数据元素个数是有限的,线性表中的每一个数据元素都有自己的位置
2、“相同类型”,它是指线性表中的数据元素都属于同一种类型
线性表基本操作
1、创建一个空表
2、插入数据
头部插入
中间插入
尾部插入
3、删除数据
头部删除
中间删除
尾部删除
4、查找数据
按位置查找
按值插找
5、清空线性表、销毁线性表
6、判断是否为空
7、遍历
8、线性表的逆序
9、线性表的无序合并
10、线性表的有序合并
/*
1 、建立线性表
初始条件:一个初始化线性表
*/
List* List_Init() //创建一个空表
{
List* L= (List*)malloc(sizeof(List));
L->element = (int *)malloc(sizeof(int));
if(!L->element)
return NULL;
L->Length = 0;
L->ListSize = LIST_INIT_SIZE;
return L;
}
/*
2、插入数据
1、从头部插入
2、从中间插入
3、从尾部插入
如果空间不够可以用realloc扩展空间
void* realloc(void*ptr,size_t new_size);
1、更改由ptr指向的内存块的大小。
2、该函数可能会将内存块移到新位置(其地址由函数返回)
3、即使将块移动到新位置,内存块的内容也会保留为新旧尺寸中较小的一个。如果新尺寸较大,则新分配的部分的值不确定
4、如果ptr是一个空指针,该函数的行为就像的malloc,分配一个大小为字节的新块并返回一个指向其开始的指针
*/
void List_Insert_head(List* L, ElementType e) //插入数据 头部插入
{
if(L == NULL)
return ;
if(L->Length + 1 >= L->ListSize){
//如果空间和够可以用realloc扩展空间realloc()
int* newBase = (int*)realloc(L->element, L->ListSize + LISTINCREMENT*sizeof(int));
if(!newBase)
cout << "失败" << endl;
L->element = newBase;
L->ListSize += LISTINCREMENT;
}
for(int i = L->Length; i >= 1; i--)
L->element[i + 1] = L->element[i];
L->element[1] = e;
++L->Length;
}
void List_Insert_Last(List* L, ElementType e) // 尾部插入
{
if(L == NULL)
return ;
if(L->Length + 1 >= L->ListSize){
//如果空间和够可以用realloc扩展空间realloc()
int* newBase = (int*)realloc(L->element, L->ListSize + LISTINCREMENT*sizeof(int));
if(!newBase)
cout << "失败" << endl;
L->element = newBase;
L->ListSize += LISTINCREMENT;
}
L->element[L->Length + 1] = e;
++L->Length;
}
void List_Insert_midlle(List* L, ElementType e, int i) // 中间插入
{
if(i < 1 || i > L->Length)
return ;
if(L == NULL)
return ;
if(L->Length >= L->ListSize){
//如果空间和够可以用realloc扩展空间realloc()
int* newBase = (int*)realloc(L->element, L->ListSize + LISTINCREMENT*sizeof(int));
if(!newBase)
cout << "失败" << endl;
L->element = newBase;
L->ListSize += LISTINCREMENT;
}
for(int j = L->Length; j >= i; j--)
L->element[j + 1] = L->element[j];
L->element[i] = e;
++L->Length;
}
/*
3、删除数据
1、从头部删除
2、从中间删除
3、从尾部删除
*/
void List_Delete_head(List* L) // 头部删除
{
if(L == NULL)
return ;
for(int i = 2; i <= L->Length; i++)
L->element[i - 1] = L->element[i];
--L->Length;
}
void List_Delete_Last(List* L) // 尾部删除
{
if(L == NULL)
return ;
--L->Length;
}
void List_Delete_midlle(List* L, int i) // 中间删除
{
if(i < 1 || i > L->Length)
return ;
if(L == NULL)
return ;
for(int j = i + 1; j <= L->Length; j++)
L->element[j - 1] = L->element[j];
--L->Length;
}
/*
4、查找数据
1、按位置查找
2、按值查找
*/
int List_Find_seat(List* L, int i) //按位置查找
{
if(L->element == NULL)
return 0;
for(int j = 0; j <= L->Length; j++){
if(j == i)
return L->element[j];
}
return -1;
}
int List_Find_reult(List* L, int e) //按值查找
{
if(L->element == NULL)
return 0;
for(int j = 1; j <= L->Length; j++){
if(L->element[j] == e)
return L->element[j];
}
return -1;
}
// 1、清空并销毁线性表
void DestroyList(List* L)
{
L->Length = 0;
L->ListSize = 0;
L->element = NULL;
free(L->element);
}
// 1、判断表是否为空
int IsEmpty(List* L)
{
if(L->element == NULL)
return 0;
else
return -1;
}
// 遍历线性表
void List_Print(List* L)
{
if(L == NULL)
return ;
for(int i = 1; i <= L->Length; i++)
cout << L->element[i] << " ";
}
// 线性表的逆序
void List_Reverse(List *L)
{
for(int i = 1; i <= L->Length / 2; i++){
int tmp = L->element[i];
L->element[i] = L->element[L->Length - i + 1];
L->element[L->Length - i + 1] = tmp;
}
}
// 线性表的无序排序,直接把一个表中的数据添加到另一个表中
void List_Wuxu(List* L1, List* L2)
{
for(int i = 1; i <= L2->Length; i++){
if(L1->Length >= L1->ListSize){
int* newBase = (int*)realloc(L1->element, L1->ListSize + LISTINCREMENT*sizeof(int));
if(!newBase)
cout << "失败" << endl;
L1->element = newBase;
L1->ListSize += LISTINCREMENT;
}
L1->element[L1->Length + 1] = L2->element[i];
L1->Length++;
}
}
//线性表的有序排序,定义一个新的线性表,按顺序把添加到新的线性表中
List* List_Youxu(List* L1, List* L2)
{
List* NewP;
int i = 1, j = 1;
NewP = List_Init();
List* head = NewP;
while(i <= L1->Length && j <= L2->Length){
if(NewP->Length >= NewP->ListSize){
int* newBase = (int*)realloc(NewP->element, NewP->ListSize + LISTINCREMENT*sizeof(int));
if(!newBase)
cout << "失败" << endl;
NewP->element = newBase;
NewP->ListSize += LISTINCREMENT;
}
if(L1->element[i] <= L2->element[j]){
NewP->element[++NewP->Length] = L1->element[i++];
}else{
NewP->element[++NewP->Length] = L2->element[j++];
}
}
while(i <= L1->Length)
NewP->element[++NewP->Length] = L1->element[i++];
while(j <= L2->Length)
NewP->element[++NewP->Length] = L2->element[j++];
return head;
}