线性表的顺序存储及操作实现

typedef int ElemType;
typedef struct
{
 ElemType *list;
 int len;
 int MaxSize;
}ListSq ;
/* 1.初始化线性表,分两种情况,一种不需分配空间,一种为其分配空间*/
void InitList(ListSq &L)
{
 L.list=NULL;
 L.len=L.MaxSize=0;

}
void InitList(ListSq &L,int ms)
{
 if (ms<0)
 {
  cout<<"ms值非法"<<endl; exit(1);
 }
 L.MaxSize=ms;
 L.list=new ElemType[L.MaxSize];
 if (L.list==NULL)
 {
  cout<<"内存分配失败"<<endl;exit(1);
 }
 L.len=0;
}

/* 2.得到线性表中第pos个位置元素的值*/
ElemType GetElemList(ListSq &L,int pos)
{
 if (pos<0||pos>L.len)
 {
  cout<<"所给下标无效"<<endl;exit(1);
 }
 return L.list[pos];
 

}

/* 3.从线性表中查找一个元素,返回该元素的位置,查找失败返回-1*/
/*顺序查找,算法复杂度为O(n)*/
int FindList(ListSq &L,ElemType item)
{
 int i;
 for (i=0;i<L.len;i++)
 {
  if (item==L.list[i])
  {
   return i;
  }
 }
 return -1;
}

/*二分查找,针对线性表中元素是有序的,即是按照每一关键字升序或者降序排列 查找不成功返回-1 算法复杂度O(logn)*/

int BinaryFindList(ListSq &L,ElemType item)
{
 int low=0;
 int high=L.len-1;
 
 while (low<=high)
 {
  int mid=(low+high)/2;
  if (item==L.list[mid])
  {
   return mid;
  }
  else if (item<L.list[mid])
  {
   high=mid-1;
  }
  else low=mid+1;
 }
 return -1;

}


/* 4.修改线性表中的第一个指定元素*/
bool ModifyList(ListSq &L,const ElemType & item )
{
 int i;
 for (i=0;i<L.len;i++)
 {
  if (item==L.list[i])
  {
   L.list[i]=item;
   return true;
  }
 }
 return false;
}

/* 5.向线性表中插入一个元素 分两种情况,一种是插入到指定位置,另一种是插入后还是保持原来的有序性*/
void InsertList(ListSq &L,ElemType item,int pos)
{
 int i;
 if (pos<1||pos>L.len)
 {
  cout<<"所给插入元素的位置编号无效";
  exit(1);
 }
 if (L.len==L.MaxSize)
 {
  ElemType *p=new ElemType[2*L.MaxSize+1];
  if (p==NULL)
  {
   cout<<"内存非配失败"<<endl;exit(1);
  }
  for (i=0;i<L.len;i++)
  {
   p[i]=L.list[i];
  }
  delete []L.list;
  L.list=p;
  L.MaxSize=2*L.MaxSize+1;

 }
 for (i=L.len;i>=pos;i--)
 {
  L.list[i]=L.list[i-1];
 }
 L.list[pos-1]=item;
 L.len++;


}

/*有序插入,即在有序线性表插入*/
void OrderInsertList(ListSq &L,ElemType item)
{
 int i,j;
 if (L.len==L.MaxSize)
 {
  ElemType *p=new ElemType[2*L.MaxSize+1];
  if(p==NULL)
  {
   cout<<"内存分配失败"<<endl;exit(1);
  }
  for (i=0;i<L.len;i++)
  {
   p[i]=L.list[i];
  }
  delete []L.list;
  L.list=p;
  L.MaxSize=2*L.MaxSize+1;

 }
 for (i=0;i<L.len-1;i++)
 {
  if (item<L.list[i])
  {
   break;
  }
 }
 for(j=L.len;j>=i+1;j--)
 {
  L.list[j]=L.list[j-1];
 }
 L.list[i]=item;
 L.len++;
}

/* 6.从线性表中删除满足条件的第一个元素*/
bool DeleteList(ListSq & L,ElemType item)
{
 int i,j;
 for (i=0;i<L.len;i++)
 {
  if (item==L.list[i])
  {
   break;
  }
 }
 if (i==L.len)return false;
 else
 {
  for (j=i;j<L.len;j++)
  {
   L.list[j]=L.list[j+1];
  }
  L.len--;
  return true;
 }
}
/* 7.判断线性表是否为空*/
bool EmptyList(ListSq &L)
{
 return L.len==0;
}

/* 8.求出线性表长度*/
int LenthList(ListSq &L)
{
 return L.len;
}

/* 9.遍历出线性表*/
void OutputList(ListSq &L)
{
 for (int i=0;i<L.len;i++)
 {
  cout<<L.list[i]<<' ';
 }
 cout<<endl;
}
/* 10.清除线性表中的所有元素,释放占有的动态内存空间*/
void ClearList(ListSq &L)
{
 if (L.list!=NULL)
 {
  delete []L.list;
  L.list=NULL;
  L.len=L.MaxSize=0;
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值