單鏈表升序合併和降序合併(新版)

#include <iostream>
#include<stdlib.h>
using namespace std;

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


/*void listCreateByHead(Lnode *&h)//create a list by inserting into the head always
{
    Lnode *newnode;
    for(int i=0; i<6; i++)
    {
        newnode=(Lnode*)malloc(sizeof(Lnode));
        newnode->data=i;
        newnode->next=h->next;
        h->next=newnode;
    }
}*/

void listMergeUp(Lnode *&h1,Lnode *&h2,Lnode *&c)//merge h1 and h2 to be C in an ascending way.
{
    Lnode *p=h1->next;
    Lnode *q=h2->next;

    Lnode *r=NULL;//pointer r points to the tail if c.

    c=h1;//choose h1 as c's head(h2 can do the same thing).
    c->next=NULL;
    free(h2);//now,h2 is useless.
    r=c;//at the beginning,c's head is it's tail too.

    while((p!=NULL)&&(q!=NULL))
    {
        if(p->data<=q->data)
        {
            r->next=p;
            p=p->next;
            r=r->next;
        }
        else
        {
            r->next=q;
            q=q->next;
            r=r->next;
        }

        r->next=NULL;
    }
    if(p!=NULL)
    {
        r->next=p;
    }
    if(q!=NULL)
    {
        r->next=q;
    }
}

void listMergeDown(Lnode *&h1,Lnode *&h2,Lnode *&c)//merge h1 and h2 to be C in a descending way.
{
    Lnode *p=h1->next;
    Lnode *q=h2->next;

    Lnode *s=NULL;//pointer s is used to point to a new temp node.

    c=h1;
    c->next=NULL;
    free(h2);//now,h2 is useless.

    while((p!=NULL)&&(q!=NULL))
    {
        if(p->data<=q->data)
        {
            s=p;
            p=p->next;
            s->next=c->next;
            c->next=s;
        }
        else
        {
            s=q;
            q=q->next;
            s->next=c->next;
            c->next=s;
        }

    }
    while(p!=NULL)//insert the rest of p into c one by one.
    {
        s=p;
        p=p->next;
        s->next=c->next;
        c->next=s;
    }
    while(q!=NULL)//insert the rest of q into c one by one.
    {
        s=q;
        q=q->next;
        s->next=c->next;
        c->next=s;
    }
}

void listDestroy(Lnode *&h)//release h's space
{
    Lnode *temp=h->next;
    while(temp!=NULL)
    {
        free(h);
        h=temp->next;
        temp=temp->next;
    }
    free(h);
    cout<<"List is destroyed."<<endl;
}

void printstate(Lnode *h)
{
    h=h->next;
    while(h!=NULL)
    {
        cout<<h->data<<" ";
        h=h->next;
    }
    cout<<endl;
}

int main()
{
    Lnode *h1=(Lnode*)malloc(sizeof(Lnode));
    Lnode *h2=(Lnode*)malloc(sizeof(Lnode));
    Lnode *c=(Lnode*)malloc(sizeof(Lnode));
    h1->next=NULL;
    h1->next=NULL;
    c->next=NULL;

    Lnode *newnode;
    for(int i=7; i>=1; i-=2)//init list h1
    {
        newnode=(Lnode*)malloc(sizeof(Lnode));
        newnode->data=i;
        newnode->next=h1->next;
        h1->next=newnode;
    }
    for(int i=8; i>=2; i-=2)//init list h2
    {
        newnode=(Lnode*)malloc(sizeof(Lnode));
        newnode->data=i;
        newnode->next=h2->next;
        h2->next=newnode;
    }

    listMergeUp(h1,h2,c);
    //listMergeDown(h1,h2,c);
    printstate(c);
    listDestroy(c);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章