永发信息网

设计一个包含初始化 求数据元素个数 插入删除和取数据元素的有序顺序表

答案:2  悬赏:10  手机版
解决时间 2021-05-19 08:18

还有 一小时 交 作业 哈 时间有限 会的同学 请 快点 教教咱

最佳答案

#include<iostream.h>
#define maxsize 20


template<class T>
class Seqlist
{
private:
T a[maxsize];
int len;
public:
void init(); //初始化
int length();//求长度
void insert(Seqlist &l,int i,T x); //插入
void deleted(Seqlist &l,int i);//删除
void print();//输出
void create(Seqlist &L,T e);//创建
};


template<class T>
void Seqlist<T>::init()
{
len=0;
}


template<class T>
int Seqlist<T>::length()
{
return len;
}


template<class T>
void Seqlist<T>::insert(Seqlist &l,int i,T x)
{
if(l.len>maxsize)
cout<<"overflow"<<endl;
else if (i<1||i>l.len+1)
cout<<"position is not correct!"<<endl;
else
{
for(int j=l.len;j>=i;j--)
l.a[j+1]=l.a[j];
l.a[i]=x;
l.len++;
}
}


template<class T>
void Seqlist<T>::deleted(Seqlist &l,int i)
{


if (i<1||i>l.len)
cout<<"position is not correct!"<<endl;
else
{
for(int j=i+1;j<=l.len;j++)
l.a[j-1]=l.a[j];
l.len--;
}
}


template<class T>
void Seqlist<T>::print()
{
for(int i=1;i<=len;i++)
cout<<a[i]<<'\t';
cout<<endl;
}


template<class T>
void Seqlist<T>::create(Seqlist &L,T e)
{
cout<<"输入数据,当输入-1时结束:"<<endl;
cin>>e;
while(e!=-1)
{
L.insert(L,L.length()+1,e);
cin>>e;
}
}


void main()
{
Seqlist<int>L;
int e,i;
L.init();
L.create(L,e);

cout<<"输出顺序表:"<<endl;
L.print();
cout<<"输入要删除元素的位置:";
cin>>i;
L.deleted(L,i);
cout<<"输出删除后的顺序表:"<<endl;
L.print();
}

全部回答

typedef struct

{ DataType list[Maxsize];

int size;

}SeqList;

void ListInitiate(SeqList *L)

{ L->size = 0;

}

int ListLength(SeqList L)

{ return L.size;

}

int ListInsert(SeqList *L, int i, DataType x)

{ int j;

if(L->size >= Maxsize)

{ printf("顺序表已满无法插入! \n");

return 0;

}

else if(i < 0 || i > L->size )

{ printf("参数i不合法! \n");

return 0;

}

else

{ for(j = L->size; j > i; j--)

L->list[j] = L->list[j-1];

L->list[i] = x;

L->size ++;

return 1;

}

}

int ListDelete(SeqList *L, int i, DataType *x)

{ int j;

if(L->size <= 0)

{ printf("顺序表已空无数据元素可删! \n");

return 0;

}

else if(i < 0 || i > L->size-1)

{ printf("参数i不合法");

return 0;

}

else

{ *x = L->list[i];

for(j = i+1; j <= L->size-1; j++)

L->list[j-1] = L->list[j];

L->size--;

return 1; }

}

int ListGet(SeqList L, int i,DataType *x)

{ if(i < 0 || i > L.size-1)

{

printf("参数i不合法! \n");

return 0;

}

else

{ *x = L.list[i];

return 1; }

}

void SeqListSort(SeqList *L)

{ int i ,j ,temp;

for(j = 0; j < L->size-1; j++)

for(i = 0; i < L->size-1-j; i++)

if(L->list[i] > L->list[i+1])

{

temp = L->list[i];

L->list[i] = L->list[i+1];

L->list[i+1] = temp;

}

}

int SeqListInsert(SeqList *L, int m)

{ int p = 0, j, i;

if(L->size >= Maxsize)

{ printf("顺序表已满无法插入! \n");

return 0;

}

for(i = 0; i < L->size; i++)

if(m > L->list[i])

p++;

for(j = L->size; j > p; j--)

L->list[j] = L->list[j-1];

L->list[p] = m;

L->size++;

return 1;

}

void main()

{

SeqList myList;

int i , n , x ,m;

ListInitiate(&myList);

printf("请输入原始数据元素个数:");

scanf("%d", &n);

printf("请输入原始数据元素:\n");

for(i = 0; i < n; i++)

{scanf("%d", &x); ListInsert(&myList, i, x) ;}

printf("\n原始数据元素为:\n");

for(i = 0; i < ListLength(myList); i++)

if(ListGet(myList, i, &x))printf("%d ", x);

printf("\n正在对该顺序表递增排序......,");

SeqListSort(&myList);

printf("\n排序后的数据元素为:");

for(i = 0; i < ListLength(myList); i++)

if(ListGet(myList, i, &x))printf("%d ", x);

printf("\n数据元素个数为: %d\n", ListLength(myList));

printf("\n请输入要插入的数据元素:");

scanf("%d", &m);

SeqListInsert(&myList, m);

printf("\n插入新的数据元素之后有序顺序表为:\n");

for(i = 0; i < ListLength(myList); i++)

if(ListGet(myList, i, &x))printf("%d ", x);

printf("\n此时数据元素个数为: %d\n", ListLength(myList));

printf("\n请输入要删除的元素位置(0~%d):", n);

scanf("%d", &n);

ListDelete(&myList, n, &x);

printf("\n删除第%d个元素后的顺序表为:\n", n);

for(i = 0; i < ListLength(myList); i++)

if(ListGet(myList, i, &x))printf("%d ", x);

printf("\n此时的数据个数为: %d\n", ListLength(myList));

}

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
张湾区十堰中国工商银行24小时自助银行(阳光
蓝瘦香菇是怎么回事,蓝瘦香菇是什么意思
孕妇三个月了
合肥市六安路的疾病控制中心早上几点上班?
建华区齐齐哈尔友孚心理养生咨询研究所地址有
怎样注销电脑系统用户帐户密码?
今天早上吃了好多,为什么还是饿啊
求宿主txt(完整版)~~
DNF加15垫子多少钱?
摩天大楼进不了,急!!!
钟祥市荆门中国太平地址是什么,有没有知道的
殡仪馆的意思是什么,殡仪馆是什么意思
请问BL网通线路哪区能秒排战场 或者说是5分钟
跪求一个故事!!
劲舞团卡小人,帮下忙?
推荐资讯
水浒Q传杀镜子
张翰朱子骁郑爽魏晨俞灏明谁的家境最好
本周五快男帮帮唱张杰会来吗?选手们会和谁搭
绩效工资和提成工资有什么不同
不能参加婚礼怎样祝福,同学结婚祝福语
彪马在哪个国家便宜,卡地亚是哪个国家的。
苟字开头的名句苏轼,生活不止眼前苟且还有诗
我这显卡是64的吗? 我看不懂
世界上蛇有多少种
新版QQ宠物现在是26号,为什么领不了感恩节的
诺基亚E63是否近期有降价的趋势?
有什么药物可以治好青春痘?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?