鱼C论坛

 找回密码
 立即注册
查看: 3746|回复: 12

线性表的插入

[复制链接]
发表于 2013-3-14 20:18:53 | 显示全部楼层 |阅读模式
2鱼币
#include <stdio.h>
#include <stdlib.h>
#define N 100
typedef struct _student
{
    int num[N];
    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[j+1] = Psqlist->num[j];
            Psqlist->num[i-1]=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]=i+1;
     for(i=0;i<a->length;i++)
     printf("%d",a->num[i]);
     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[i]);
     }
     else
     printf("insert wrong!");
}
麻烦看看那里错了,请稍微详细点!!本人刚学,谢谢大家了!!!!!!!1

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-3-14 21:58:56 | 显示全部楼层
不想看啊啊啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-15 13:40:30 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-3-15 18:55:12 | 显示全部楼层
注释都没有,谁有闲心去看这个!!!!而且链表问题书上不是源码吗???照着一个个对比吧  亲
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-15 20:13:09 | 显示全部楼层
主要是我这个是自己理解的,跟书上的风格不一样,我不喜欢书上那种,如果是高手的话这种菜鸟级别的程序简直就是秒杀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-15 20:14:12 | 显示全部楼层
而且这个就是一个线性表的插入函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-3-18 10:11:09 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#define N 100
typedef struct _student
{
    int num[N];
    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[j+1] = Psqlist->num[j];
         }
         //下面这句不应该放在for循环里,因为你只对他们修改一次值,而不是在每次循环时都改变  
         Psqlist->num[i-1]=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]=i+1;
        for(i=0;i<a->length;i++)
                printf("%d",a->num[i]);
        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[i]);
        }
        else
                printf("insert wrong!");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-3-26 15:15:42 | 显示全部楼层
楼上正解,路过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-31 14:32:28 | 显示全部楼层
JIAFEIMAO 发表于 2013-3-18 10:11
#include
#include
#define N 100

谢谢,我现在已经明白了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-9-18 16:53:25 | 显示全部楼层
我是来领鱼币的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2016-2-28 14:00:44 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2016-2-28 14:04:17 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-3-6 21:02:46 | 显示全部楼层
学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 07:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表