#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define MAXSIZE 100
typedef struct
{
ElemType *elem;
int lenth;
}Sqlist;
typedef struct Lnode
{
ElemType data;
Lnode *next;
}*Linklist;
Status InitList_Sq(Sqlist &L)//有序表的初始化
{
L.elem=new ElemType[MAXSIZE];
if(!L.elem)
exit(OVERFLOW);
L.lenth=0;
return OK;
}
void InputList_Sq(Sqlist &L)//顺序表输入数据
{
int n;
cout<<"请输入要输入的元素个数:";
cin>>n;
cout<<"请输入每个元素的值:";
for(int i=0;i<n;i++)
cin>>L.elem[i];
L.lenth=n;
cout<<"输入成功!"<<endl<<endl;
}
Status Erfen(Sqlist L,ElemType e)//二分查找要输入或要删除的元素下标时间复杂度为O(logn);
{
int mid,l=0,r=L.lenth-1;
while(l<r)
{
mid=(l+r)/2;
if(L.elem[mid]>e)
r=mid-1;
else
l=mid+1;
}
return l;
}
bool InsertList_Sq(Sqlist &L,ElemType e)//顺序表的插入
{
int i=Erfen(L,e);
if(L.elem[i]<e)
i++;
for(int j=L.lent
有序线性表的基本操作及实现
最新推荐文章于 2022-04-03 14:22:05 发布