int Sqlist_search(Sqlist L,int x)改名的问题
答案:1 悬赏:30 手机版
解决时间 2021-04-18 04:42
- 提问者网友:暗中人
- 2021-04-17 23:22
int Sqlist_search(Sqlist L,int x)改名的问题
最佳答案
- 五星知识达人网友:一叶十三刺
- 2021-04-17 23:42
#include
#define MAXLEN 50
typedef struct
{
int elem[MAXLEN];
int length;
}Sqlist;
Sqlist insert(Sqlist L,int i,int x)
{
int j;
if(i<1||i>L.length+1)
printf("ERROR!");
else if(L.length>=MAXLEN)
printf("OVERFLOW!");
else
{
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=x;
L.length++;
}
return L;
}
Sqlist del(Sqlist L,int i)
{
int j;
if(i<1||i>L.length)
printf("ERROR!");
else
{
for(j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
}
return L;
}
//原代码 int Sqlist_search(Sqlist L,int x)
int search(Sqlist L,int x)
{
int i;
for(i=1;i<=L.length&&L.elem[i-1]!=x;i++);
if (i<=L.length)
return i;
else
return 0;
}
void display(Sqlist L)
{
int j;
for(j=0;j<=L.length-1;j++)
printf("%4d",L.elem[j]);
printf("
");
}
int main()
{
Sqlist L;
int i,x,j;
printf("
please input the length:");
scanf("%d",&L.length);
printf("please input the Value:
");
for(j=0;j<=L.length-1;j++)
scanf("%d",&L.elem[j]);
printf("please input the insert position:");
scanf("%d",&i);
printf("please input the insert node:");
scanf("%d",&x);
L=insert(L,i,x);
display(L);
printf("please input the delete position:");
scanf("%d",&i);
L=del(L,i);
display(L);
printf("please input the search node:");
scanf("%d",&x);
//原代码 if (Sqlist_search(L,x))
//原代码 printf("search is success and %d is %d position
",x,Sqlist_search(L,x));
int nResult;
nResult=search(L,x);
if (nResult)
printf("search is success and %d is %d position
",x,nResult);
else
printf("search is unsuccess
");
printf("The program is end,press any key to continue.");
getchar(); //getch();
return 0; //函数int main()要有返回值
}
追问int nResult;
nResult=search(L,x);
if (nResult)
printf("search is success and %d is %d position\n",x,nResult);
为什么要加这一段
而且我用的c++ 6.0不行追答//=========================================
//这里,查找函数search()只需执行1次
int nResult;
nResult=search(L,x);
if (nResult)
printf("search is success and %d is %d position\n",x,nResult);
//=========================================
//这里,查找函数search()执行了2次
if (search(L,x)) //函数search()执行第1次
printf("search is success and %d is %d position\n",x,search(L,x));//函数search()执行第2次
//实际上,函数search()只需执行1次,而要执行第2次,没有必要,
//所以用变量nResult获得函数search()的返回值
//=========================================
楼主的c++ 6.0不行,显示了什么错误信息?
试试将search换成其它名称,例如search3追问C:\Users\Administrator\Desktop\jj.c(92) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Administrator\Desktop\jj.c(93) : error C2065: 'nResult' : undeclared identifier
执行 cl.exe 时出错.
#define MAXLEN 50
typedef struct
{
int elem[MAXLEN];
int length;
}Sqlist;
Sqlist insert(Sqlist L,int i,int x)
{
int j;
if(i<1||i>L.length+1)
printf("ERROR!");
else if(L.length>=MAXLEN)
printf("OVERFLOW!");
else
{
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=x;
L.length++;
}
return L;
}
Sqlist del(Sqlist L,int i)
{
int j;
if(i<1||i>L.length)
printf("ERROR!");
else
{
for(j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
}
return L;
}
//原代码 int Sqlist_search(Sqlist L,int x)
int search(Sqlist L,int x)
{
int i;
for(i=1;i<=L.length&&L.elem[i-1]!=x;i++);
if (i<=L.length)
return i;
else
return 0;
}
void display(Sqlist L)
{
int j;
for(j=0;j<=L.length-1;j++)
printf("%4d",L.elem[j]);
printf("
");
}
int main()
{
Sqlist L;
int i,x,j;
printf("
please input the length:");
scanf("%d",&L.length);
printf("please input the Value:
");
for(j=0;j<=L.length-1;j++)
scanf("%d",&L.elem[j]);
printf("please input the insert position:");
scanf("%d",&i);
printf("please input the insert node:");
scanf("%d",&x);
L=insert(L,i,x);
display(L);
printf("please input the delete position:");
scanf("%d",&i);
L=del(L,i);
display(L);
printf("please input the search node:");
scanf("%d",&x);
//原代码 if (Sqlist_search(L,x))
//原代码 printf("search is success and %d is %d position
",x,Sqlist_search(L,x));
int nResult;
nResult=search(L,x);
if (nResult)
printf("search is success and %d is %d position
",x,nResult);
else
printf("search is unsuccess
");
printf("The program is end,press any key to continue.");
getchar(); //getch();
return 0; //函数int main()要有返回值
}
追问int nResult;
nResult=search(L,x);
if (nResult)
printf("search is success and %d is %d position\n",x,nResult);
为什么要加这一段
而且我用的c++ 6.0不行追答//=========================================
//这里,查找函数search()只需执行1次
int nResult;
nResult=search(L,x);
if (nResult)
printf("search is success and %d is %d position\n",x,nResult);
//=========================================
//这里,查找函数search()执行了2次
if (search(L,x)) //函数search()执行第1次
printf("search is success and %d is %d position\n",x,search(L,x));//函数search()执行第2次
//实际上,函数search()只需执行1次,而要执行第2次,没有必要,
//所以用变量nResult获得函数search()的返回值
//=========================================
楼主的c++ 6.0不行,显示了什么错误信息?
试试将search换成其它名称,例如search3追问C:\Users\Administrator\Desktop\jj.c(92) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Administrator\Desktop\jj.c(93) : error C2065: 'nResult' : undeclared identifier
执行 cl.exe 时出错.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯