链表的各种函数

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

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

struct node *creat1(int n)  //顺序建立链表
{
    struct node *head, *tail, *p;
    head = (struct node *)malloc(sizeof(struct node ));
    head -> next = NULL;
    tail = head;
    for(int i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node ));
        scanf("%d", &p -> data);
        p -> next = NULL;
        tail -> next = p;
        tail = p;
    }
    return head;
}
struct node *creat2(int n)   //逆序建立链表
{
    struct node *head, *p;
    head = (struct node *)malloc(sizeof(struct node));
    head -> next = NULL;
    for(int i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p -> data);
        p -> next = head -> next;
        head -> next = p;
    }
    return head;
}

void Delete1(struct node *&head, int n, int m)   //删除指定元素 n输出长度
{
    struct node *p, *q;
    p = head;
    while(p -> next != NULL)
    {
        if(p -> next -> data == m)
        {
            n--;
            q = p -> next;
            p -> next = q -> next;
            free(q);
        }
        else
            p = p -> next;
    }
    printf("%d\n", n);
}

void Delete2(struct node *&head, int n)  //删除重复元素 输出长度
{
    struct node *p, *q, *t, *tail;
    p = head -> next;
    while(p)
    {
        q = p -> next;
        t = p;
        while(q)
        {
            tail = q;
            if(p -> data == q -> data)
            {
                n--;
                t -> next = q -> next;
                q = q -> next;
                free(tail);
            }
            else
            {
                q = q -> next;
                t = t -> next;
            }
        }
        p = p -> next;
    }
    printf("%d\n", n);
}

void sortline(struct node *&head, int n)  //链表排序
{
    struct node *p, *q;
    while(n--)
    {
        p = head -> next;
        q = p -> next;
        while(p -> next)
        {
            if(p -> data > q -> data)
            {
                int t = p -> data;
                p -> data = q -> data;
                q -> data = t;
            }
            p = p -> next;
            q = q -> next;
        }
    }
}

struct node *unionline(struct node *head1, struct node *head2)  //有序链表的归并
{
    struct node *p1, *p2, *tail;
    p1 = head1 -> next;
    p2 = head2 -> next;
    tail = head1;
    free(head2);
    while(p1 && p2)
    {
        if(p1 -> data < p2 -> data)
        {
            tail -> next = p1;
            tail = p1;
            p1 = p1 -> next;
        }
        else
        {
            tail -> next = p2;
            tail = p2;
            p2 = p2 -> next;
        }
    }
    if(p1)
        tail -> next = p1;
    else
        tail -> next = p2;
    return head1;
}

struct node *split(struct node *head1) //单链表的拆分
{
    int ji, ou;
    ji = ou = 0;
    struct node *head2, *p, *q;
    head2 = (struct node *)malloc(sizeof(struct node));
    head2 -> next = NULL;
    p = head1 -> next;
    head1 -> next = NULL;
    q = p -> next;
    while(p != NULL)
    {
        if(p -> data % 2 == 0)
        {
            ou++;
            p -> next = head1 -> next;
            head1 -> next = p;
        }
        else
        {
            ji++;
            p -> next = head2 -> next;
            head2 -> next = p;
        }
        p = q;
        if(q != NULL)
            q = q -> next;
    }
    printf("%d %d\n", ou, ji);
    return head2;
}

void output(struct node *head)  //链表输出
{
    struct node *r = head;
    while(r -> next -> next != NULL)
    {
        printf("%d ", r -> next -> data);
        r = r -> next;
    }
    printf("%d\n", r -> next -> data);
}
 
View Code

 

 

posted @ 2013-06-17 11:23  GLSilence  阅读(289)  评论(0编辑  收藏  举报