| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 605 人关注过本帖
标题:求解答,c语言链表插入,输出,结果出不来(文件在压缩包里)
取消只看楼主 加入收藏
wangjiankun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-1-1
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:1 
求解答,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
wangjiankun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-1-1
收藏
得分:0 
回复 3楼 mic123
真心感谢,但我试了一遍还是不行~~~
2014-01-02 19:06
快速回复:求解答,c语言链表插入,输出,结果出不来(文件在压缩包里)
数据加载中...
 
   



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

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