双向链表操作集合(头尾指针)

在这里插入代码片
```#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node* pre;//前驱
    struct Node* next;//后继
}Node;

Node* tail = NULL;

// 初始化函数
Node* initList()
{
    // 开辟空间,创建节点
    Node* head = (Node*)malloc(sizeof(Node));
    // 容错判断
    if (head == NULL)
    {
        perror("init malloc error\n");
    }
    head->data = 0;
    head->next = NULL;
    head->pre = NULL;
    return head;
}


// 头插法
void headInsert(Node* list, int data)
{
    // 开辟空间,创建节点
    Node* node = (Node*)malloc(sizeof(Node));
    // 容错判断
    if (node == NULL)
    {
        perror("headinsert malloc error\n");
        return NULL;
    }
    
    // 接收数据
    node->data = data;
    node->next = list->next;
    node->pre = list;//优美的代码:node-next,  node-pre, list-next-pre, list-next,
    if (list->next != NULL)
    {
        list->next->pre = node;//只能这么写了,如果将list->next = node提取出来,那么,list指向已经改变,这行代码将无效
        list->next = node;
    }
    else
    {
        list->next = node;
    }
    
    list->data++;
}

// 任意位置插入
void insert(Node* list, int position, int data)
{
    // 对位置合法性进行判断
    if (position <= 0 || position > list->data + 1)
    {
        printf("位置超范围!\n");
        return;
    }
    // 尾指针的复制品
    Node* tail_cp = tail;
    // 头指针的复制品
    Node* head_cp = list;
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    // 对具体位置进行判断
    // 如果是第一个位置或者最后一个直接插入
    // 开辟空间,创建节点  
    if (list->next == NULL || position == list->data + 1)
    {
        node->next = tail->next;
        node->pre = tail;
        tail->next = node;
        tail = tail->next;
        list->data++;
    }
    else if (position <= (list->data / 2))//在前半段时,移动前面的指针
    {
        // 头指针移动到插入位置前一个
        for (int i = 0; i < position; i++)
        {
            head_cp = head_cp->next;
        }
        // 插入操作
        node->next = head_cp;
        node->pre = head_cp->pre;
        head_cp->pre->next = node;
        head_cp->pre = node;
        list->data++;
    }
    else//在后半段时,移动尾指针
    {
        // 移动次数:list->data - position
        for (int i = 0; i < (list->data - position); i++)
        {
            tail_cp = tail_cp->pre;
        }
        // 插入操作
        node->next = tail_cp;
        node->pre = tail_cp->pre;
        tail_cp->pre->next = node;
        tail_cp->pre = node;
        list->data++;
    }
}

// 尾插法
void tailInsert(Node* list, int data)
{
    // 开辟空间,创建节点
    Node* node = (Node*)malloc(sizeof(Node));
    // 接收数据
    node->data = data;
    // 找到尾节点
    Node* move_ptr = list->next;
    while (move_ptr->next != NULL)
    {
        move_ptr = move_ptr->next;
    }

    node->next = move_ptr->next;
    move_ptr->next = node;
    node->pre = move_ptr;
    list->data++;
}

// 删除指定数据操作
void delete(Node* list, int data)
{
    Node* pre = list;
    Node* current = list->next;
    while (current)
    {
        if (current->data == data)
        {
            pre->next = current->next;//调整前一个的后继
            current->next->pre = pre;//调整后一个的前驱,使他们跨过current
            free(current);
            current = NULL;
            list->data--;
            printf("已删除!\n");
            return;
        }
        pre = current;
        current = current->next;
    }
}

// 删除指定位置的数据
void deleteAny(Node* list, int post)
{
    Node* pre = list;
    Node* current = list->next;
    if (post < 0 || post > list->data)
    {
        printf("位置超范围!\n");
        return;
    }
    // 移动到指定位置
    for (int i = 1; i < post; i++)
    {
        pre = current;
        current = current->next;
    }
    // 判断是否是最后位置
    if (current->next == NULL)
    {
        pre->next = current->next;
    }
    else
    {
        pre->next = current->next;
        current->next->pre = pre;
    }

    free(current);
    current = NULL;
    list->data--;
}

// 修改指定位置
void modifyList(Node* list, int post, int data)
{
    Node* current = list->next;
    if (post < 0 || post > list->data)
    {
        printf("位置超范围!\n");
        return;
    }
    // 移动到指定位置
    for (int i = 1; i < post; i++)
    {
        current = current->next;
    }
    // 修改数据
    current->data = data;
}

// 打印
void printList(Node* list)
{
    Node* move_ptr = list->next;
    while (move_ptr)
    {
        printf("%d->", move_ptr->data);
        move_ptr = move_ptr->next;
    }
    printf("NULL\n");
}

int main(int argc, char const *argv[])
{
    // 初始化
    Node* list = initList();
    tail = list;
    // headInsert(list, 1);
    // headInsert(list, 2);
    // headInsert(list, 3);
    // headInsert(list, 4);
    // headInsert(list, 5);
    // printList(list);

    // tailInsert(list, 6);
    // tailInsert(list, 7);
    // tailInsert(list, 8);
    // printList(list);

    // deleteAny(list, 4);
    // deleteAny(list, 7);
    // printList(list);

    // insert(list, 4, 9);
    // printList(list);

    // modifyList(list, 4, 1);
    // printList(list);

    insert(list, 1, 1);
    insert(list, 2, 2);
    insert(list, 2, 3);
    printf("%d\n", list->data);
    insert(list, 4, 3);
    printList(list);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值