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

    #include <stdio.h>
    #include 
    <stdlib.h>
    #define N 10
    typedef 
    struct student
    {
        
    int num;
        
    float score;
        
    struct student *next;
    }STU;

    STU 
    *create()
    {
        
    int i;
        STU 
    *p,*head=NULL,*tail=head;
        
    for (i=0;i<N;i++)
        {
            p
    =(STU *)malloc(sizeof(STU));
            scanf(
    "%d%f",&p->num,&p->score);
            p
    ->next=NULL;
            
    if (p->num<0)
            {
                free(p);
                
    break;
            }
            
    if(head==NULL)
                head
    =p;
            
    else
                tail
    ->next=p;
            tail
    =p;
        }
        
    return head;
    }

    void output(STU *p)
    {
        
    while (p!=NULL)
        {
            printf(
    "%d\t%.2f\n",p->num,p->score);
            p
    =p->next;
        }
    }

    STU 
    *link(STU *p1,STU *p2)
    {
        STU 
    *p,*head;
        
    if (p1->num<p2->num)
        {
            head
    =p=p1;
            p1
    =p1->next;
        }
        
    else
        {
            head
    =p=p2;
            p2
    =p2->next;
        }
        
    while (p1!=NULL&&p2!=NULL)
        {
            
    if (p1->num<p2->num)
            {
                p
    ->next=p1;
                p
    =p1;
                p1
    =p1->next;
            }
            
    else
            {
                p
    ->next=p2;
                p
    =p2;
                p2
    =p2->next;
            }
        }
        
    if(p1!=NULL)
            p
    ->next=p1;
        
    else
            p
    ->next=p2;

        
    return head;
    }

    int main(int argc, char *argv[]) 
    {
        STU 
    *a,*b,*c;
        printf(
    "\n请输入链表a的信息,学号小于零时结束输入:格式(学号 成绩)\n");
        a
    =create();
        printf(
    "\n请输入链表b的信息,学号小于零时结束输入:格式(学号 成绩)\n");
        b
    =create();
        printf(
    "\n链表a的信息为:\n");
        output(a);
        printf(
    "\n链表b的信息为:\n");
        output(b);
        c
    =link(a,b);
        printf(
    "\n合并后的链表信息为:\n");
        output(c);

        
    return 0;
    }
  • 相关阅读:
    如何用js刷新aspxgridviw
    ASPxSpinEdit 控件的三元判断
    关于cookie
    asp.net解决数据转换为DBNULL的问题
    Devexpress 中如何写ASPxGridView新增修改时的数据验证
    ASPxGridView中批量提交及个别提交的写法
    c#中如何做日期的三元判断(日期不为空赋值)
    c#中如何不通过后台直接用js筛选gridview中的数据条件筛选查询?
    devexpress中如何绑定ASPxTreeList控件
    如何在后台动态生成ASPxCheckBoxList标签并循环(数据调用存储过程)
  • 原文地址:https://www.cnblogs.com/qixin622/p/1238758.html
Copyright © 2011-2022 走看看