中易网

试编写算法:在顺序表L中查找值为x的元素位置

答案:3  悬赏:70  
解决时间 2021-01-12 22:08
  • 提问者网友:风月客
  • 2021-01-12 19:06
试编写算法:在顺序表L中查找值为x的元素位置
最佳答案
  • 二级知识专家网友:患得患失的劫
  • 2021-01-12 19:45
#include
#include
#include
#define ADDRESS 100
#define ZL 10
typedef struct
{
int *elem;
int length;
int size;
}LIST;

LIST create()
{
LIST L;
L.elem=(int *)malloc(ADDRESS*sizeof(int));
L.length=0;
L.size=ADDRESS;
return L;
}

int input(LIST L,int n)
{
int i;
printf("please input your nums:");
for(i=0;i scanf("%d",L.elem++);
return 1;
}

void output(LIST L)
{
int i;
printf("LIST=(");
for(i=0;i printf("%2d",*L.elem++);
printf(")\n");
}
int find(LIST L,int x)
{
int i,seat;
for (i=0;i
{
if(x==*L.elem++)
{ seat=i+1;
break;
}
}
if(i==L.length)
printf("CAN'T FIND %d\n",x);
return seat;
}
void main()
{
LIST L;
int i,j,n,a,b,x,SEAT;
printf("pleases input the amount of nums:");
scanf("%d",&n);
L=create();
input(L,n);
L.length=n;

printf("please input the number you want to find:");
scanf("%d",&x);
SEAT=find(L,x);
printf("the seat is %d \n",SEAT);
}
全部回答
  • 1楼网友:枭雄戏美人
  • 2021-01-12 22:01
顺序表就用对半查找呗。。。
  • 2楼网友:渊鱼
  • 2021-01-12 21:09
currentnode=headnode;//指向头节点
n=0;
while assigned(currentnode) //存在结点
{ n=n+1;
if ( currentnode.value=x ){
break;
b=true}
currentnode=currentnode.link;
}
b=true //输出
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息