1)顺序表的操作
① 输入一组整型元素序列,建立线性表的顺序存储结构。
② 实现该线性表的遍历。
③ 在该顺序表中查找某一元素,查找成功显示查找元素,否则显示查找失败。
④ 在该顺序表中删除或插入指定元素。
⑤ 建立两个按值递增有序的顺序表,将他们合并成一个按值递增有序的顺序表。
<pre name="code" class="html">#include<stdio.h>
#include<stdlib.h>
typedef struct{
int *a;
int length;
int listsize;
}sq_list;
void initList(sq_list &p)
{
int n;
scanf("%d",&n);
p.length=n;
p.listsize=100;
p.a=(int*)malloc(p.listsize*sizeof(int)); //开辟最大存储空间
if (!p.a)
exit(0);
for (int i=1;i<=n;i++)
scanf("%d",&p.a[i]);
}
void search(sq_list p,int e)
{
int i=1;
while (p.a[i]!=e&&i<=p.length)
{
i++;
}
if (i>p.length)
printf("无此值\n");
else
printf("%d\n",i);
}
void bianli(sq_list p) //遍历一遍,输出数值
{
int i;
for (i=1;i<=p.length;i++)
printf("%d ",p.a[i]);
printf("\n");
}
void insert(sq_list &p,int i,int e)
{
int k;
if (i<1||i>p.length+1)
exit(0);
if (p.length>=p.listsize)
{
p.a=(int*)realloc(p.a,(p.length+p.listsize)*(sizeof(int)));
p.listsize+=p.length;
if (!p.a)
exit(0);
}
p.length++;
for (k=p.length-1;k>=i;k--)
p.a[k+1]=p.a[k];
p.a[i]=e;
}
void dele(sq_list &p,int i,int &e)
{
int k;
if (i<1||i>p.length)
exit(0);
e=p.a[i];
printf("%d\n",e);
for (k=i;k<p.length;k++)
p.a[k]=p.a[k+1];
p.length--;
}
void hebing(sq_list p,sq_list q)
{
sq_list m;
int c=0,k,i,j;
m.length=p.length+q.length;
m.listsize+=2*(p.length+q.length);
m.a=(int*)malloc(m.listsize*sizeof(int));
for (i=1;i<=p.length;i++)
for (j=1;j<=q.length;j++)
if (p.a[i]==q.a[j])
c++;
c=m.length-c;
k=c;
i=p.length;
j=q.length;
while (i>0&&j>0)
{
if (p.a[i]>q.a[j])
{
m.a[c--]=p.a[i--];
}
if (p.a[i]<q.a[j])
{
m.a[c--]=q.a[j--];
}
if (p.a[i]==q.a[j])
{
m.a[c--]=p.a[i--];
j--;
}
}
while (i>0)
{
m.a[c--]=p.a[i--];
}
while (j>0)
{
m.a[c--]=q.a[j--];
}
for (i=1;i<=k;i++)
printf("%d ",m.a[i]);
printf("\n");
}
int main()
{
sq_list p,q;
int i,e;
initList(p);
bianli(p);
scanf("%d",&e);
search(p,e);
scanf("%d %d",&i,&e);
insert(p,i,e);
bianli(p);
scanf("%d",&i);
dele(p,i,e);
bianli(p);
initList(p);
initList(q);
hebing(p,q);
return 0;
}
代码如果收什么错误的话,欢迎积极评论