#include<iostream>
#define MAXSIZE 100
using namespace std;
typedef int ElemType;
typedef struct SeqList
{
ElemType data[MAXSIZE];
int length;
}SeqList;
SeqList SeqListInit();
void SeqListInsert(SeqList L,int i,ElemType x);
void SeqListDelete(SeqList L,int i);
int SeqListLocate(SeqList L,ElemType x);
int main()
{
SeqList L;
int p,i;
ElemType q,x;
cout<<”构造线性表中….”<<endl;
SeqListInit();
cout<<”构造完成”<<endl<<endl;
cout<<”插入位置i和元素x”<<endl;
cin>>i>>x;
p=i;
q=x;
cout<<p<<” “<<q<<endl;
SeqListInsert(L,p,q);
cout<<endl;
cout<<”删除操作”<<endl;
cin>>i;
SeqListDelete(L,p);
cout<<endl;
cout<<”按值查找”<<endl;
cin>>x;
SeqListLocate(L,q);
system(“pause”);
return 0;
}
/*********构造一个空的线性表*******/
SeqList SeqListInit()
{
SeqList L;
L.length=MAXSIZE;
return L;
}
/********在顺序表的第i个位置插入元素**********/
void SeqListInsert(SeqList L,int i,ElemType x)
{
if(L.length==MAXSIZE)
{
cout<<”表已满”<<endl;
}
if(i<=0||i>L.length+1)
{
cout<<”位置错”<<endl;
}
if(i>=1||i<=L.length)
{
for(int j=L.length-1;j>=i-1;j–)
{
L.data[j+1]=L.data[j];
L.data[i-1]=x;
L.length++;
}
}
}
/**************删除操作****************/
void SeqListDelete(SeqList L,int i)
{
if(i<1||i>L.length)
{
cout<<”位置非法”<<endl;
}
for(int j=1;j<=L.length-1;j++)
{
L.data[j-1]=L.data[j];
L.length–;
}
}
/***********按值查找***************/
int SeqListLocate(SeqList L,ElemType x)
{
int i=1;
while(i<=L.length&&L.data[i-1]!=x)
{
i++;
}
if(i<=L.length)return i;
else
{
return 0;
}
}
>> 本文固定链接: http://www.vcgood.com/archives/2380