插入函数运行不起,求救
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
int list[25];
int i,n,a,sum=0,k,l;
int eleminsert;
/*------------------创建函数--------------*/
void initlist()
{
printf("Please input the total of the elems:");
scanf("%d",&n);
if(n>25||n<1)
{printf("ERROE!");}
printf("Please input the elems:...\n");
for(i=0;i<n;i++)
{scanf("%d",&list[i]);
}
}
/*------------------打印函数--------------*/
void Print(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
printf("\n");
}
/*------------------查找函数------------*/
int Search(int list[],int n,int m)//n是总个数,m是输入个数
{
if(m<1||m>n)
printf("ERROR!\n");
else
printf("The elem is %d at %d place\n",list[m-1],m);
}
/*----------------插入函数------------*/
void Insert(int list[],int n,int m,int elem)
{
int j;
if(m<1||m>n)
{printf("ERROR!\n"); }
else
for(j=n-1;j>=m-1;i--)
{list[j]=list[j-1];}
list[m-1]=elem;
n=n+1;
printf("The new list are:" );
Print(list,n);
}
/*---------------删除函数-----------*/
void Delete(int list[],int n,int m)
{
int q;int j;
if(m<1||m>n)
{printf("ERROR!\n"); }
j=list[m-1];
for(q=m-1;q<=n;q++)
{list[q]=list[q+1];}
printf("The new list are:");
Print(list,n-1);
}
/*-------------求和函数------------*/
void Sum(int list[],int n,int sum)
{
int j;
for(j=0;j<n;j++)
{sum=sum+list[j];}
printf("The sum is :%d",sum);
}
void menu()
{
int j;
/*------------菜单函数------------*/
menulab:
printf("********************** MENU ******************\n\n");
printf("Create a new int list :...................press 1\n\n");
printf("Print the whole list :....................press 2\n\n");
printf("Search by order :........................press 3\n\n");
printf("Insert the elem in the place i:...........press 4\n\n");
printf("Delete the elem by order :................press 5\n\n");
printf("Sum all elem in the list :................press 6\n\n");
printf("exit the programe :.......................press 0\n\n");
printf("********************** END *******************\n\n");
printf("Please choose the number from (0~6).....");
checklabel: scanf("%1d",&j);getchar();
if(j<0||j>6)
{printf("Error! Please choose again......");
goto checklabel;
}
printf("\n\tYou choose the number %d\n ",j);
getchar();
switch(j)
{
case 1:/*创建任意整数线性表*/
initlist();
goto menulab;
case 2: /*打印(遍历)该线性表*/
printf("The original list is:");
Print(list,n);
printf("Press any key to continue.....");
getchar();
goto menulab;
case 3:/*在线性表中查找第i个元素,并返回其值*/
printf("Input which LNode you want to Search(Input number):");
scanf("%d",&a);
Search(list,n,a);
printf("Press any key to continue.....");
getchar();
goto menulab;
case 4:/*在线性表中第i个元素之前插入一已知元素*/
printf("Please input the elem's place where you want to insert");
scanf("%d",&k);
printf("Input the elem which you want to insert:");
scanf("%d",&eleminsert);
Insert(list,n,k,eleminsert);
printf("Press any key to continue.....");
getchar();
goto menulab;
case 5:/*在线性表中删除第i个元素*/
printf("Please input the elem you want to delete:");
scanf("%d",&l);
n=n+1;
Delete(list,n,l);
n=n-1;
printf("Press any key to continue.....");
getchar();
goto menulab;
case 6:/*求线性表中所有元素值(整数)之和*/
Sum(list,n,sum);
printf("Press any key to continue.....");
getchar();
goto menulab;
case0:/*退出程序*/
printf("Press any key to continue.....");
getchar();
exit(0);
}}
void main()
{
void menu();
menu();
}
[解决办法]
已经帮你修改过来了!oK了
void Insert(int list[],int n,int m,int elem)
{
int j;
if(m<1
[解决办法]
m>n)
{
printf("ERROR!\n");
}
else
for(j = n-1; j>=m-1;j--) // 这里是j --
{
list[j]=list[j-1];
}
list[m-1]=elem;
n= n+1;
printf("The new list are:" );
Print(list,n);
}
/*----------------插入函数------------*/
void Insert(int list[],int n,int m,int elem)
{
int j;
if(m<1
[解决办法]
m>n)
{printf("ERROR!\n"); }
else
for(j=n-1;j>=m-1;i--) //i--是不是应该改成j--呢?
//还有是不是应该改成下面这样呢?
//for(j=n;j>=m;j--) //你自己再仔细看一下吧!
{list[j]=list[j-1];}
list[m-1]=elem;
n=n+1;
printf("The new list are:" );
Print(list,n);
}
//还有就是
/*----------------插入函数------------*/
void Insert(int list[],int n,int m,int elem)
/*---------------删除函数-----------*/
void Delete(int list[],int n,int m)
这两个函数的第二个参数int n是不是应该改成int *n呢?
n应该要做为形参返回的吧?要不然你在函数外面怎么知道
list插入或删除后全局变量n的变化呢?
自己在仔细看一下代码,运行看看结果就知道了!