• 链表的各种函数


    # 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);
    }
    
    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);
    }
    
    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
    ", 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
    ", r -> next -> data);
    }
     
    View Code
  • 相关阅读:
    Java代码输出到txt文件(申请专利贴源码的必备利器)
    Vmware10组建局域网
    Ubuntu14.04更换阿里云源
    Ubuntu16.04如何彻底删除Apache2
    HustOJ平台搭建
    Centos 7 联想Y430P无线网卡驱动安装 过程参考
    Windows远程CentOS桌面
    centos 7 查看系统/硬件信息及运维常用命令+联想Y430P无线网卡驱动安装
    zookeeper工作原理、安装配置、工具命令简介
    centos 7 安装五笔输入法
  • 原文地址:https://www.cnblogs.com/Silence-AC/p/3139909.html
Copyright © 2020-2023  润新知