顺序表:除头尾外,所有的数据有且仅有一个前驱和一个后继。头无前驱,尾无后继。
定长顺序表:
结构:
typedef struct SQList
{
int elem[10];//存放数据,固定长度为10
int length;//有效数据的个数
}SQList,*PSQList;
顺序表的实现和操作:
cpp
#include<stdio.h>
#include<assert.h>
#include "SQList11月16.h"
//初始化
void InitSqlist(PSQList ps)
{
assert(ps != NULL);//确定传入的不是空指针
if (ps == NULL)
{
return;
}
ps->length == 10;//如果是十,则表满
}
static bool IsFul(PSQList(ps))//判断数据是否满
{
return ps->length == 10;
}
//插入数据,在PS顺序表的pose位置插入val
bool Insert(PSQList ps, int pos, int val)
{
assert(ps != NULL);//参数判断,断言ps不为空
if (ps == NULL)
{
return false;
}
if (pos<0 || pos>ps->length || IsFul(ps))//边界条件处理,pos位置判断,插入位置不能为负,插入范围不能超过边界,数据满了也不能插入
{
return false;
}
for (int i = ps->length-1; i >=pos; i--)//
{
ps->elem[i + 1] = ps->elem[i];//把数据挪到后面
}
ps->elem[pos] = val;//插入数据
ps->length++;//有效数据+1
return true;
}
//判空
bool IsEmpty(PSQList ps)
{
assert(ps != NULL);
if (ps == NULL)
{
return false;
}
return ps->length == 0;
}
//在PS中查找第一个key值,找到返回下标,没有找到返回-1
int Search(PSQList ps, int key)
{
int len;
assert(ps != NULL);
if (ps == NULL)
{
return false;
}
for (int i = 0; i < ps->length; i++)//用结构体,遍历
{
if (ps->elem[i] == key)
{
return i;
}
}
return -1;
}
//删除pos位置的值,注意边界,和插入相同,数据覆盖
bool DelPos(PSQList ps, int pos)
{
assert(ps != NULL);//保证所传数据不为空
if (ps == NULL)
{
return false;
}
if (pos<0 || pos>=ps->length || IsFul(ps))//边界问题(pose<0,pose超出范围,pose为空则查找失败)pos>pos->length!!!
{
return false;
}
for (int i = 0; i < ps->length-1; i++)//后面的数据前移,ps->length!!!
{
ps->ele