顺序表
01 基本概念
-
定义
线性表中元素按顺序放在连续的存储空间
-
存储结构
顺序存储
-
特点
逻辑相邻的元素物理次序也相邻
元素地址计算公式: l o c ( a i ) = l o c ( a 0 ) + ( i − 1 ) × l loc(a_i) = loc(a_0) + (i - 1) \times l loc(ai)=loc(a0)+(i−1)×l
a i a_i ai表示第 i i i个元素, a 0 a_0 a0表示第一个元素, l l l表示一个元素的存储长度
Ex.1
设顺序表的首地址为1000,每个元素长度为2个字节,计算第5个元素的地址。
l o c ( a 5 ) = l o c ( a 0 ) + ( i − 1 ) × l = 1000 + 2 × ( 5 − 1 ) = 1008 loc(a_5) = loc(a_0) + (i-1) \times l = 1000 + 2 \times (5 - 1) = 1008 loc(a5)=loc(a0)+(i−1)×l=1000+2×(5−1)=1008
02 类型定义
用结构体对其进行封装
#define LIST_MAX_SIZE 100 //最大允许长度
typedef int DataType;
typedef struct{
DataType data[LIST_MAX_SIZE];//存储空间基地址
int length;//顺序表内元素个数,length = 0 时为空
}SeqList;
SeqList l;//创建顺序表
03 基本操作
/*
num : 1 2 3 4 5 6 7 8
Addr: 0 1 2 3 4 5 6 7
Val :11 13 16 77 65 90 98 34
*/
l.data[n] //访问第n+1个元素,l.data[4] ---> 65
l.length //当前表长(即表内含有元素个数),当前为8
l.data[l.length] //顺序表的下一个空位置,当前为l.data[8]
04 基本运算
4.1 初始化
void SeqListInit(SeqList *l){
l->length = 0;
}
/* 需要用指针来进行值传递,没有只能是参数传递,无法改变l初始化的长度*/
4.2 插入
//插入
//在位置pos之后插入,成功返回1,失败返回0
unsigned char Insert(SeqList *l,DataType x,int pos){
if(l->length == LIST_MAX_SIZE){ //表已经满了,没有空位
printf("Error: There have no enough space to insert the new param!\n");
return 0;
}
if(pos < 0 || pos > l->length){ //位置出错
printf("Error: the pos is error!\n");
return 0;
}
unsigned char i;
for (i = l->length; i > pos;i--){
l->data[i] = l->data[i - 1];
}
l->data[i] = x;
l->length++;
return 1;
}
4.3 删除
//删除
//成功返回1,失败返回0
int Delete(SeqList *l,DataType x){
int pos = FindVal(*l, x);
if(pos != -1){
unsigned char i;
for (i = pos; i < l->length;i++){
l->data[i] = l->data[i + 1];
}
l->length--;
return 1;
}
else
return 0;
}
4.4 查找
-
按值查找
//按值查找 //不存在返回-1,存在则返回其地址 int FindVal(const SeqList l,DataType tag){ int reVal = -1; if(l.length == 0){ //空表 printf("Error: The List is empty!\n"); exit(0); }else{ unsigned char i = 0; for (i = 0; i < l.length;++i){ if(l.data[i] == tag) return i; } if(i == l.length){ //已经遍历完整张表,不存在该值 printf("Error: the elment is not in this list!\n"); } } return reVal; }
-
按位置查找
直接输出即可,例如
l.data[pos - 1]
,pos表示位置,地址是从0开始的,所以pos要减一
4.5 取值
-
取该位置本值
DataType GetData(const SeqList l,int i){ if(i < 1 && i > l.length){ //超出表的范围 printf("Error: param i is not right!\n"); return 0; //如果数据类型是自定义类型,就返回null }else{ return l.data[i - 1]; //如果是返回某个位置的数据,则不需减一 } }
-
取前驱
DataType GetDataPre(const SeqList l,DataType x){ int pos = FindVal(l, x); if(pos > 0){ //既不是首元素,也存在于该表中 return l.data[pos - 1]; } else{ printf("Error: the pos is not right!\n"); return 0; } }
-
取后驱
DataType GetDataNxt(const SeqList l,DataType x){ int pos = FindVal(l, x); if(pos < l.length && pos != -1){ //既不是末尾元素,也存在于表中 return l.data[pos + 1]; } else{ printf("Error: the pos is not right!\n"); return 0; } }
4.6 求长度
//求长度
//int类型,返回表长
int Length(const SeqList l){
return l.length;
}
05 整体代码参考
-
运行结果如下
-
查找、删除、插入等涉及元素修改时,依据要确定好,不要一时靠位置,一时靠值,会导致程序有些混乱
-
代码参考(大部分按值)
#include<stdio.h> #include<stdlib.h> #define LIST_MAX_SIZE 100 typedef int DataType; typedef struct{ DataType data[LIST_MAX_SIZE]; int length; } SeqList; //初始化 void SeqListInit(SeqList *l){ l->length = 0; } //按值查找 //不存在返回-1,存在则返回其地址 int FindVal(const SeqList l,DataType tag){ int reVal = -1; if(l.length == 0){ //空表 printf("Error: The List is empty!\n"); exit(0); }else{ unsigned char i = 0; for (i = 0; i < l.length;++i){ if(l.data[i] == tag) return i; } if(i == l.length){ //已经遍历完整张表,不存在该元素 printf("Error: the elment is not in this list!\n"); } } return reVal; } //求长度 //int类型,返回表长 int Length(const SeqList l){ return l.length; } //提取第i个元�? //当数据类型为自定义类型时更能体现该函数优越性 DataType GetData(const SeqList l,int i){ if(i < 1 && i > l.length){ //超出表的范围 printf("Error: param i is not right!\n"); return 0; //如果数据类型是自定义类型,就返回null }else{ return l.data[i - 1]; //如果是返回某个位置的数据,则不需减一 } } DataType GetDataPre(const SeqList l,DataType x){ int pos = FindVal(l, x); if(pos > 0){ //既不是首元素,也存在于该表中 return l.data[pos - 1]; } else{ printf("Error: the pos is not right!\n"); return 0; } } DataType GetDataNxt(const SeqList l,DataType x){ int pos = FindVal(l, x); if(pos < l.length && pos != -1){ //既不是末尾元素,也存在于表中 return l.data[pos + 1]; } else{ printf("Error: the pos is not right!\n"); return 0; } } //插入 //在位置pos之后插入,成功返回1,失败返回0 unsigned char Insert(SeqList *l,DataType x,int pos){ if(l->length == LIST_MAX_SIZE){ //表已经满了,没有空位 printf("Error: There have no enough space to insert the new param!\n"); return 0; } if(pos < 0 || pos > l->length){ //位置出错 printf("Error: the pos is error!\n"); return 0; } unsigned char i; for (i = l->length; i > pos;i--){ l->data[i] = l->data[i - 1]; } l->data[i] = x; l->length++; return 1; } //删除 //成功返回1,失败返回0 int Delete(SeqList *l,DataType x){ int pos = FindVal(*l, x); if(pos != -1){ unsigned char i; for (i = pos; i < l->length;i++){ l->data[i] = l->data[i + 1]; } l->length--; return 1; } else return 0; } void Print(const SeqList l){ unsigned char i; for (i = 0; i < l.length;i++){ printf("%d ", l.data[i]); } printf("\n"); } int main(){ SeqList l; //初始化 SeqListInit(&l); unsigned char i; for (i = 0; i < 5;++i){ Insert(&l, i * 2, i); } Print(l); printf("Length:%d\n", l.length); printf("Delete param 5:"); Delete(&l, 5); printf("Delete param 6:"); Delete(&l, 6); Print(l); printf("Cehck for param 4:"); if(FindVal(l, 4) != -1){ printf("Pos:%d\n", FindVal(l, 4)); } printf("Get the third param:"); printf("%d\n", GetData(l, 3)); printf("Get the pre of third param:"); printf("%d\n", GetDataPre(l, 4)); printf("Get the next of third param:"); printf("%d\n", GetDataNxt(l, 4)); return 0; }