线性表是最基本、最简单、也是最常用的一种数据结构。线性表(linear list)是数据结构的一种,一个线性表是n个具有相同特性的数据元素的有限序列。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部。比如,循环链表逻辑层次上也是一种线性表(存储层次上属于链式存储,但是把最后一个数据元素的尾指针指向了首位结点)。
2.线性表的操作操作一般有以下几种:
1)线性表的创建:typedef struct List{
int data[MAXSIZE];
int LineLength;
} *ListPtr;
2)打印线性表:
void outputList(ListPtr L) { //打印这个线性表
for(int i = 0; i < L->LineLength; i++ )
{
printf("%d ", L->data[i]);
}
printf("n");
}
3)线性表的赋值:(选择用数组)
ListPtr ListInit(int paraData[], int paraLength) { //将数组赋值给线性表
ListPtr resP = (List*)malloc(sizeof(List)); //申请空间
for(int i = 0; i < paraLength; i++)
{
resP->data[i] = paraData[i]; //赋值
}
resP->LineLength = paraLength; //长度赋值
printf("n");
return resP; //返回线性表
}
4)数据进行插入线性表操作:
void ListInsert(ListPtr L, int paraPostion, int paraData){ // 数据的插入, 线性表 数据的位置 数据的值
//1.先行判断空间是否已满
if(L->LineLength >= MAXSIZE)
{
printf("Cannot insert element : List is full");
return;
}
//2.再进行判断插入位置是否合法
if(paraPostion > L->LineLength || paraPostion < 0)
{
printf("Cannot insert element : Postion is bigger than LineLength or Postion is smaller than 0!n");
return;
}
//前两关过关证明可以插入数据了
//3.开始移动数据
for(int i = L->LineLength; i > paraPostion; i--)
{
L->data[i] = L->data[i-1]; //向后面移动一位来给插入数据提供空间
}
//开插!
L->data[paraPostion] = paraData;
//最后不忘线性表长度加1;
L->LineLength++;
} //搞定!
5)插入测试:
void Inserttext()
{
int i;
int array[5] = {7, 0, 4, 9, 6};
printf("---- sequentialInsertTest begins. ----rn");
//创建线性表!
ListPtr tempList = ListInit(array, 5);
printf("After initialization, the list is: ");
outputList(tempList);
//在首位插入一个元素!
printf("Now insert to the first, the list is: ");
ListInsert(tempList, 0, 5);
outputList(tempList);
//在末尾插入一个元素!
printf("Now insert to the last, the list is: ");
ListInsert(tempList, 6, 8);
outputList(tempList);
//没事找事插入一个很远或负数位置的元素
printf("Now insert beyond the tail. rn");
ListInsert(tempList, 1000, 666);
ListInsert(tempList, -6, 777);
outputList(tempList);
//这个就表演一下插入操作,一直给首位插
}
6)指定位置元素删除:
int ListDelete(ListPtr L, int paraPostion){ //删除指定位置的元素并返回删除的值, 无法找到该元素则返回-1;
//和插入元素很像
//判断postion是否合法捏
if(paraPostion > L->LineLength || paraPostion < 0)
{
printf("Cannot insert element : Postion is bigger than LineLength or Postion is smaller than 0!n");
return -1;
}
//直接保存数据
int data = L->data[paraPostion];
//直接移动数据进行覆盖
for(int i = paraPostion; i < L->LineLength; i++)
{
L->data[i] = L->data[i+1];
}
//更新length
L->LineLength--;
//返回值
return data;
}
7)删除测试:
void Deletetext()
{
int array[5] = {7, 0, 4, 9, 6};
printf("---- sequentialDeleteTest begins. ----rn");
ListPtr tempList = ListInit(array, 5);
printf("After initialization, the list is: ");
outputList(tempList);
//删除第一个元素!
printf("Now delete the first, the list is: ");
ListDelete(tempList, 0);
outputList(tempList);
//删除最后一个元素!
printf("Now delete the last, the list is: ");
ListDelete(tempList, 3);
outputList(tempList);
//再删 第四个元素
printf("Now delete the last, the list is: ");
ListDelete(tempList, 3);
outputList(tempList);
//删负
printf("Now delete the (-6)th, the list is: n");
ListDelete(tempList, -6);
outputList(tempList);
}
8)返回指定元素位置:
int locateElement(ListPtr L, int paradata){ //返回想要元素第一次出现的位置,若不存在,则返回-1;
for(int i = 0; i < L->LineLength; i++)
{
if(L->data[i] == paradata)
return i;
}
return -1;
}
9)返回目的位置的元素:
int GetElement(ListPtr L, int paraPostion){ //返回目的位置的元素
//先判断位置合不合法
if(paraPostion > L->LineLength || paraPostion < 0)
{
printf("Cannot insert element : Postion is bigger than LineLength or Postion is smaller than 0!n");
return -1;
}
//合法了就直接return不就行了?
return L->data[paraPostion];
}
10)clear操作:
void ClearList(ListPtr L) //清空线性表
{
L->LineLength=0;
return;
}
最后就是所有代码:
#include#include #define MAXSIZE 100 #define isMyFaith main//夹带私货 typedef int Kurumi; //夹带私货 typedef struct List{ int data[MAXSIZE]; int LineLength; } *ListPtr; //函数 void Inserttext(); //插入测试 void Deletetext(); //删除测试 void outputList(ListPtr L); //打印这个线性表 void outputMemory(ListPtr L); //打印一些地址啊 void ListInsert(ListPtr L, int paraPostion, int paraData); // 数据的插入, 线性表 数据的位置 数据的值 int ListDelete(ListPtr L, int paraPostion); // 删除指定位置的元素并返回删除的值, 无法找到该元素则返回-1; int locateElement(ListPtr L, int paradata); // 返回想要元素第一次出现的位置,若不存在,则返回-1; int GetElement(ListPtr L, int paraPostion); //返回目的位置的元素 ListPtr ListInit(int paraData[], int paraLength); //将数组赋值给线性表 void outputList(ListPtr L) { //打印这个线性表 int i = 0; for(i = 0; i < L->LineLength; i++ ) { printf("%d ", L->data[i]); } if(i == 0) { printf("the list is empty!"); } printf("n"); } void outputMemory(ListPtr L) { //打印一些地址啊 printf("The address of the structure: %ldrn", L); printf("The address of actualLength: %ldrn", &L->LineLength); printf("The address of data: %ldrn", &L->data); printf("The address of actual data: %ldrn", &L->data[0]); printf("The address of second data: %ldrn", &L->data[1]); } ListPtr ListInit(int paraData[], int paraLength) { //将数组赋值给线性表 ListPtr resP = (List*)malloc(sizeof(List)); //申请空间 for(int i = 0; i < paraLength; i++) { resP->data[i] = paraData[i]; //赋值 } resP->LineLength = paraLength; //长度赋值 printf("n"); return resP; //返回线性表 } void ListInsert(ListPtr L, int paraPostion, int paraData){ // 数据的插入, 线性表 数据的位置 数据的值 //1.先行判断空间是否已满 if(L->LineLength >= MAXSIZE) { printf("Cannot insert element : List is full"); return; } //2.再进行判断插入位置是否合法 if(paraPostion > L->LineLength || paraPostion < 0) { printf("Cannot insert element : Postion is bigger than LineLength or Postion is smaller than 0!n"); return; } //前两关过关证明可以插入数据了 //3.开始移动数据 for(int i = L->LineLength; i > paraPostion; i--) { L->data[i] = L->data[i-1]; //向后面移动一位来给插入数据提供空间 } //开插! L->data[paraPostion] = paraData; //最后不忘线性表长度加1; L->LineLength++; } //搞定! void Inserttext() { int i; int array[5] = {7, 0, 4, 9, 6}; printf("---- sequentialInsertTest begins. ----rn"); //创建线性表! ListPtr tempList = ListInit(array, 5); printf("After initialization, the list is: "); outputList(tempList); //在首位插入一个元素! printf("Now insert to the first, the list is: "); ListInsert(tempList, 0, 5); outputList(tempList); //在末尾插入一个元素! printf("Now insert to the last, the list is: "); ListInsert(tempList, 6, 8); outputList(tempList); //没事找事插入一个很远或负数位置的元素 printf("Now insert beyond the tail. rn"); ListInsert(tempList, 1000, 666); ListInsert(tempList, -6, 777); outputList(tempList); //这个就表演一下插入操作,一直给首位插 } int ListDelete(ListPtr L, int paraPostion){ //删除指定位置的元素并返回删除的值, 无法找到该元素则返回-1; //和插入元素很像 //判断postion是否合法捏 if(paraPostion > L->LineLength || paraPostion < 0) { printf("Cannot insert element : Postion is bigger than LineLength or Postion is smaller than 0!n"); return -1; } //直接保存数据 int data = L->data[paraPostion]; //直接移动数据进行覆盖 for(int i = paraPostion; i < L->LineLength; i++) { L->data[i] = L->data[i+1]; } //更新length L->LineLength--; //返回值 return data; } void Deletetext() { int array[5] = {7, 0, 4, 9, 6}; printf("---- sequentialDeleteTest begins. ----rn"); ListPtr tempList = ListInit(array, 5); printf("After initialization, the list is: "); outputList(tempList); //删除第一个元素! printf("Now delete the first, the list is: "); ListDelete(tempList, 0); outputList(tempList); //删除最后一个元素! printf("Now delete the last, the list is: "); ListDelete(tempList, 3); outputList(tempList); //再删 第四个元素 printf("Now delete the last, the list is: "); ListDelete(tempList, 3); outputList(tempList); //删负 printf("Now delete the (-6)th, the list is: n"); ListDelete(tempList, -6); outputList(tempList); } int locateElement(ListPtr L, int paradata){ //返回想要元素第一次出现的位置,若不存在,则返回-1; for(int i = 0; i < L->LineLength; i++) { if(L->data[i] == paradata) return i; } return -1; } int GetElement(ListPtr L, int paraPostion){ //返回目的位置的元素 //先判断位置合不合法 if(paraPostion > L->LineLength || paraPostion < 0) { printf("Cannot insert element : Postion is bigger than LineLength or Postion is smaller than 0!n"); return -1; } //合法了就直接return不就行了? return L->data[paraPostion]; } void ClearList(ListPtr L) { L->LineLength=0; return; } Kurumi isMyFaith() { Inserttext(); //测试 Deletetext(); //测试 int a[5] = {5, 7, 0, 4, 9}; ListPtr L2 = ListInit(a, 5); ClearList(L2); //清空线性表 outputList(L2); //尝试输出 ListInsert(L2, 0, 666); //插入一个 outputList(L2); return 0; }
谢谢阅览,如有错误,欢迎指出!



