zoukankan      html  css  js  c++  java
  • 已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并。按学号升序排列.

    #include <stdio.h>
    #define SIZE sizeof(struct student)
    struct student
    {
           long num;
           float score;
           struct student *next;
    };

    struct student * create();
    struct student * input();
    void print(struct student *);
    struct student * union_linktable(struct student *,struct student *);
    struct student * insert(struct student *,struct student *);
    int main(int argc,char *argv[])
    {
        struct student *head1,*head2;
        printf("please input linktable1: ");
        head1 = (struct student *)create();
        printf("the lintable1 data is ");
        print(head1);
        printf("please input linktable2: ");
        head2 = (struct student *)create();
        printf("the lintable2 data is ");
        print(head2);
        printf("union linktable1 and linktable2: ");
        head1 = union_linktable(head1,head2);
        print(head1);
        system("pause");
        return 0;
    }

    struct student * create()
    {
           int n = 0;
           struct student *head;
           struct student *p1,*p2;
           head = NULL;
           do 
           {
              p1 = input();
              if (0 == p1->num)
              {
                 break;
              }
              else
              {
                  if (0 == n)
                  {
                     head = p1;
                  }
                  else
                  {
                      p2->next = p1;
                  }
                  p2 = p1;
                  n++;
              }
           }while(1);
           p2->next = NULL;
           return head;
    }

    struct student * input()
    {
           struct student *p;
           p = (struct student *)malloc(SIZE);
           printf("please input num,score:");
           scanf("%ld,%f",&p->num,&p->score);
           return p;
    }

    void print(struct student *head)
    {
         struct student *p;
         p = head;
         if (head != NULL)
         {
            do
            {
              printf("num:%ld,score:%5.1f ",p->num,p->score);
              p = p->next;
            }while(p != NULL);
         }
         else
         {
             printf("error:linktable data is NULL. ");
         }
    }

    struct student * union_linktable(struct student *head1,struct student *head2)
    {
           struct student *pa1,*pa2,*pb1,*pb2;
           pa1 = pa2 = head1;
           pb1 = pb2 = head2;
           do
           {
             while ((pb1->num > pa1->num) && (pa1->next) != NULL)
             {
                   pa2 = pa1;
                   pa1 = pa1->next;
             }
             if (pb1->num <= pa1->num)
             {
                if(head1 == pa1)
                {
                         head1 = pb1;
                }
                else
                {
                    pa2->next = pb1;
                }
                pb2->next = pa1;
                pa2 = pb2;
                pb2 = pb1;
             }
           }while((pa1->next != NULL) || (pa1 == NULL && pb1 != NULL));
           if ((pb1 != NULL) && (pb1->num > pa1->num) &&(pa1->next == NULL))
           {
              pa1->next = pb1;
           }
           return head1;
    }

    struct student *insert(struct student *head,struct student *stu)
    {
           
           struct student *p0,*p1,*p2;
           p1 = head;
           p0 = stu;
           if(NULL == head)
           {
                   head = p0;
                   p0->next = NULL;
                   
           }
           else
           {
               while (p0->num > p1->num && p1->next != NULL)
               {
                     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;
               } 
           }
           return head;
    }

  • 相关阅读:
    【题解】 bzoj2748 [HAOI2012]音量调节 (动态规划)
    【题解】 bzoj1190: [HNOI2007]梦幻岛宝珠 (动态规划)
    【题解】 bzoj1864: [Zjoi2006]三色二叉树 (动态规划)
    【题解】 [ZJOI2006]书架 (Splay)
    【题解】 [HNOI2004]宠物收养场(Splay)
    【题解】 [HNOI2002]营业额统计 (Splay)
    【题解】 [ZJOI2008] 泡泡堂(贪心/二分图/动态规划)
    【题解】 [SDOI2009] Elaxia的路线(最短路+拓扑排序)
    Aptana Studio 3 如何汉化,实现简体中文版
    js中获得当前时间是年份和月份
  • 原文地址:https://www.cnblogs.com/aspirant/p/4028294.html
Copyright © 2011-2022 走看看