永发信息网

C语言问题求教

答案:1  悬赏:20  手机版
解决时间 2021-04-28 12:43
  • 提问者网友:眉目添风霜
  • 2021-04-28 06:42

题目是:

设顺序表L中数组元素递增有序,写一算法,将X插入到顺序表的适当位置使表仍然有序,我写的程序是下面的,但是得不到想要的结果:

#define M 100
typedef int ItemType;
typedef struct {
int len;
ItemType items[M];
} Vector;

int createCircle(int population, Vector* vector)
{
int i;
if(population>M) return 0;
for (i=0; i<population; i++)
{
vector->items[i]=i+1;
}
vector->len=population;
return 1;
}

void insertOrderList(Vector* vector ,ItemType x )
{
int i;
if (vector->len>=100)
printf("overflow");
for (i=vector->len;i>1&&vector->items[i-1]>x;i--)
{
vector->items[i]=vector->items[i-1] ;
}
vector->items[i] =x;
vector->len++;
}


main()
{

int n,m,i;
Vector vector;
printf("Please input the length =");
scanf("%d",&n);
createCircle(n,&vector);
printf("Please type the number what to insert =");
scanf("%d",m);
insertOrderList(&vector,m);
for(i=0;i<=n;i++)
printf("%d ",vector.items[i]);
scanf("%d");

}

我的思路是,输入表长15,然后创建顺序表 1.2.3.4.。。。15 ,然后再输入插入数3,将3插入到表中,再输出新表,为什么输出结果不对??

最佳答案
  • 五星知识达人网友:不想翻身的咸鱼
  • 2021-04-28 06:55
你掉了个符号
帮你改过来了 错误地方有注释
#include <stdio.h>
#define M 100
typedef int ItemType;
typedef struct {
int len;
ItemType items[M];
} Vector;

int createCircle(int population, Vector* vector)
{
int i;
if(population>M) return 0;
for (i=0; i<population; i++)
{
vector->items[i]=i+1;
}
vector->len=population;
return 1;
}

void insertOrderList(Vector* vector ,ItemType x )
{
int i;
if (vector->len>=100)
printf("overflow");
for (i=vector->len;i>1&&vector->items[i-1]>x;i--)
{
vector->items[i]=vector->items[i-1] ;
}
vector->items[i] =x;
vector->len++;
}


main()
{

int n,m,i;
Vector vector;
printf("Please input the length =");
scanf("%d",&n);
createCircle(n,&vector);
printf("Please type the number what to insert =");
scanf("%d",&m);//这里要加取地址符
insertOrderList(&vector,m);
for(i=0;i<=n;i++)
printf("%d ",vector.items[i]);
scanf("%d");

}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯