Chapter 02 线性表(上)
https://blog.youkuaiyun.com/wang13342322203/article/details/80862382 malloc详解
2.1 线性表的基本概念
线性表是一个具有相同特性的数据元素的有限序列。
- 相同特性:所有元素属于同一数据类型。
- 有限:数据元素个数是有限的。
- 序列:数据元素由逻辑序号唯一确定。一个线性表中可以有相同值的 元素。
线性表的逻辑表示为: (a1,a2,…,ai , ai+1,…,an)
- 线性表的9个运算
- 初识化线性表InitList(&L)
- 销毁线性表DestroyList(&L)
- 判断线性表是否为空表
- 求线性表的长度
- 输出线性表
- 求线性表L中指定位置的某个数据元素
- 定位查找
- 插入数据元素
- 删除数据元素
- 线性表作用:存放数据 基本运算
2.2 线性表的顺序存储结构 顺序表
如果是输出型参数,要加&
把线性表中的所有元素按照顺序存储 方法进行存储。
-
逻辑结构:线性结构 (a1, a2, … , ai, … an)
-
物理结构:顺序存储结构
0 1 … i … n MaxSize-1 a1 a2 … ai … an … n 前面是顺序表 后面一个是长度 (最后n存放的是线性表的长度 length)
注意这里的线性表已经确定最大长度,每个线性表的长度是确定的,但是可用元素有length决定
-
注意:逻辑位序和物理位序相差1
C语言复习
- malloc 返回的是void* 所以需要 转换一下 L = (SqList*)malloc(sizeof(SqList));
- 关于结构体,(*指针变量名).成员名 变量名.成员名 指针变量名->成员名
全部代码见文件ListSequence.cpp
// 2.6 求某个元素的值
bool getElem(SqList *L,int i, ElemType &e)
{
if(i<1 || i>L->length)
{
return false;
}else{
e=L->data[i-1];
return true;
}
}
本算法的时间复杂度为O(1)
体现顺序表的随机存取特性
// 2.8 插入
bool listInsert(SqList* &L,int i, int e)
{
if(i<1 || i>L->length){
return false;
}else{
i--;
for(int j=L->length+1;j>i;j--){
L->data[j]=L->data[j-1];
}
L->data[i]=e;
L->length++;
return true;
}
}
-
最好时间复杂度:
当i=n+1时,移动次数为0,O(1)
-
最坏时间复杂度:
当i=1时,移动n次,算法最坏时间复杂度O(n)
-
平均时间复杂度:
共有n+1种情况,
在插入元素ai时,需要移动n+1-i个元素
所以求和求平均得到 移动元素的平均次数为n/2
算法平均时间复杂度为O(n)
//2.9 删除
bool listDelete(SqList* &L, int i, ElemType &e)
{
if(i<1&&i>L->length)
{
return false;
}
else{
i--;
e=L->data[i];
for(int j=i;j<L->length;j++){
L->data[j]=L->data[j+1];
}
L->length--;
return true;
}
}
-
最好时间复杂度 i=n时,移动为0 O(1)
-
最坏时间复杂度 i=1是,移动为n-1 O(n)
-
平均时间复杂度
共n种情况 平均移动(n-1)/2
O(n)
以下为全部代码
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#define MaxSize 20
using namespace std;
typedef int ElemType;
// 顺序表 定义
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
//1、 建立顺序表
void createList(SqList*&L,ElemType a[], int n)
{
L = (SqList*)malloc(sizeof(SqList));
for(int i =0; i< n;i++){
L->data[i]=a[i];
}
L->length=n;
}
// 2、顺序表基本运算算法
// 2.1 初始化线性表
// 要动态分配内存* 输出型参数&
void initList(SqList* &L)
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
// 2.2 销毁线性表
void destroyList(SqList* &L)
{
free(L);
}
// 2.3 判定是否为空表
bool listEmpty(SqList* L)
{
return (L->length==0);
}
// 2.4 输出线性表长度
int listLength(SqList* &L)
{
return L->length;
}
// 2.5 输出线性表
void dispList(SqList* &L)
{
for(int i=0;i<L->length;i++){
printf("%d ",L->data[i]);
}
printf("\n");
}
// 2.6 求某个元素的值
bool getElem(SqList *L,int i, ElemType &e)
{
if(i<1 || i>L->length)
{
return false;
}else{
e=L->data[i-1];
return true;
}
}
// 2.7 按元素值查找
int locateElem(SqList* L,int e)
{
int i=0;
while(i<L->length&&e!=L->data[i]){
i++;
}
if(i>=L->length)
return 0;
else
return i+1;
}
// 2.8 插入
bool listInsert(SqList* &L,int i, int e)
{
if(i<1 || i>L->length){
return false;
}else{
i--;
for(int j=L->length+1;j>i;j--){
L->data[j]=L->data[j-1];
}
L->data[i]=e;
L->length++;
return true;
}
}
//2.9 删除
bool listDelete(SqList* &L, int i, ElemType &e)
{
if(i<1&&i>L->length)
{
return false;
}
else{
i--;
e=L->data[i];
for(int j=i;j<L->length;j++){
L->data[j]=L->data[j+1];
}
L->length--;
return true;
}
}
int main()
{
int a[]={1,2,3};
SqList s1,s2;
SqList* L1=&s1;
SqList* L2=&s2;
createList(L1,a,3); // 这里如果直接用&s会出错 为啥呢不知道
initList(L2);
cout<<listLength(L1)<<endl;
cout<<listEmpty(L2)<<endl;
dispList(L1);
int e;
getElem(L1,2,e);
printf("%d\n",e);
printf("%d\n",locateElem(L1,e));
listInsert(L1,1,4);
dispList(L1);
listDelete(L1,2,e);
dispList(L1);
printf("%d\n",e);
destroyList(L2);
destroyList(L1);
}