顺序表的基本操作及应用
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MaxSize 100
typedef struct{
int *elem;
int length;
int data[5]={10,9,8,7,6};
int data1[MaxSize];
int data2[10];
}Sqlist;
Sqlist CreateList(Sqlist L,int n)
{
int e;
L.elem=(int *)malloc(MaxSize * sizeof(int));
if (!L.elem) exit(0);
L.length=n;
printf("total number is %d:\n",n);
for(int i=0;i<n;i++)
{
printf("input the %d number:\n",i+1);
scanf("%d",&e);
L.elem[i]=e;
}
return L;
}
void ShowList(Sqlist L)
{
int i;
printf("show the number:\n");
for (i=0;i<L.length;i++)
printf("%d ",L.elem[i]);
}
void ShowList1(Sqlist D)
{
int i;
printf("show the number:\n");
for(i=0;i<D.length;i++)
printf("%d ",D.data1[i]);
}
Sqlist InsertList(Sqlist L,int i)
{
int e;
if((i<1) || (i > L.length+1 ))
exit(0);
if(L.length >= MaxSize)
exit(0);
for(int j = L.length; j>=i; j--)
L.elem[j] = L.elem[j-1];
printf("\ninput the insert number:\n");
scanf("%d",&e);
L.elem[i-1]=e;
L.length++;
return L;
}
Sqlist DeleteList(Sqlist L,int i)
{
int *e;
if((i<1)||(i>L.length)) printf("error\n");
e=&(L.elem[i-1]);
for( int j=i;j<L.length;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return L;
}
int GetElem(Sqlist L,int i)
{
int e;
if ((i<=1)||(i>=L.length+1)) exit(0);
e=L.elem[i-1];
return e;
}
int LocateElem(Sqlist L,int e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e)
return i+1;
}
Sqlist Inversion(Sqlist L)
{
printf("元素逆置:");
for(int i=0;i<L.length/2;i++)
{
int data;
data=L.elem[i];
L.elem[i]=L.elem[L.length-i-1];
L.elem[L.length-i-1]=data;
}
return L;
}
Sqlist DeleteAll(Sqlist L,int x)
{
int k=0;
printf("删除“3”这个元素:");
for(int i=0;i<L.length;i++)
{
if(L.elem[i]!=x)
{
L.elem[k]=L.elem[i];
k++;
}
}
L.length=k;
return L;
}
Sqlist DeleteRange(Sqlist L,int n,int m)
{
int a=n;
int b=m;
printf("删除指定范围元素:");
if((n<1)||(m>L.length)) printf("error\n");
for(int i=n;m<=L.length;i++,m++)
{
L.elem[i-1]=L.elem[m];
}
L.length=L.length-b+a-1;
return L;
}
Sqlist DeleteSame(Sqlist L)
{
int i,j;
printf("删除重复的元素:");
if(L.length==0) printf("error\n");
for(i=0,j=1;j<L.length;j++)
{
if(L.elem[i]!=L.elem[j])
{
L.elem[++i]=L.elem[j];
}
}
L.length=i+1;
return L;
}
Sqlist Merge(Sqlist L,Sqlist D)
{
printf("合并两个顺序表:");
Sqlist elem,data1;
Sqlist data;
int DL=sizeof(D.data)/sizeof(D.data[0]);
if(MaxSize<L.length+DL) printf("error\n");
int i=0,j=0,k=0;
while(i<L.length&&j<DL)
{
if(L.elem[i]<=D.data[j])
D.data1[k++]=L.elem[i++];
else
D.data1[k++]=D.data[j++];
}
while(i<L.length)
{
D.data1[k++]=L.elem[i++];
}
while(j<DL)
{
D.data1[k++]=D.data[j++];
}
D.length=k;
return D;
}
Sqlist left(Sqlist L,int p)
{
printf("左移2个位置:");
int n=p;
for(int i=0;i<p;i++)
{
L.elem[L.length]=L.elem[i];
L.length++;
}
for(int i=0;i<L.length;i++)
{
L.elem[i]=L.elem[p];
p++;
}
L.length=L.length-n;
return L;
}
int main()
{
Sqlist list,list2;
int get_e;
int locate_e;
list=CreateList(list,8);
ShowList(list);
list=InsertList(list,3);
printf("\n");
ShowList(list);
list=DeleteList(list,3);
printf("\n");
ShowList(list);
get_e=GetElem(list,4);
printf("\n");
printf("the fourth number is: %d\n",get_e);
locate_e=LocateElem(list,6);
if(locate_e<=list.length) printf("数字6在顺序表中位置是%d!\n",locate_e);
else printf("no such number!\n");
list=Inversion(list);
ShowList(list);
printf("\n");
list=DeleteAll(list,3);
ShowList(list);
printf("\n");
list=DeleteRange(list,2,3) ;
ShowList(list);
printf("\n");
list=DeleteSame(list);
ShowList(list);
printf("\n");
list2=Merge(list,list2);
ShowList1(list2);
printf("\n");
list=left(list,2);
ShowList(list);
printf("\n");
}