前置:
//#include<iostream>
#include<stdlib.h>//存放exit
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXlength 100 //初始大小为100,可按需修改
typedef int Status; //函数调用状态
struct Poly
{
float p;
int e;
};
struct Sqlist
{
Poly* elem;
int length;
};
二、线性表的销毁
void DestroyList(Sqlist& L)
{
if (L.elem) delete L.elem;
}
三、线性表的清空
void CLearList(SqList &L)
{
L.length = 0;
//将线性表的长度设置为0
}
四、线性表是否为空
int IsEmpty(Sqlist L)
{
if (L.length == 0)
return true;
else
return false;
}
五、返回线性表元素个数
int Listlength(Sqlist L)
{
return L.length;
}
六、返回线性表第i个元素值
int GetElem(Sqlist L,int i,Poly ele)//element:元素
{
if (i <= 0 || i > L.length)
return ERROR;
ele = L.elem[i-1];
return OK;
}
int GetElem(Sqlist L,int i,Poly ele)//element:元素
{
if (i <= 0 || i > L.length)
return ERROR;
ele = L.elem[i-1];
return OK;
}
或者:
bool GetELem(const SqList &L, const size_t i, ElemType &e)
{
if(i<1 || i>MAXSIZE)
{
cerr<<"out of range"<<endl;
return false;
}
e = L.elem[i-1];
return true;
}
ele = L.elem[i-1]:
elem是结构体Sqlist里面的一个指针(型)变量成员,指向一个Ploy类型的目标变量
也就是说,其本身就是这个类型的对象的首地址,至于剩下的,就不用我多说了吧
如果还要我说,那就详见:C语言日记 25 指针与数组_宇 -Yu的博客-CSDN博客
这个(以上)算法的复杂度都为常量阶O(1)
顺序表可以随机存取(只要进行一次赋值操作,想要哪个都可以)这是其非常大的优点
比如链表就没有(不能实现)这样的功能
重难点算法:(算法时间复杂度:O(n))
七、查找元素
project1:
int LocateElem(Sqlist L, Poly i)
{
int a;
for (a = 0; L.elem[a] != i; a++);
cout << "元素序号: " << a << endl;
// cout<<
}
注意,我们要的,是把所有符合条件的元素筛选出来,而不是只选取第一个:
project2:
int LocateElem(Sqlist L, Poly i)
{
for (int a = 0; a <= L.length; a++)
{
if (i == L.elem[a])
return a + 1;
return 0;
}
}
可结果却显示:
为什么???
完整代码:
#include<stdlib.h>
#include<math.h>//OVERFLOW,exit
#define OK 1
struct Poly
{
float p;
int e;
};
struct Sqlist
{
Poly* elem;
int length;
};
int LocateElem(Sqlist L, Poly i)
{
for (int a = 0; a <= L.length; a++)
{
if (i == L.elem[a])
return a + 1;
return 0;
}
}
原因不在我写的操作函数里,而是在前面的类体当中;
究其根本原因,还是因为:==不能直接比较结构体,需要手撸比较的工具:
bool operator==(Poly& t)
{
return t.p == p && t.e == e;
}
project 3:
#include<math.h>//OVERFLOW,exit
#define OK 1
struct Poly
{
float p;
int e;
bool operator==(Poly& t)
{
return t.p == p && t.e == e;
}
};
struct Sqlist
{
Poly *elem;
int length;
};
int LocateElem(Sqlist L, Poly i)
{
for (int a = 0; a <= L.length; a++)
{
if (i == L.elem[a])
return a + 1;
return 0;
}
}
int main()
{
}
用while语句实现:
#include<stdlib.h>
#include<math.h>//OVERFLOW,exit
#define OK 1
struct Poly
{
float p;
int e;
bool operator==(Poly& t)
{ return t.p == p || t.e == e; }
bool operator!=(Poly& t)
{
return t.p != p && t.e != e;
}
};
struct Sqlist
{
Poly* elem;
int length;
};
int LocateElem(Sqlist L, Poly i)
{
int a = 0;
while (a <= L.length && i != L.elem[a])
a++;
if(a <= L.length)
return a + 1;
return 0;
}
int main()
{
}
八、线性表的插入
project 1:
int ListInsert(Sqlist L,int i,Poly e)
{//insert:插入//i:插入位置(位置序号)
if (i<1 || i>L.length + 1)
return ERROR;
//把插入位置后面的元素全部往后移
for (int j = L.length - 1; j >= i - 1; j--)//j为下标
L.elem[j + 1] = L.elem[j];
//放元素
L.elem[i-1] = e;
return 0;
}
记得(别忘了)写:
if (L.length == MAXlength)return ERROR;
和L.length++;
project 2:
int ListInsert(Sqlist L,int i,Poly e)
{//insert:插入//i:插入位置(位置序号)
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length == MAXlength)return ERROR;//当前储存空间已满
//把插入位置后面的元素全部往后移
for (int j = L.length - 1; j >= i - 1; j--)//j为下标
L.elem[j + 1] = L.elem[j];
//放元素
L.elem[i-1] = e;
L.length++;
return 0;
}
//#include<iostream>
#include<stdlib.h>//存放exit
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXlength 100 //初始大小为100,可按需修改
typedef int Status; //函数调用状态
struct Poly
{
float p;
int e;
};
struct Sqlist
{
Poly* elem;
int length;
};
int ListInsert(Sqlist L,int i,Poly e)
{//insert:插入//i:插入位置(位置序号)
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length == MAXlength)return ERROR;
//把插入位置后面的元素全部往后移
for (int j = L.length - 1; j >= i - 1; j--)//j为下标
L.elem[j + 1] = L.elem[j];
//放元素
L.elem[i-1] = e;
L.length++;
return 0;
}
int main()
{
}
注意:
i:位序;不是下标
j:数组下标(用于访问数据)
L.length:总的元素个数,i的最大值
九、线性表的删除
int ListDelete(Sqlist L,int i)
{
if (i < 1 || i > L.length )
return ERROR;
for (int j = i - 1; j <= L.length-1; j++)
L.elem[j] = L.elem[j + 1];
L.length--;
}
按照书上的写法:(不要忘了: return OK;)
int ListDelete(Sqlist L,int i)
{
if (i < 1 || i > L.length )
return ERROR;
for (int j = i ; j <= L.length; j++)
L.elem[j-1] = L.elem[j];
L.length--;
return OK;
}
bool EraseList(Sqlist &L, const int &i)
{
//异常判断
if(i<0 || i>L.length)
{
cerr << "wrong erase position!" << endl;
return false;
}
if(L.length == 0)
{
cerr << "List has no length" << endl;
return false;
}
//将位于删除位置之后的元素依次向前挪动一位
for (int p = i + 1; p < L.length; ++p)
{
L.elem[p - 1] = L.elem[p];
}
//线性表长度-1
L.length -= 1;
return true;
//算法时间复杂度:O(n)
}
有关于线性表的所有基本操作到此为止,完结撒花
下一节,我们开关于链表的学习