关于数据结构中顺序表的问题

1.建立顺序表
2.在表中插入元素
3.在表中查找元素
4.在表中删除元素
注:要建表、插入、查找、删除分开的,能在VC下运行并带注释的。
急急...

把这个直接粘到VC里运行, 看一下就知道~

#include <iostream.h>
#include<stdlib.h>
#define MAX 100
typedef int datatype;
typedef struct List
{
datatype elem[MAX];
int Last;
}*SeqList;
SeqList InitList() //初始化顺序表
{
SeqList L;
L=(SeqList)malloc(sizeof(List));
L->Last=-1;
return L;
}
void CreateList(SeqList L) //创建顺序表
{
int n;
cout<<"请输入你要创建的顺序表元素个数n= ";
cin>>n;
cout<<"请输入你要创建的顺序表:";
for(int i=0;i<n;i++)
{
cin>>L->elem[i];
L->Last++;
}
}
int Location(SeqList L,datatype x) //查找某元素所在位置
{
int i=0;
while(L->elem[i]!=x&&i<=L->Last)
{
i++;
}
if(i>L->Last)
return -1;
else
return i;
}
void Insertelem(SeqList L,datatype m) //插入元素
{
int n;
cout<<"请输入你要插入的位置n=";
cin>>n;
if((L->Last+1)>MAX)
cout<<"表以满,能插入"<<endl;
else
{
L->Last++;
for(int i=L->Last;i>=n-1;i--)
{
L->elem[i+1]=L->elem[i];
}
L->elem[n-1]=m;
}
}
void Deleteelem(SeqList L,datatype m) //删除表中某元素
{
int i;

i=Location(L,m);
while(i==-1)
{
datatype n;
cout<<"你所查找的元素不在表中,请重新输入你要删除的元素"<<endl;
cin>>n;
i=Location(L,n);
}
for(int j=i;j<=L->Last;j++)
{
L->elem[i]=L->elem[i+1];
}
L->Last--;
}
void ShowList(SeqList L) //显示当前顺序表
{
cout<<"当前顺序表元素为:";
for(int i=0;i<=L->Last;i++)
{
cout<<L->elem[i]<<" ";
}
cout<<endl;
}
void main()
{
SeqList L;
L=InitList();
CreateList(L);
int Opration;
cout<<"输入操作(1)为删除某元素(2)为插入(3)为查找(4)为输出当前顺序表(5)为退出"<<endl;
while(Opration!=5)
{
cin>>Opration;
if(Opration==1)
{
int n;
cout<<"请输入你要删除的元素n=";
cin>>n;
Deleteelem(L,n);
}
if(Opration==2)
{
int n;
cout<<"请输入你要插入的元素n=";
cin>>n;
Insertelem(L,n);
}
if(Opration==3)
{
datatype x;
cout<<"请输入你要查找的元素x=";
cin>>x;
cout<<"此元素在顺序表中的位置为:"<<Location(L,x)+1<<endl;
}
if(Opration==4)
{
ShowList(L);
}
if(Opration==5)
{
break;
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-18
//以前写的,你自己改改吧。
#define max 50//定义数组长度
#include<iostream>
using namespace std;
typedef int elem;//定义数组元素的类型
struct sqlist
{
elem a[max];
int length;
};
///用1表示操作能完成,0表示不能
void Init_list(sqlist *&L)//建立
{
L=new sqlist;
L->length=0;
}
void Destory_list(sqlist *&L)//摧毁
{
delete L;
}
int List_empty(sqlist *L)//判断是否为空
{
return L->length==0;
}
int List_length(sqlist *L)//有多少个元素
{
return L->length;
}
void Dis_list(sqlist *L)//显示元素
{
int i;
if(List_length(L)==0) cout<<"There is no elem in the list"<<endl;
else
{
for(i=0;i<L->length;i++)
cout<<L->a[i]<<endl;
}
}
int Get_listelem(sqlist *L,int i,elem &e)//求出表中第i个元素
{
if(i>L->length+1||i<1) return 0;
e=L->a[i-1];
return 1;
}
int Locate_listelem(sqlist *L,elem x,elem &e)//判断x是表中第几个元素
{
int i;
for(i=0;i<L->length;i++)
if(x==L->a[i]) {e=i+1;return 1;}
return 0;
}
int Insert_listelem(sqlist *&L,elem x,int i)//在第i个位置插入新元素x
{
if(i<1||i>L->length) return 0;
i--;
int j;
for(j=L->length;j>i;j--)
{
L->a[j]=L->a[j-1];
}
L->a[i]=x;
L->length++;
return 1;
}
void Listinsert(sqlist *&L,elem x) //如果表为顺序表(从小到大),插入一个元素也使之为顺序表
{
int i,t;
for(i=0;i<L->length;i++)
if(x<L->a[i]) break;
t=i;
for(i=L->length;i>t;i--)
{
L->a[i]=L->a[i-1];
}
L->a[t]=x;
L->length++;
}
int Delete_listelem(sqlist *&L,int i)//删除第i个元素
{
int j;
if(i<1||i>L->length) return 0;
i--;
for(j=i;j<L->length-1;j++)
L->a[j]=L->a[j+1];
L->length--;
return 1;
}
相似回答