网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

合并两个有序的链表

#include
#include
#include
 
struct Listnode
{
    int _value;
    Listnode* _next;
};
void Init(Listnode*& head)
{
    Listnode* cur =head;
    if(cur==NULL)
    {
        cur=(Listnode*)malloc(sizeof(Listnode));
        cur->_next=NULL;
        cur->_value=0;
    }
    head=cur;
}
 
void push(Listnode*& head,int value)
{
    Listnode* cur =head;
 
        while(cur->_next)
        {
            cur=cur->_next;
        }
        Listnode* tmp=NULL;
        tmp=(Listnode*)malloc(sizeof(Listnode));
        tmp->_next=NULL;
        tmp->_value=value;
        cur->_next=tmp;
     
         
 
}
void pop(Listnode* head)
{
    Listnode* cur=head;
    Listnode* prev=NULL;
    while(cur->_next!=NULL)
    {
        prev=cur;
        cur=cur->_next;
    }
    prev->_next=NULL;
    free(cur);
    cur=NULL;
}
void print(Listnode* head)
{
    Listnode* cur=head;
    while(cur)
    {
        printf("%d\n",cur->_value);
        cur=cur->_next;
    }
}
Listnode* reverse(Listnode* head)
{
	Listnode* newhead=NULL;
	if(head==NULL||head->_next==NULL)
	{
		return head;
	}
	while(head)
	{
		Listnode* tmp=head;
		head=head->_next;
		tmp->_next=newhead;
		newhead=tmp;
		
	}
	return newhead;
}
Listnode* merge(Listnode* head1,Listnode* head2)
{
	if(head1==NULL&&head2!=NULL)
	{
		return head2;
	}
	else if(head2==NULL&&head1!=NULL)
	{
		 return head1;
	}
	else if(head1==NULL&&head2==NULL)
	{
		return NULL;
	}
	Listnode* newhead=NULL;
	Init(newhead);
	head1=head1->_next;
	head2=head2->_next;
	Listnode* cur=newhead;
	while(head1&&head2)
	{
		if(head1->_value>head2->_value)
		{
			cur->_next=head2;
			cur=cur->_next;
			head2=head2->_next;
		}
		else
		{
			cur->_next=head1;
			cur=cur->_next;
			head1=head1->_next;
		}
	}
	if(head1==NULL&&head2!=NULL)
	{
		cur->_next=head2;
		cur=cur->_next;
	}
	else if(head2==NULL&&head1!=NULL)
	{
		cur->_next=head1;
		cur=cur->_next;
	}
	return newhead;
}

void test()
{
    Listnode* head=NULL;
    Init(head);
    push(head,1);
    push(head,3);
    push(head,5);
	push(head,7);
	push(head,9);

    /*pop(head);*/
     Listnode* head2=NULL;
    Init(head2);
    push(head2,0);
    push(head2,2);
    push(head2,4);
	push(head2,6);
	push(head2,8);
	push(head2,10);

	Listnode* newhead=merge(head,head2);
	print(newhead);
  

   
 
}
int main()
{
    test();
    system("pause");
    return 0;
}

结果:

目前成都创新互联公司已为近1000家的企业提供了网站建设、域名、虚拟主机网站托管、企业网站设计、民勤网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

合并两个有序的链表


当前题目:合并两个有序的链表
本文链接:http://bjjierui.cn/article/pggejh.html

其他资讯