List of Retrograde Collection

Retrograde list

#include<bits/stdc++.h>
using namespace std;

struct LNode
{
    int data;
    LNode *next;
};
void creathead(LNode *&L,int a[],int n)///尾插法
{
    LNode *r,*s;
    L=(LNode*)malloc(sizeof(LNode));
    L->next=NULL;
    r=L;
    for(int i=0; i<n; i++)
    {
        s=(LNode*)malloc(sizeof(LNode));
        s->data=a[i];
        r->next=s;
        r=r->next;
    }
    r->next=NULL;
}
void reverse(LNode *&L)
{
    LNode *p,*q;
    p=L->next;
    L->next=NULL;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
        q->next=L->next;
        L->next=q;
    }
}
int main()
{
    LNode *L,*B;
    int n,c[10000];
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&c[i]);
    creathead(L,c,n);
    reverse(L);
    while(L->next!=NULL)
    {
        printf("%d ",L->next->data);
        L=L->next;
    }
    printf("\n");
    return 0;
}

A list decompose an even and odd list list

#include<bits/stdc++.h>
using namespace std;

struct LNode
{
    int data;
    LNode *next;
};
void creathead(LNode *&L,int a[],int n)///尾插法
{
    LNode *r,*s;
    L=(LNode*)malloc(sizeof(LNode));
    L->next=NULL;
    r=L;
    for(int i=0; i<n; i++)
    {
        s=(LNode*)malloc(sizeof(LNode));
        s->data=a[i];
        r->next=s;
        r=r->next;
    }
    r->next=NULL;
}
/把A分解为A里面节点值为偶数
    B里面节点值为奇数*/

void split(LNode *A,LNode *&B)
{
    LNode *p,*q,*r;
    B=(LNode*)malloc(sizeof(LNode));
    B->next=NULL;
    p=A;
    r=B;
    while(p->next!=NULL)
    {
        if(p->next->data%2==0)
        {
            q=p->next;
            p->next=q->next;
            q->next=NULL;
            r->next=q;///尾插
            r=q;
        }
        else
            p=p->next;
    }
}



int main()
{
    LNode *L,*B;
    int n,c[10000];
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&c[i]);
    creathead(L,c,n);
    split(L,B);
    printf("奇数\n");
    while(L->next!=NULL)
    {
        printf("%d ",L->next->data);
        L=L->next;
    }
    printf("\n");
    printf("偶数\n");
    while(B->next!=NULL)
    {
        printf("%d ",B->next->data);
        B=B->next;
    }
    return 0;
}

The two merged into a decreasing increments list list

#include<bits/stdc++.h>
using namespace std;

struct LNode
{
    int data;
    LNode *next;
};

void merge(LNode *&A,LNode *B)
{
    LNode *p=A->next;
    LNode *q=B->next;
    LNode *s;
    A->next=NULL;
    while(p!=NULL && q!=NULL)
    {
        if(p->data <= q->data)
        {
            s=p;
            p=p->next;
            s->next=A->next;
            A->next=s;
        }
        else
        {
            s=q;
            q=q->next;
            s->next=A->next;
            A->next=s;
        }
    }
    while(p!=NULL)
    {
        s=p;
        p=p->next;
        s->next=A->next;
        A->next=s;

    }
    while(q!=NULL)
    {
        s=q;
        q=q->next;
        s->next=A->next;
        A->next=s;
    }
}
void creathead(LNode *&L,int a[],int n)///尾插
{
    LNode *r,*s;
    L=(LNode*)malloc(sizeof(LNode));
    L->next=NULL;
    r=L;
    for(int i=0; i<n; i++)
    {
        // printf("1111\n");
        s=(LNode*)malloc(sizeof(LNode));
        s->data=a[i];
        r->next=s;
        r=r->next;
    }
    r->next=NULL;

}

int main()
{
    LNode *A,*B;
    int a[1000],b[1000];
    int n,m;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int i=0; i<m; i++)
        scanf("%d",&b[i]);
     creathead(A,a,n);
    creathead(B,b,m);
    merge(A,B);
    while(A->next!=NULL)
    {
        printf("%d ",A->next->data);
        A=A->next;
    }
    return 0;
}

A list into two lists:
a first list containing a list of the original number of nodes is odd
second linked list sequence number contained in the original list of nodes is an even number

#include<bits/stdc++.h>
using namespace std;

struct LNode
{
    int data;
    int num;
    LNode *next;
};

void bianhao(LNode *L)
{
    int i=0;
    while(L->next!=NULL)
    {
        i++;
        L->next->num=i;
        L=L->next;
    }
}
void split(LNode *A,LNode *&B)
{
    LNode *p,*q,*r;
    B=(LNode*)malloc(sizeof(LNode));
    B->next=NULL;
    p=A;
    r=B;
    while(p->next!=NULL)
    {
        if(p->next->num%2==0)
        {
            q=p->next;
            p->next=q->next;
            q->next=NULL;
            r->next=q;
            r=q;
        }
        else
            p=p->next;
    }
}
void creathead(LNode *&L,int a[],int n)///头插法
{
    LNode *r,*s;
    L=(LNode*)malloc(sizeof(LNode));
    L->next=NULL;
    r=L;
    for(int i=0; i<n; i++)
    {
        s=(LNode*)malloc(sizeof(LNode));
        s->data=a[i];
        r->next=s;
        r=r->next;
    }
    r->next=NULL;
}

int main()
{
    LNode *A,*B;
    int a[1000];
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    creathead(A,a,n);
    bianhao(A);
    split(A,B);
    printf("奇数\n");
    while(A->next!=NULL)
    {
        printf("%d ",A->next->data);
        A=A->next;
    }
    printf("\n");
    printf("偶数\n");
    while(B->next!=NULL)
    {
        printf("%d ",B->next->data);
        B=B->next;
    }
    return 0;
}

A list into two lists
of the list contains a list of nodes in the original 0 is less than
a second linked list of nodes contained in the original list of greater than 0

#include<bits/stdc++.h>
using namespace std;

struct LNode
{
    int data;
    struct LNode *next;
};

void create(LNode *L,int a[],int n)///尾插
{
    LNode *r,*s;
    L=(LNode*)malloc(sizeof(LNode));
    L->next=NULL;
    r=L;
    for(int i=0; i<n; i++)
    {
        s=(LNode*)malloc(sizeof(LNode));
        s->data=a[i];
        r->next=s;
        r=r->next;
    }
    r->next=NULL;

}
void solve(LNode *L,LNode *&B,LNode *&C)
{
    LNode *p,*q,*r,*h;
    B=(LNode*)malloc(sizeof(LNode));
    C=(LNode*)malloc(sizeof(LNode));
    B->next=NULL;
    C->next=NULL;
    p=L->next;
    q=B;
    r=C;
    while(p)
    {
        if(p->data < 0)
        {
            h=p;
            p=p->next;//尾插
            q->next=h;
            q=h;
        }
        else
        {
            h=p;
            p=p->next; ///尾插
            r->next=h;
            r=h;
        }
    }
    while(B->next!=NULL)
    {
        printf("%d ",B->next->data);
        B=B->next;
    }
    while(C->next!=NULL)
    {
        printf("%d ",C->next->data);
        C=C->next;
    }
}

int main()
{
    int n,a[100];
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    LNode *L,*B,*C;
    create(L,a,n);
    solve(L,B,C);
    return 0;
}
Published 72 original articles · won praise 5 · Views 6322

Guess you like

Origin blog.csdn.net/AYSXY/article/details/104476744