有关顺序表的知识点总结:
1顺序表的概念:
顺序表就是用一组地址连续的存储单元来存储顺序表的数据元素;
2 顺序表存储形式:
假设每个元素的存储需要L个存储单
元,起始地址为a1,那么第i个元素的地址ai=a1+(i-1)*L;
3线性表的逻辑结构:
除了第一个元素以外,每一个元素都有一个前驱,除了最后一个元素以外,每一个元素都有一个后继;
4:
线性表的基本操作:
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define ERROR 0
#define OVERFLOW 0
#define OK 1
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType* Data;
int Lenght;
}SqList;//使用typedef 可以在命名一个结构体变量时直接SqList L,如果不使用typedef则命名时必须使用struct SqList L ;
Status InitList(SqList &L)//引用 初始化线性表
{
L.Data=new int [MAXSIZE];
if(!L.Data){
return OVERFLOW;
exit(-1);
}
L.Lenght=0;
return OK;
}
Status CreatList(SqList &L,int n){//建立线性表
if(n>MAXSIZE-1){
return OVERFLOW;
}
for(int i=0;i<n;i++)
cin>>L.Data[i];
L.Lenght=n;
return OK;
}
Status InsertList (SqList&L,int i,ElemType e){//线性表元素的插入,其中i为插入的位置 ;
if(i<1||i>L.Lenght+1)
return ERROR;
else if(L.Lenght==MAXSIZE)
return OVERFLOW;
else
{
int j;
for(j=L.Lenght-1;j>=i-1;j--)//注意这里是插入的位置,而j对应的是元素的下标
L.Data[j+1]=L.Data[j];
L.Data[i-1]=e;
L.Lenght++;
return OK;
}
}
Status DeleteList(SqList&L,int i,ElemType &e)//线性表的删除,其中i为元素的位置
{
if(i<1||i>L.Lenght)
return ERROR;
else if(L.Lenght==0)
return ERROR;
else{
e=L.Data[i];
for(int j=i-1;j<=L.Lenght-1;j++)
L.Data[j-1] =L.Data[j];
L.Lenght--;
return e;
}
}
Status ClearList(SqList&L){
L.Lenght=0;
return OK;
}
Status DestroyList(SqList&L){
L.Lenght=0;
delete L.Data;
return OK;
}
Status LocateList(SqList L,ElemType e){//返回该元素的位置
int i;
for(i=0;i<L.Lenght;i++)
if(L.Data[i]==e)
return i+1;
return ERROR;
}
Status GetList (SqList L,int i,ElemType&e){//返回第i个元素的值
if(i<1||i>L.Lenght)
return ERROR;
e=L.Data[i-1] ;
return e;
}
Status Traverse (SqList L){
for(int i=0;i<L.Lenght;i++)
cout<<L.Data[i]<<" ";
}
Status NextList(SqList L,ElemType cur_e,ElemType &e){//返回cur_e元素的后继
for(int i=0;i<L.Lenght;i++){
if(L.Data[i]==cur_e&&i!=L.Lenght-1){
e=L.Data[i+1];
return e;
}
return ERROR;
}
}
Status PriorList (SqList L,ElemType cur_e,ElemType&e){
for(int i=0;i<L.Lenght;i++){
if(L.Data[i]==cur_e&&i!=0){
e=L.Data[i+1];
return e;
}
return ERROR;
}
}
Status EmptyList (SqList L){
if(L.Lenght)
return OK;
return ERROR;
}
Status LenghtList(SqList L){
return L.Lenght;
}在这里插入代码片
2020-11-14
最新推荐文章于 2024-07-25 11:43:17 发布