#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 LocateElem_Sq(Sqlist L,ElemType e)//顺序表的查找
{
for(int i=0;i<L.lenth;i++)
if(L.elem[i]==e)
return i+1;
return 0;
}
bool InsertList_Sq(Sqlist &L,ElemType e,int i)//顺序表的插入
{
if(i<1||i>L.lenth)
return ERROR;
for(int j=L.lenth;j>=i;j--)
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
L.lenth++;
return OK;
}
bool DeleteList_Sq(Sqlist &L,int i,ElemType &e)//顺序表的删除
{
if(i<1||i>L.lenth)
return ERROR;
e=L.elem[i-1];
for(int j=i-1;j<L.lenth;j++)
L.elem[j]=L.elem[j+1];
L.lenth--;
return OK;
}
Status Init(Linklist &l
线性表的基本实现与操作
最新推荐文章于 2022-06-02 11:51:42 发布
该博客介绍了线性表的基本操作,包括顺序表和链表的初始化、输入数据、查找、插入、删除等。通过C++代码展示了如何实现这些操作,并提供了用户交互式的操作选项。
摘要由CSDN通过智能技术生成