动态顺序表~DS笔记③

结构体:

typedef int ElemType;  	/*ElemType类型根据情况而定,这里为int*/
typedef struct
{
 ElemType* data;   	/*线性表数据起始地址*/
 int length;    	/*线性表当前长度*/
 int capacity;  	/*线性表容量*/
}SqList;

函数声明:

void ListAddCapacity(SqList* sl)//添加“容量”
bool ListEmpty(const SqList* sl)//是否为空
int ListLength(const SqList* sl)//返回长度
int ListCapacity(const SqList* sl)//返回容量
int LocateElem(const SqList* sl, ElemType e)//查找
bool ListInsert(SqList* sl, int i, ElemType e)//插入
bool ListDelete(SqList* sl, int i)//删除
void ListDeleteAug(SqList* sl, ElemType e)//删除多个
void ShowList(const SqList*sl)//输出
void Union(SqList* La, SqList* Lb)//合并

扩容(ListAddCapacity):

void ListAddCapacity(SqList* sl)
{
 int newcapacity;		//新容量
 if (sl->capacity == 0)
 {
  newcapacity = 1;		//如果当前容量为空,新容量置为一
  sl->data = (ElemType*)malloc(newcapacity * sizeof(ElemType));
 }
 else
 {				//新容量扩为原来二倍
  newcapacity = 2 * sl->capacity;
  sl->data = (ElemType*)realloc(sl->data, newcapacity * sizeof(ElemType));
 }
 sl->capacity = newcapacity;
}

是否空(ListEmpty):

bool ListEmpty(const SqList* sl)
{
 return sl->length ? false : true;
}

求长度(ListLength):

int ListLength(const SqList* sl)
{
 return sl->length;
}

求容量(ListLength):

int ListCapacity(const SqList* sl)
{
 return sl->capacity;
}

查找(LocateElem):

int LocateElem(const SqList* sl, ElemType e)
{
 for (int i = 0; i < sl->length; ++i)
 {
  if (sl->data[i] == e)
  {
   return i;		//若找到则返回该下标
  }
 }
 return -1;		//找不到返回负一
}

插入(ListInsert):

bool ListInsert(SqList* sl, int i, ElemType e)
{
 if (i < 0 || i > sl->length)
  return false;				//前插,插入位置不得小于0,不得大于长度
 if (sl->length == sl->capacity)
  ListAddCapacity(sl);			//若容量满,则调用扩容函数
 for (int j = sl->length - 1; j >= i; j--)
 {
  sl->data[j + 1] = sl->data[j];	//插入位之后元素依次后移
 }
 sl->data[i] = e;
 sl->length += 1;			//长度加一
 return true;
}

删除(ListDelete):

bool ListDelete(SqList* sl, int i)
{
 bool bret = true;
 if (i < 0 || i >= sl->length)	//删除位不得小于0,不得大于长度
  bret = false;
 else
 {
  for (int j = i; j < sl->length; ++j)
  {
   sl->data[j] = sl->data[j + 1];//删除位之后元素依次前移
  }
  sl->length -= 1;		//长度减一
 }
 return bret;
}

删除多个(ListDeleteAug):

void ListDeleteAug(SqList* sl, ElemType e)
{
 int i , j;
 for (i = 0, j = 0; i < sl->length; ++i, ++j)
 {				//i,j初值为0
  while (e == sl->data[j])
  {
   ++j;				//若遇到要删元素j++
  }
  sl->data[i] = sl->data[j]; 	//每次都把j的值赋给i
 }
 sl->length -= (j - i);		//最后长度减去删掉个数
}

合并(Union):

void Union(SqList* La, SqList* Lb)
{
 int Lalen, Lblen, i;
 ElemType e;
 Lalen = ListLength(La);	//求线性表的长度
 Lblen = ListLength(Lb);
 for (i = 0; i < Lblen; i++)
 {
  e = Lb->data[i];
  if (LocateElem(La, e) == -1)
  {				//发现该元素a中没有,则插入
   ListInsert(La, La->length, e);
  }
 }
}

输出(ShowList):

void ShowList(const SqList*sl)
{
 for (int i = 0; i < sl->length; ++i)
 {
  printf("%d,", sl->data[i]);
 }
 puts("\b;");
}

线性表
顺序存储结构:

  • 静态存储
  • 动态存储

链式存储结构:

  • 单链表
  • 双向链表
  • 循环链表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值