Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

HUIS

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


struct el
{
    int ch;
    struct el *p;
};




void slist(struct el *p11, struct el *p22, struct el **p3)
{
    struct el *p4,*p1,*p2;
    p1=p11;
    p2=p22;
    if (p1->ch <= p2->ch)
    {
        *p3=p1;
        p4=p1;
        p1=p1->p;
    }
    else
    {
        *p3=p2;
        p4=p2;
        p2=p2->p;
    }
    while ((p1!=NULL)&&(p2!=NULL))
        if (p1->ch <= p2->ch)
        {
            p4->p=p1;
            p4=p1;
            p1=p1->p;
        }
        else
        {
            p4->p=p2;
            p4=p2;
            p2=p2->p;
        }
        if (p1!=NULL) p4->p=p1;
        else p4->p=p2;
}



void sortlist(struct el **p, int n)
{
    struct el *p1,*p2;
    int k,i;
    if (n>1)
    {
        k=n/2;
        p1=*p;
        for (i=1; i<k-1; i++) p1=p1->p;
        
        p2=p1->p;
        p1->p=NULL;
        p1=*p;
        
        sortlist(&p1,k);
        sortlist(&p2,n-k);
        slist(p1,p2,p);
    }
}



int main()
{
    int n = 100;
    srand(time(NULL));
    int i;
    struct el *p1,*p2,*p3;
    p1 = new el;
    p2 = p1;
    for (i=0; i < n; i++)
    {
        p2->ch = rand() % 2000 - 999;
        if (i<n-1)
        {
            p3 = new struct el;
            p2->p = p3;
            p2 = p3;
        }
    }
    p2->p = NULL;
    
    p3=p1;
    for (i=0; i <n; i++)
    {
        cout << p3->ch <<"\n";
        p3=p3->p;
    }
    
    sortlist(&p1,n);
    
    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.