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

📄 5.2.cpp

📁 好啊
💻 CPP
字号:
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node{
int data;
struct  node  *next;
}LinkList;

LinkList *create (LinkList *head){
LinkList *p,*q,*r;
int k;q=head;
scanf ("%d",&k);
while( k>0){ 
    	   p=(LinkList *)malloc(sizeof(LinkList));
     	   p->data=k;
     	   q->next=p;
           q=p;
      	scanf ("%d",&k);
    }
q->next = NULL;
    r=head->next;
    return(r);
}

void display(LinkList *head)
{
	LinkList *p;
	p=head->next;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	cout<<endl;
}


LinkList *mergelink(LinkList *p,LinkList *q)
{
    LinkList *h;LinkList *r;
   	h=(LinkList *)malloc(sizeof(LinkList));
   	h->next=NULL;r=h;
   	while( p!=NULL&&q!=NULL)
	{ 
   		if (p->data<=q->data)
		{
            r->next=p;
            r=p;
            p=p->next;
		}
     	else
		{ 
           r->next=q;
           r=q;
           q=q->next;
		}
     }
   if (p==NULL) r->next=q;
   else    r->next=p;
   return  h;
} 


void main()
{
	LinkList *L,*S,*M,*N,*K;
    L=(LinkList *)malloc(sizeof(LinkList));
	S=(LinkList *)malloc(sizeof(LinkList));
	cout<<"请建立一个单链表:"<<endl;
	M=create(L);
	cout<<"单链表的内容为:"<<endl;
	display(L);
	cout<<"再建立一个单链表:"<<endl;
    N=create(S);
	cout<<"单链表的内容为:"<<endl;
	display(S);
	K=mergelink(M,N);
	cout<<"合并的新链表为:"<<endl;
    display(K);
}

⌨️ 快捷键说明

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