线性表的插入
#include <stdio.h>#include <stdlib.h>
#define N 100
typedef struct _student
{
int num;
int length;
}Student;
Student *sqlist()
{
Student * p= (Student*)malloc(sizeof(Student));
p->length= 0;
return p;
}
int insert(Student*Psqlist,int i,int x)
{
int j =0;
if((Psqlist->length)==N)
{
printf("Overflow!/n");
return 0;
}
else if(i<1||i>Psqlist->length+1)
{
printf("the location is wrong/n");
return 0;
}
else
{
for(j=Psqlist->length-1;j>=i-1;j--)
{
Psqlist->num = Psqlist->num;
Psqlist->num=x;
Psqlist->length++;
}
return (1);
}
}
void main()
{
int i,location,value;
Student * a=NULL;
a->length=5;
for(i=0;i<5;i++)
a->num=i+1;
for(i=0;i<a->length;i++)
printf("%d",a->num);
printf("please input the location to insert:");
scanf("%d",&location);
printf("please input the value:");
scanf("%d",&value);
if(insert(a,location,value))
{
for(i=0;i<a->length;i++)
printf("%d",a->num);
}
else
printf("insert wrong!");
}
麻烦看看那里错了,请稍微详细点!!本人刚学,谢谢大家了!!!!!!!1 不想看啊啊啊 向往青莲 发表于 2013-3-14 21:58 static/image/common/back.gif
不想看啊啊啊
大哥,如果懂得话就看看嘛,拜托了
注释都没有,谁有闲心去看这个!!!!而且链表问题书上不是源码吗???照着一个个对比吧亲 主要是我这个是自己理解的,跟书上的风格不一样,我不喜欢书上那种,如果是高手的话这种菜鸟级别的程序简直就是秒杀 而且这个就是一个线性表的插入函数 #include <stdio.h>
#include <stdlib.h>
#define N 100
typedef struct _student
{
int num;
int length;
}Student;
Student *sqlist()
{
Student * p= (Student*)malloc(sizeof(Student));
p->length= 0;
return p;
}
int insert(Student*Psqlist,int i,int x)
{
int j =0;
if((Psqlist->length)==N)
{
printf("Overflow!/n");
return 0;
}
else if(i<1||i>Psqlist->length+1)
{
printf("the location is wrong/n");
return 0;
}
else
{
//以下有改动
for(j=Psqlist->length-1;j>=i-1;j--)
{
Psqlist->num = Psqlist->num;
}
//下面这句不应该放在for循环里,因为你只对他们修改一次值,而不是在每次循环时都改变
Psqlist->num=x; Psqlist->length++;
return (1);
}
}
void main()
{
int i,location,value;
// Student * a=NULL;//a 是一个空指针,没有指向任何已分配内存空间的对象,空指针在没有指向对象时是不能做其他事的
Student * a=(Student*)malloc(sizeof(Student)); //给你指向一个内存空间
a->length=5;
for(i=0;i<5;i++)
a->num=i+1;
for(i=0;i<a->length;i++)
printf("%d",a->num);
printf("\n");
printf("please input the location to insert:");
scanf("%d",&location);
printf("please input the value:");
scanf("%d",&value);
if(insert(a,location,value))
{
for(i=0;i<a->length;i++)
printf("%d",a->num);
}
else
printf("insert wrong!");
}
楼上正解,路过 JIAFEIMAO 发表于 2013-3-18 10:11 static/image/common/back.gif
#include
#include
#define N 100
谢谢,我现在已经明白了 我是来领鱼币的 学习了
页:
[1]