双链表相关操作

单链表学习完,完成一下双链表的练习,本人小白,还望大佬指正!

此图为选择排序第一次遍历完执行操作

此图为倒序第一次遍历完操作

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

typedef struct NODE
{
    int data;
    struct NODE *prior;
    struct NODE *next;
} Node, *List;

// 新建一个结点
List create_node(int);
// 释放一个结点
void free_node(List *);
// 创建一个双链表
List create_list(size_t);
// 计算双链表长度
size_t get_lenth(List);
// 打印双链表
void print_list(List);
// 删除一个双链表
void delete_list(List *);
// 选择位置插入
void choose_insert(List, int, int);
// 选择位置删除
void choose_delete(List, int);
// 双链表排序
void sort(List);
// 双链表倒序
void reverse(List);

int main()
{
    List list = NULL;
    printf("please enter the quality number of your list:\n");
    size_t qua = 0;
    scanf("%lu", &qua);

    /********************************************
     * 创建链表测试
     * *****************************************/
    list = create_list(qua);
    printf("========================================\n");
    printf("the original list is:\n");
    print_list(list);

    /*******************************************
     * 删除链表测试
     * *****************************************/
    // printf("the list after delete is:\n");
    // delete_list(&list);
    // print_list(list);

    /*******************************************
     * 选择位置插入测试
     * *****************************************/
    printf("the list after choose-insert is:\n");
    choose_insert(list, 2, 99);
    print_list(list);

    /*******************************************
     * 选择位置删除测试
     * *****************************************/
    printf("the list after choose-delete is:\n");
    choose_delete(list, 1);
    print_list(list);

    /*******************************************
     * 排序测试
     * *****************************************/
    printf("the list after sort is:\n");
    sort(list);
    print_list(list);

    /*******************************************
     * 倒序测试
     * *****************************************/
    printf("the list after reserve is:\n");
    reverse(list);
    print_list(list);

    return 0;
}

/*******************************************
 * 创建一个节点
 * *****************************************/
List create_node(int val)
{
    List head = (List)malloc(sizeof(Node));
    if (head == NULL)
    {
        perror("malloc");
        return NULL;
    }

    head->data = val;
    head->prior = NULL;
    head->next = NULL;

    return head;
}

/*******************************************
 * 释放一个节点
 * *****************************************/
void free_node(List *node)
{
    if (node == NULL)
    {
        printf("empty node\n");
        return;
    }

    free(*node);
    *node = NULL;
    node = NULL;
}

/*******************************************
 * 创建一个双链表
 * *****************************************/
List create_list(size_t size)
{
    List head = create_node(0);
    List list = head;
    int val;

    printf("please enter the nember:\n");
    while (size--)
    {
        scanf("%d", &val);
        List node = create_node(val);
        list->next = node;
        node->prior = list;
        list = list->next;
    }
    list->next = NULL;
    return head;
}

/*******************************************
 * 计算链表长度
 * *****************************************/
size_t get_lenth(List list)
{
    if (list == NULL)
    {
        printf("list is NULL!\n");
        return 0;
    }
    size_t count = 0;
    while (list->next != NULL)
    {
        count++;
        list = list->next;
    }

    return count;
}

/*******************************************
 * 打印双链表
 * *****************************************/
void print_list(List list)
{
    if (list == NULL)
    {
        printf("list is NULL!\n");
        printf("========================================\n");
        return;
    }

    int lenth = (int)get_lenth(list);
    list = list->next;
    while (lenth--)
    {
        printf("%d ", list->data);
        list = list->next;
    }
    printf("\n");
    printf("========================================\n");
    return;
}

/*******************************************
 * 删除双链表
 * *****************************************/
void delete_list(List *list)
{
    List tmp = *list;
    while (tmp->next != NULL)
    {
        tmp = (*list)->next;
        free_node(list);
        (*list) = tmp;
    }
    free_node(&tmp);
    tmp = NULL;
    *list = NULL;
    list = NULL;
    return;
}

/********************************************
 * 选择位置插入
 * *****************************************/
void choose_insert(List list, int pos, int val)
{
    if (list == NULL)
    {
        printf("list is NULL!\n");
        return;
    }

    int lenth = (int)get_lenth(list);
    List new_node = create_node(val);

    if (pos < 1 || pos > lenth)
        printf("wrong position to insert ! list'll not change !\n");
    else
    {
        pos -= 1;
        while (pos--)
            list = list->next;

        List tmp = list->next;

        list->next = new_node;
        new_node->prior = list;

        new_node->next = tmp;
        tmp->prior = new_node;
    }
    return;
}

/*******************************************
 * 选择位置删除
 * *****************************************/
void choose_delete(List list, int pos)
{
    if (list == NULL)
    {
        printf("list is NULL!\n");
        return;
    }

    int lenth = (int)get_lenth(list);
    if (pos < 1 || pos > lenth)
        printf("wrong position to delete ! list'll not change !\n");
    else
    {
        pos -= 1;
        while (pos--)
            list = list->next;

        List delete_node = list->next;
        list->next = delete_node->next;
        delete_node->next->prior = list;

        free_node(&delete_node);
    }
    return;
}

/*******************************************
 * 双链表排序
 * *****************************************/
void sort(List list)
{
    if (list == NULL)
    {
        printf("list is NULL!\n");
        return;
    }

    int lenth = (int)get_lenth(list);
    List head = list;
    List find = list;
    list = list->next;

    for (int i = 0; i < lenth; i++)
    {
        find = list;
        while (list->next != NULL)
        {
            if (list->next != NULL && find->data < list->next->data)
                find = list->next;
            list = list->next;
        }

        List tmp = NULL;
        List prior_node = NULL;

        if (find != head->next)
        {
            tmp = head->next;
            head->next = find;

            prior_node = find->prior;

            if (find->next != NULL)
                find->next->prior = prior_node;
            find->prior = head;

            prior_node->next = find->next;
            tmp->prior = find;

            find->next = tmp;
        }
        head = head->next;

        if (head)
            list = head->next;
    }
}

/*******************************************
 * 双链表倒序
 * *****************************************/
void reverse(List list)
{
    if (list == NULL)
    {
        printf("list is NULL!\n");
        return;
    }

    int lenth = (int)get_lenth(list);
    List head = list;

    for (int i = 0; i < lenth - 1; i++)
    {
        while (list->next)
            list = list->next;

        List list_prior = list->prior;

        list_prior->next = list->next;
        list->next = head->next;
        list->prior = head;
        head->next = list;

        head = head->next;
    }

    return;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值