04双循环链表

双循环链表

在这里插入图片描述

头节点的pre指向尾,尾节点的next指向pre

初始化链表

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef struct Node {
    int data;
    struct Node* pre;
    struct Node* next;
} Node;

Node* initList() {
    Node* L = (Node*)malloc(sizeof(Node));
    L -> data = 0;
    L -> pre = L;
    L -> next = L;
    return L;
}

int main() 
{
    Node* L = initList();

    return 0;
}

增加节点

头插法

void headInsert(Node* L, data) {
    Node* p = (Node*)malloc(sizeof(Node));
    p -> data = data;
    p -> next = L -> next;
    p -> pre = L;
    L -> next -> pre = p;
    L -> next = p;
    L -> data++;
}

尾插法

void tailInsert(Node* L, int data) {
    Node* p = L;
    while(p -> next != L) {
        p = p -> next;
    }
    Node* n = (Node*)malloc(sizeof(Node));
    n -> data = data;
    p -> next = n;
    n -> pre = p;
    n -> next = L;
    L -> pre = n;
    L -> data++;
}

删除节点

int deleteList(Node* L, int data) {
    Node* p = L -> next;
    while(p != L) {
        if (p -> data == data) {
            if (p -> next != NULL) {
                p -> pre -> next = p -> next;
                p -> next -> pre = p -> pre;
            } else p -> pre -> next = NULL;
        L -> data--;
        free(p);
        return TRUE;
        }
        p = p -> next;
    }
    return FALSE;
}

遍历链表

void printList(Node* L) {
    Node* p = L -> next;
    while (p != L) {
        printf("%d -> ", p -> data);
        p = p -> next;
    }
    printf("NULL\n");
}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值