| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 605 人关注过本帖
标题:求解答,c语言链表插入,输出,结果出不来(文件在压缩包里)
只看楼主 加入收藏
wangjiankun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-1-1
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:5 
求解答,c语言链表插入,输出,结果出不来(文件在压缩包里)
链表包含的文本.zip (192.35 KB)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

#define LEN sizeof(struct student)
struct student
{
    int number;
    char name[10];
    struct student * next;
   
};
//主函数
void main()
{   void print(struct student * head);//声明
    struct student * shanchu(struct student *p3,char k[10]);
    struct student * dig(struct student *p3,char m[10],int n,int u);
    struct student * creat(void);
    struct student * p3,* head1;
    int h,n,u;
   
    char k[10];
    char m[10];
    printf("你想插入哪个人,人名,学号,请输入1,你想删除哪个人,请输入2\n");
    scanf("%d",&h);
    if(h==2)
    {
    scanf("%s",&k);
    p3=creat();
    head1=shanchu(p3,k);
   
    print(head1);
    }
    else
    {
    printf("插入人的姓名及学号,某人前面");
    scanf("%s %d %d\n",m,&n,&u);
    p3=creat();
    head1=dig(p3,m,n,u);
    printf("123456");
    print(head1);
    }

}
//创建链表
struct student * creat(void)
{
    struct student * p1,*p2;
    struct student * head;
    FILE * fp;
    int n=0;
    if((fp=fopen("name2.txt","r"))==NULL)
    {
        printf("cannot open");
        exit(0);
    }
   
    p1=p2=(struct student *)malloc(LEN);
    fscanf(fp,"%d %s\n",&p1->number,&p1->name);
    while(n<30)
    {
        n=n+1;
        if(n>1)
        {
            p2->next=p1;
            p2=p1;
            
        }
        else head=p1;
        p1=(struct student * )malloc(LEN);
        fscanf(fp,"%d %s\n",&p1->number,&p1->name);
    }
    p1=NULL;
    fclose(fp);
    return(head);
}
//输出
void print(struct student *  head)
{
    struct student * p;
    p=head;
    if(head!=NULL)
    {
        do
        {printf("%d %s\n",p->number,p->name);
        p=p->next;}
        while(p!=NULL);
    }
}
//删除
struct student * shanchu(struct student * p3,char k[])
{
    struct student *p5;
    struct student * p6,*head2;
    p5=p3;
    head2=p3;
    while(strcmp(k,p5->name)!=0)
    {   p6=p5;
        p5=p5->next;
    }
    if(p5==head2)
    {
        head2=p5->next;
    }
    else if(p5->next==NULL)
    {
        p6->next=NULL;
    }
    else
    {
    p6->next=p5->next;
    }
    return(head2);
}
//插入
struct student * dig(struct student * p3,char m[],int n,int u)
{
  struct student * head3,*p7,*p8,*p0;
  int i=0;
  struct student p;

  p0=(struct student *)malloc(LEN);
  strcpy((p0->name),m);
 
  p0->number=n;
  
  p8=p3;
  
  while(u!=p8->number)
  {
      p7=p8;
      p8=p8->next;
  }
  p7->next=p0;
  p0->next=p8;
  
  return(p3);
}
搜索更多相关主题的帖子: include number 压缩包 c语言 
2014-01-01 23:37
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:0 
太长、分少了点

编写的程序,不能改变世界,却可以改变自己...
2014-01-02 00:21
mic123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:22
专家分:123
注 册:2013-12-23
收藏
得分:0 
文件读出来形成链表有问题,
试着修改了下,你可以试试,
因为fscanf 不知道什么时候是文件的末尾,可以多加一行,表示文件结束。
比如  0000000000    ABCABC

程序代码:
struct student * creat(void)
{
    struct student * p1,*p2;
    struct student * head;
    FILE * fp;
    int n=0;
    int i=0;
    struct student * NodeTmp_fro_file;
    struct student * NodeHead_fro_file=(struct student *)malloc(LEN);


    if((fp=fopen("name2.txt","r"))==NULL)
    {
        printf("cannot open");
        exit(0);
    }

    //p1=p2=(struct student *)malloc(LEN);
    //fscanf(fp,"%d %s\n",&p1->number,&p1->name);

    fscanf(fp,"%d %s\n",&NodeHead_fro_file->number,&NodeHead_fro_file->name);
    NodeHead_fro_file->next=NULL;
    while(((NodeHead_fro_file->number)!=0) && ((NodeHead_fro_file->name)!="ABCABC"))
    {
        //printf("%d\n%s\n",NodeHead_fro_file->number,NodeHead_fro_file->name);
        NodeTmp_fro_file=(struct student *)malloc(LEN);
        fscanf(fp,"%d %s\n",&NodeTmp_fro_file->number,&NodeTmp_fro_file->name);
        NodeTmp_fro_file->next=NodeHead_fro_file;
        NodeHead_fro_file=NodeTmp_fro_file;
    }
    fclose(fp);
    return(NodeHead_fro_file);
}


[ 本帖最后由 mic123 于 2014-1-2 16:29 编辑 ]
2014-01-02 16:25
so_love
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:7
帖 子:812
专家分:4151
注 册:2013-11-25
收藏
得分:0 
建议你malloc的时候加一个判断   判断是否申请下来内存。读取文件的时候也加判断、判断是读取文件错误还是其他错误。而且节点的时候   记得要释放内存

一花一世界、一叶一追寻、片片花叶落、情系何人身。
2014-01-02 16:43
wangjiankun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-1-1
收藏
得分:0 
回复 3楼 mic123
真心感谢,但我试了一遍还是不行~~~
2014-01-02 19:06
mic123
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:22
专家分:123
注 册:2013-12-23
收藏
得分:20 
可以试下这个代码,功能应该没问题。
其他的应该有还可以优化的地方,一起讨论
对了 保存个这样的文件
程序代码:
0000000000    CBACBA
2012210155    甘元钊
2012210266    陈越
..
2012210266    陈越



程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

#define LEN sizeof(struct student)

struct student
{
    int number;
    char name[10];
    struct student * next;

};
typedef struct student Struct_stu_p;
//主函数
void main()
{   void print(struct student * head);//声明
    struct student * shanchu(struct student *p3,char k[10]);
    struct student * dig(struct student *p3,char m[10],int n,int u);
    struct student * creat(void);
    struct student * p3,* head1,*tmp;
    int h,n,u;
    char k[10];
    char m[10];
    printf("你想插入哪个人,人名,学号,请输入1,你想删除哪个人,请输入2\n");
    scanf("%d",&h);
    if(h==2)
    {
        scanf("%s",&k);
        p3=creat();
        //print(p3);
        head1=shanchu(p3,k);

        print(head1);

    }
    else
    {
        printf("插入人的姓名及学号,某人前面\n");
        scanf("%s%d%d",m,&n,&u);
        p3=creat();
        head1=dig(p3,m,n,u);
        printf("123456");
        print(head1);
    }
    while (head1 !=NULL)
    {
        tmp=head1;
        head1=head1->next;
        Free_p(&tmp);
    }

}

void Free_p(Struct_stu_p ** tmp)
{
    if(*tmp !=NULL)
    {
        free(*tmp);
        *tmp=NULL;
    }
    //return 1;
}
struct student * creat(void)
{
    //struct student * p1,*p2;
    //struct student * head;
    FILE * fp;
    int n=0;
    int i=0;
    int return_fscanf=0;
    struct student * NodeTmp_fro_file;
    struct student * NodeHead_fro_file=(struct student *)malloc(LEN);


    if((fp=fopen("name2.txt","r"))==NULL)
    {
        printf("cannot open");
        exit(0);
    }
    if((fscanf(fp,"%d %s\n",&NodeHead_fro_file->number,&NodeHead_fro_file->name))!=2)
    {
        printf("fscanf error \n");
    }
    NodeHead_fro_file->next=NULL;
    while(1)
    {
        //printf("%d\n%s\n",NodeHead_fro_file->number,NodeHead_fro_file->name);
        if((NodeTmp_fro_file=(struct student *)malloc(LEN))==NULL)
        {
            printf("NodeTmp_fro_file malloc error!\n");
        }
        if((return_fscanf=fscanf(fp,"%d %s\n",&NodeTmp_fro_file->number,&NodeTmp_fro_file->name))==EOF)
        {
            break;
        }
        else if(return_fscanf !=2)
        {
            printf("fscanf-- error \n");
        }
        NodeTmp_fro_file->next=NodeHead_fro_file;
        NodeHead_fro_file=NodeTmp_fro_file;
    }

    Free_p(&NodeTmp_fro_file);
    if(fp !=NULL)
    {
        fclose(fp);
    }
    return(NodeHead_fro_file);
}
//输出
void print(struct student *  head)
{
    struct student * p;
    p=head;
    while(p!=NULL)
    {
        printf("%d %s\n",p->number,p->name);
        p=p->next;

    }
}
//删除
struct student * shanchu(struct student * p3,char k[])
{
    struct student *p5;
    struct student * p6,*head2;
    p5=p3;
    head2=p3;
    int found_one=0;
    if(p3 ==NULL)
    {
        printf("shanchu-p3 is NULL\n");
    }
    while((strcmp(k,p5->name)!=0)&&(strcmp("CBACBA",p5->name) !=0))
    {

        p6=p5;
        p5=p5->next;
    }
    if(strcmp(k,p5->name)==0)
    {
        found_one++;        //no over
        printf("found_one  %d\n",found_one);
    }
    else
    {
        printf("nothing found in the list \n");
        printf("please input the correct name\n");
    }
    if(p5==head2)
    {
        head2=p5->next;
    }
    else if(p5->next==NULL)
    {
        p6->next=NULL;
    }
    else
    {
        p6->next=p5->next;
    }
    Free_p(&p3);
    Free_p(&p5);
    Free_p(&p6);
    return(head2);
}
//插入
struct student * dig(struct student * p3,char m[],int n,int u)
{
    struct student * head3,*p7,*p8,*p0;
    int i=0;
    struct student p;

    if(p3 == NULL)
    {
        printf("dig-p3 is null\n");
    }
    if((p0=(struct student *)malloc(LEN))==NULL)
    {
        printf("dig-malloc error\n");
    }
    strcpy((p0->name),m);

    p0->number=n;

    p8=p3;

    while(u!=(p8->number))
    {
        p7=p8;
        p8=p8->next;
    }
    p7->next=p0;
    p0->next=p8;
    //printf("found_one  %d\n",p0->number);
    //Free_p(&p0);
    //Free_p(&p7);
    //Free_p(&p8);
    return(p3);
}




[ 本帖最后由 mic123 于 2014-1-3 11:45 编辑 ]
2014-01-03 11:41
快速回复:求解答,c语言链表插入,输出,结果出不来(文件在压缩包里)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025981 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved