顺序表的定义
线性表的顺序存储又称顺序表。它是用一组地址连续的存储单元依次存储线性表中的数据元素,使得逻辑上相邻的两个元素在物理位置上也相邻。
顺序表的特点:
- 随机访问。通过首地址和元素序号可在时间O(1)内找到指定元素。
- 存储密度高。每个结点只存储数据元素。
- 逻辑上相邻的元素在物理上也相邻,故插入和删除操作需要移动大量元素。
顺序表上的操作
在顺序表上的操作主要有:
- 初始化Init(&L)
- 求表长Length(L)
- 遍历表PrintList(L)
- 插入Insert(&L,i,e)
- 按值查找LocateElem(L,x)
- 按位查找GetElem(L,i)
- 删除操作Delete(&L,i,&x)
- 判空操作Empty(L)
#define MaxSize 100 //定义线性表的最大长度
typedef struct Sqlist{
int data[MaxSize];
int length;
}Sqlist;
初始化
//初始化
void Init(Sqlist &L){
L.length = 0;
}
求表长
int Length(Sqlist L){
return L.length;
}
遍历整个表
//遍历操作
void PrintList(Sqlist L){
for(int i=0; i<L.length; i++){
cout<<L.data[i]<<" ";
}
cout<<endl;
}
插入操作
//插入操作:在表中第i个位置上插入x,其中1<=i<=L.length+1
void Insert(Sqlist &L, int i, int x){
if(i<1 || i>L.length+1 || L.length >= MaxSize){
cout<<x<<" Insert failed."<<endl;
return;
}
for(int j=L.length; j>=i; j--){ //第i个及以后的元素后移
L.data[j] = L.data[j-1];
}
L.data[i-1]=x;
L.length++;
}
查找操作
查找操作一般有两种方式:按值查找和按位查找,需要注意的是顺序表中元素的位序是从1开始的,而数组中元素的下标是从0开始的。
按值查找
//按值查找:返回顺序表L中第一个值为x的元素的位置
int LocateElem(Sqlist L, int x){
for(int i=0; i<L.length; i++){
if(L.data[i] == x)
return i+1; //返回元素位置
}
return -1; //查找失败,返回-1
}
按位查找
//按位查找:返回顺序表中第i个元素的元素值
int GetElem(Sqlist L, int i){
return L.data[i-1];
}
删除操作
//删除操作:删除顺序表L中第i个元素并返回其元素值
int Delete(Sqlist &L, int i, int &x){
if(i<1 || i>L.length){
return -1;
}else{
x = L.data[i-1];
for(int j=i; j<L.length; j++){
L.data[j-1] = L.data[j];
}
L.length--;
return x;
}
}
判空操作
//判空操作
int Empty(Sqlist L){
return L.length==0? 1 : 0;
}
完整代码及实例
#include<bits/stdc++.h>
using namespace std;
#define MaxSize 100 //定义线性表的最大长度
typedef struct Sqlist{
int data[MaxSize];
int length;
}Sqlist;
//初始化
void Init(Sqlist &L){
L.length = 0;
}
//求表长
int Length(Sqlist L){
return L.length;
}
//插入操作:在表中第i个位置上插入x
void Insert(Sqlist &L, int i, int x){
if(i<1 || i>L.length+1 || L.length >= MaxSize){
cout<<x<<" Insert failed."<<endl;
return;
}
for(int j=L.length; j>=i; j--){ //第i个及以后的元素后移
L.data[j] = L.data[j-1];
}
L.data[i-1]=x;
L.length++;
}
//遍历操作
void PrintList(Sqlist L){
cout<<"L: ";
for(int i=0; i<L.length; i++){
cout<<L.data[i]<<" ";
}
cout<<endl;
}
//按值查找:返回顺序表L中第一个值为x的元素的位置
int LocateElem(Sqlist L, int x){
for(int i=0; i<L.length; i++){
if(L.data[i] == x)
return i+1; //返回元素位置
}
return -1; //查找失败,返回-1
}
//按位查找:返回顺序表中第i个元素的元素值
int GetElem(Sqlist L, int i){
return L.data[i-1];
}
//删除操作:删除顺序表L中第i个元素并返回其元素值
int Delete(Sqlist &L, int i, int &x){
if(i<1 || i>L.length){
return -1;
}else{
x = L.data[i-1];
for(int j=i; j<L.length; j++){
L.data[j-1] = L.data[j];
}
L.length--;
return x;
}
}
//判空操作
int Empty(Sqlist L){
return L.length==0? 1 : 0;
}
int main(){
Sqlist L;
Init(L);
Insert(L, 1, 50);
Insert(L, 2, 60);
Insert(L, 1, 40);
Insert(L, 1, 666);
Insert(L, 5, 70);
Insert(L, 6, 40);
Insert(L, 7, 100);
cout<<"长度:"<<Length(L)<<endl;
PrintList(L);
int x;
cout<<"第三个元素是:"<<GetElem(L,3)<<endl;
cout<<"40在第"<<LocateElem(L,40)<<"位"<<endl;
cout<<"删除"<<Delete(L,6,x)<<endl;
PrintList(L);
if(!Empty(L)){
cout<<"L不为空"<<endl;
}else{
cout<<"L为空"<<endl;
}
return 0;
}
运行结果: