#include<stdio.h>
int len;
// 数组 数组原长 元素e
void search(int a[], int n, int e)//查找
{
int i = n;
int b = 0;
while (i >= 0)
{
if (a[i] == e)b++;
i--;
}
if (b > 0)printf("Found!\n");
else printf("No found!\n");
}
// 数组 数组原长 插入位置 插入元素
void Insert(int a[], int n, int p, int e)//插入
{
if (p > n)printf("The given position is invalid!\n");
else {
for (int i = n - 1; i >= p - 1; i--)
a[i + 1] = a[i];
a[p - 1] = e;
len++;
for (int i = 0; i < (n+len); i++)
printf("%d ", a[i]);
printf("\n");
}
}
// 数组 数组原长 删除元素e
void Delete(int a[],int n,int e)//删除
{
int b=0,b1=0,temp=0,temp1=0;
int total=n;
while(temp<n)
{
if(a[temp]==e)b++;
temp++;
}
len=len-b;
for(int i=0;i<b;i++)
{
temp1=n;
while(temp1>0)
{
if(a[temp1]==e)break;
temp1--;
}
if(a[temp1]==e)
{
for(int j=temp1;j<n;j++)
{
a[j]=a[j+1];
}
n--;
}
}
for(int k=0;k<total+len;k++)
{
printf("%d ",a[k]);
}
}
void print(int a[], int n)//打印
{
for (int i = 0; i < n+len ; i++)
printf("%d ", a[i]);
}
int main()
{
int n;
int a[50];
printf("Please input N for the number of element = \n");//用数组建立顺序线性表
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
search(a, n, 30);
search(a, n, 67);
Insert(a, n, 5, 88);
Insert(a, n, 10, 66);
print(a, n);
printf("\n");
Delete(a, n, 44);
printf("\n");
return 0;
}
输入:
8
1 -9 6 44 400 44 30 60
运行结果: