C描述代码
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
10.2 实验内容 10.2.1 #include #define MAX 30 /*全局变量定义*/ int a[MAX]; int n,i; int b,x; /*函数声明*/ void insert(int,int); void delete_i(int); int locate(int); void input_array(); void print_array(); void main() { */ printf("Print array : a[1...n]\n"); print_array(); /*插入运算*/ i=5; b=555; printf("Insert a[%d]=%d Result :\n",i,b); insert(i,b); print_array(); /*删除运算*/ i=7; printf("Delete a[%d]=%d Result :", i, a[i]); delete_i(i); print_array(); /*查找运算*/ x=97; printf(" Print_array: \n"); print_array(); printf("Locate x=65, Result :"); /*顺序表运算*/ printf("Please input :n=? \n"); scanf("%d",&n); printf("Please input :a[1...n]\n"); input_array(); /*输入元素1..n存放于数组a[1]..a[n]中 /*顺序表大小*/ } if (locate(x)==0) printf("x does not exist .\n"); else printf("Locate=%d\n", locate(x)); void insert(int i, int b) /*插入函数*/ { int j; if ((i<1)||(i>n+1)) printf("Position does not exist.\n"); else { for ( j=n; j>=i; j--) a[j+1]=a[j]; a[i]=b; n=n+1; } } void delete_i(int i) /*删除函数*/ { int j; if ((i<1)||(i>n)) printf("Position does not exist\n"); else { for(j=i+1; j<=n; j++) a[j-1]=a[j]; n=n-1; } } /*delete*/ int locate(int x) /*查找函数*/ { int i; i=1; while (( i i++;
if ( a[i] == x ) return(i); else
return(0); } /*locate*/
void input_array() {
int i;
/*输入元素1..n存放于数组a[1]..a[n]中*/
for ( i=1; i<=n; i++ ) scanf(" %d ",&a[i]); printf("\n"); }
void print_array() /*输出元素*/ {
int i;
for ( i=1; i<=n; i++ ) printf("%d",a[i]); printf("\n"); }
本文来源:https://www.wddqw.com/doc/d70da981ec3a87c24028c400.html