⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xxbgb.cpp

📁 首先
💻 CPP
字号:
#include<stdio.h>
#include<malloc.h>

typedef struct node{
    int data;
    struct node * next;
}Lnode;

Lnode * create(int tag)//用前插法建立单链表
{
    Lnode *p,*h=NULL;
    int x;
    printf("请输入元素值(数字):");
    scanf("%d" ,&x);
    while(x!=tag)
    {
        p=(Lnode *)malloc(sizeof(Lnode));
        p->data =x;
        p->next=h;
        h=p;
        scanf("%d",&x);
    }
    return h;
}

void printd(Lnode *h)//打印单链表中的一个节点的数据域值
{
    while(h!=NULL)
    {printf("%3d",h->data);
    h=h->next;}
    printf("\n");
}

Lnode *mergeab(Lnode *ha, Lnode *hb)//归并单链表
{
    Lnode *p,*q,*r,*head=(Lnode*)malloc(sizeof(Lnode));
    head->next=hb;
    while(ha){
        p=ha;ha=p->next;
        r=head;hb=head->next;
        while(hb){
            if(p->data<hb->data){
                r->next=p;
                p->next=hb;break;}
              else
                 if(p->data==hb->data)
                  free(p);
             else{
                   r=hb;hb=r->next;}
        }
     }
     hb=head->next;
     free(head);
     return hb;
 }
 main()
 {
     Lnode *ha,*hb;
     int tag=0;
     printf("输入完成后请敲击回车后以0结束\n");
     printf("------------------------------------\n");
     printf(" 请输入ha 中元素:\n");
     ha=create(tag);
printf("ha中的元素为:") ;
     printd(ha);
printf("------------------------------------\n");
     printf("输入hb 中元素:\n");
     hb=create(tag); 
printf("ha中的元素为:") ;
     printd(hb);
     hb=mergeab(ha,hb);printf("------------------------------------\n");
     printf("归并后的数字为:");
     printd(ha);
    getchar();getchar();
    
}              

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -