阶段性综合练习一下,将链表的创建,合并,插入,删除,拆分,逆置,打印都练习了一下。

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    struct node *next;
}Node,*pNode;

int main()
{
    void create(pNode);
    void merge(pNode,pNode);
    void print(pNode);
    void insert_Node(pNode,pNode);
    void delete_Node(pNode,int);
    void depart(pNode,pNode);
    void reverse(pNode);
    pNode A,B;
    A=(pNode)malloc(sizeof(Node));
    B=(pNode)malloc(sizeof(Node));
    printf("Input link A:\n");
    create(A);/*创建链表A*/
    printf("Input link B:\n");
    create(B);/*创建链表B*/
    merge(A,B);/*合并两个链表*/
    printf("After merge:\n");
    print(A);
    pNode C=(pNode)malloc(sizeof(Node));
    printf("\nEnter a New Node: ");
    scanf("%d",&C->data);
    insert_Node(A,C);/*在链表中插入一个节点*/
    print(A);
    printf("\nEnter a Node to delete: ");
    int num;
    scanf("%d",&num);
    delete_Node(A,num);/*删除链表中某个节点*/
    print(A);
    depart(A,B);/*将链表中节点按奇偶数分离*/
    printf("\nlink A:\n");
    print(A);
    printf("\nlink B:\n");
    print(B);
    merge(A,B);/*再次将链表合并*/
    printf("\nReverse the link:\n");
    reverse(A);/*将链表逆置*/
    print(A);
    return 0;
}
/*创建一个链表*/
void create(pNode head)
{
    int n=0;
    pNode p,q;
    p=q=(pNode)malloc(sizeof(Node));
    scanf("%d",&p->data);
    while(p->data!=0)
    {
        ++n;
        if(n==1)
            head->next=p;
        else
            q->next=p;
        q=p;
        p=(pNode)malloc(sizeof(Node));
        scanf("%d",&p->data);
    }
    q->next=NULL;
}

/*向链表中插入一个节点*/
void insert_Node(pNode head,pNode newNode)
{
    pNode p1,p2;
    p1=p2=head->next;
    if(p1==NULL)
        head->next=newNode;
    else
    {
        while(p1->data<newNode->data&&p1->next!=NULL)
        {
            p2=p1;
            p1=p1->next;
        }
        if(p1->data>=newNode->data)
        {
            if(p1==p2)
                head->next=newNode;
            else
                p2->next=newNode;
            newNode->next=p1;
        }
        else
        {
            p1->next=newNode;
            newNode->next=NULL;
        }
    }
}
/*删除链表中某个元素*/
void delete_Node(pNode head,int num)
{
    pNode p1,p2;
    p1=p2=head->next;
    if(p1==NULL)
        printf("\nList is null!\n");
    else
    {
        while(p1->data!=num&&p1->next!=NULL)
        {
            p2=p1;
            p1=p1->next;
        }
        if(p1->data==num)
        {
            if(p1==p2)
                head->next=p1->next;
            else
                p2->next=p1->next;
            printf("\ndelete: %d\n",num);
        }
        else
            printf("\nNot find %d\n",num);
    }
}

/*合并链表*/
void merge(pNode a,pNode b)
{
    pNode c,pa,pb;
    c=a;
    pa=a->next;
    pb=b->next;
    while(pa!=NULL&&pb!=NULL)
    {
        if(pa->data<pb->data)
        {
            printf("\n*a=%d,b=%d*\n",pa->data,pb->data);
            c->next=pa;
            c=c->next;
            pa=pa->next;
        }
        else if(pb->data<pa->data)
        {
            printf("\n**a=%d,b=%d**\n",pa->data,pb->data);
            c->next=pb;
            c=c->next;
            pb=pb->next;
        }
        else if(pa->data==pb->data)
        {
            c->next=pa;
            c=c->next;
            pa=pa->next;
            c->next=pb;
            c=c->next;
            pb=pb->next;
        }
    }
    if(pa==NULL)
        c->next=pb;
    if(pb==NULL)
        c->next=pa;
}


/*按奇偶数分离链表*/
void depart(pNode A,pNode B)
{
    pNode pa1,pa2,pb;
    pa1=pa2=A->next;
    pb=B;
    while(pa1!=NULL)
    {
        while(pa1->data%2!=0&&pa1->next!=NULL)
        {
            pa2=pa1;
            pa1=pa1->next;
        }
        if(pa1->data%2==0)
        {
            if(pa1==pa2)
            {
                A->next=pa1->next;
                pb->next=pa1;
                pb=pb->next;
                pa1=pa2=A->next;
            }
            else
            {
                pa2->next=pa1->next;
                pb->next=pa1;
                pb=pb->next;
                pa1=pa2->next;
            }

        }
        else
        {
            pa2=pa1;
            pa1=pa1->next;
        }
    }
    pb->next=NULL;
    pa2->next=NULL;

}

/*链表逆置*/
void reverse(pNode head)
{
    pNode p0,p1,p2;
    if(head->next==NULL&&head->next->next==NULL)
        return;
    else
    {
        p0=NULL;
        p2=head->next;
        p1=p2->next;
        while(p1!=NULL)
        {
            p2->next=p0;
            p0=p2;
            p2=p1;
            p1=p1->next;
        }
        p2->next=p0;
        head->next=p2;
    }
}
/*打印链表*/
void print(pNode head)
{
    pNode p=head->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}