鱼C论坛

 找回密码
 立即注册
查看: 2070|回复: 7

[已解决]怎样实现多重插入?下面代码怎么修改实现多次插入结点?

[复制链接]
发表于 2018-5-27 19:55:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define  LEN sizeof(struct student)
#define  N  3

struct student
{
       int num;
       float score;
       struct student *next;
};
struct student *creat();
struct student *del(struct student *head,int num);
struct student *insert(struct student *head,struct student *stu_2);
void print(struct student *head);
int n;

void main()
{
      struct student *stu;
      struct student std[N]={{},{},{}};
      int n,i;

      stu=creat();
      print(stu);
      printf("\n\n");

      printf("please enter the num to delete:");
      scanf("%d",&n);
      print(del(stu,n));

      for(i=0;i<N;i++)
      {
      printf("\nplease input the num to insert:");
      scanf("%d",&stu[i].num);
      printf("please input the score:");
      scanf("%f",&stu[i].score);

      stu=insert(stu,&stu[i]);
      print(stu);
      }
      printf("\n\n");
}

struct student *creat()
{
    struct student *head;
    struct student *p1,*p2;
    p1=p2=(struct student *)malloc(LEN);
    printf("please enter the num:");
    scanf("%d",&p1->num);
    printf("please enter the score:");
    scanf("%f",&p1->score);
    head=NULL;
    n=0;
    while(p1->num)
    {
         n++;
         if(n==1)
         {
             head=p1;
         }
         else
         {
             p2->next=p1;
         }
         p2=p1;
         p1=(struct student *)malloc(LEN);
         printf("\nplease enter the num:");
         scanf("%d",&p1->num);
         printf("please enter the score:");
         scanf("%f",&p1->score);
    }
    p2->next=NULL;
    return head;
};

struct student *del(struct student *head,int num)
{
       struct student *p1,*p2;
       if(head==NULL)
       {
           printf("\nThis list is null!\n");
           goto END;
       }
       p1=head;
       while(p1->num!=num&&p1->next!=NULL)
       {
           p2=p1;
           p1=p2->next;
       }
       if(p1->num==num)
       {
           if(p1==head)
           {
               head=p1->next;
           }
           else
           {
               p2->next=p1->next;
           }
           printf("\nDelete No: %d succeed!\n",num);
           n=n-1;
       }
       else
       {
           printf("%d not been found!\n",num);
       }
END:
      return head;
};

struct student *insert(struct student *head,struct student *stu_2)
{
      struct student *p0,*p1,*p2;
      p1=head;
      p0=stu_2;
      if(NULL==head)
      {
          head=p0;
          p0->next=NULL;
      }
      else
      {
            while((p0->num>p1->num)&&(p1->next!=0))
            {
               p2=p1;
               p1=p1->next;
            }
            if(p0->num<=p1->num)
            {
               if(p1==head)
               {
                 head=p0;
               }
               else
               {
                   p2->next=p0;
               }
               p0->next=p1;
            }
            else
            {
                p1->next=p0;
                p0->next=NULL;
            }
      }
      n=n+1;
      return head;
};
void print(struct student *head)
{
      struct student *p;
      printf("\nThere are %d records!\n\n",n);
      p=head;
      if(head)
      {
          do
          {
              printf("学号为%d的成绩是:%f\n",p->num,p->score);
              p=p->next;
          }while(p);
      }
}
最佳答案
2018-6-3 20:41:47
~白. 发表于 2018-6-3 19:17
恩,如果要一次插入多个内容该怎么做?
不好意思没注释,让你费力了,下次一定注释,上面是生成链表删除 ...

你的意思是想像字符串扩展一样,可以将两个不同长度的链表合成一个?
其实这个是可行并且无意义的,
架设有一个链表头指针 head1,以及需要插入的另一个链表头指针head2,以及临时指针temp,使用头插法。

  1. temp = head1; //将head1储存到临时指针
  2. head1 = head2; //将head1赋值为head2
  3. /* 这里就相当于把head2这个列表接入head1的头*/
  4. /* 将head2列表眺到最后一个结构,我这里省略*/
  5. head2->next = temp; //将原列表后面的结构连上来
复制代码


其实对于链表来说,不管是一次添加一个还是一次添加多个,都是同样的操作方法。而这种方法的前提是这两个链表应使用同一种结构。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-2 22:58:20 | 显示全部楼层
能再所清除点吗?是一次性插入多个内容?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-3 19:17:10 | 显示全部楼层
simplerjiang 发表于 2018-6-2 22:58
能再所清除点吗?是一次性插入多个内容?

恩,如果要一次插入多个内容该怎么做?
不好意思没注释,让你费力了,下次一定注释,上面是生成链表删除链表和插入合在一起的代码,不过插入程序只能插入一个内容,插入多个就会出错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-3 20:41:47 | 显示全部楼层    本楼为最佳答案   
~白. 发表于 2018-6-3 19:17
恩,如果要一次插入多个内容该怎么做?
不好意思没注释,让你费力了,下次一定注释,上面是生成链表删除 ...

你的意思是想像字符串扩展一样,可以将两个不同长度的链表合成一个?
其实这个是可行并且无意义的,
架设有一个链表头指针 head1,以及需要插入的另一个链表头指针head2,以及临时指针temp,使用头插法。

  1. temp = head1; //将head1储存到临时指针
  2. head1 = head2; //将head1赋值为head2
  3. /* 这里就相当于把head2这个列表接入head1的头*/
  4. /* 将head2列表眺到最后一个结构,我这里省略*/
  5. head2->next = temp; //将原列表后面的结构连上来
复制代码


其实对于链表来说,不管是一次添加一个还是一次添加多个,都是同样的操作方法。而这种方法的前提是这两个链表应使用同一种结构。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-3 20:57:13 | 显示全部楼层
simplerjiang 发表于 2018-6-3 20:41
你的意思是想像字符串扩展一样,可以将两个不同长度的链表合成一个?
其实这个是可行并且无意义的,
架 ...

我说错了,不是插入链表而是插入多个数据上面insert函数就是插入函数不过只能插入一个数据插入两个就不行了,我就想知道怎么插入多个数据进入链表
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-3 20:59:38 | 显示全部楼层
~白. 发表于 2018-6-3 20:57
我说错了,不是插入链表而是插入多个数据上面insert函数就是插入函数不过只能插入一个数据插入两个就不行 ...

不好意思我绕进死胡同了,你说的对,我脑子一开始想的是按顺序插入,仔细想想插入一个链表就可以了,谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-3 21:02:05 | 显示全部楼层
~白. 发表于 2018-6-3 20:59
不好意思我绕进死胡同了,你说的对,我脑子一开始想的是按顺序插入,仔细想想插入一个链表就可以了,谢谢 ...


众所周知,链表其实只是一堆散乱的数据罢了,
对于我们来说他才是一条直线
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-3 21:03:46 | 显示全部楼层
simplerjiang 发表于 2018-6-3 21:02
众所周知,链表其实只是一堆散乱的数据罢了,
对于我们来说他才是一条直线

恩,谢谢大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 14:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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