02dlist-base

list.h
typedef int elem_t;

struct node_info {
    elem_t data;
    struct node_info *prev;
    struct node_info *next;
};

struct list_info {
    struct node_info *head;
    void (*add)(struct list_info *,
            struct node_info *); 
    void (*add_tail)(struct list_info *,
            struct node_info *); 
    void (*del)(struct list_info *,
            struct node_info *); 
    void (*for_each)(struct list_info *,
            void (*)(struct node_info*));
};

#define list_for_each(cur, head) \
    for (cur = (head)->next; \
        cur != (head); \
        cur = (cur)->next)

void list_init(struct list_info *); 
void list_destroy(struct list_info *); 


list.c

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


/* 灏.ode?..?颁袱涓..?昏.??rev
 * prev?.ext蹇.』?搁.锛..?.??..
 */
static void __list_add(struct node_info *node,
        struct node_info *prev,
        struct node_info *next)
{
    node->prev = prev;
    node->next = next;
    prev->next = node;
    next->prev = node;
}

static void list_add(struct list_info *info, 
        struct node_info *node)
{
    __list_add(node, info->head, info->head->next);
}

static void list_add_tail(struct list_info *info, 
        struct node_info *node)
{
    __list_add(node, info->head->prev, info->head);
}


 

static void list_del(struct list_info *info,
        struct node_info *node)
{
    node->prev->next = node->next;
    node->next->prev = node->prev;
    node->prev = node;
    node->next = node;
}

static void for_each(struct list_info *info,
        void (*todo)(struct node_info *)) 
{
    struct node_info *cur = info->head->next;

    for (; cur != info->head; cur = cur->next) {
        todo(cur);
    }   
}


 

void list_init(struct list_info *info)
{
    info->head = (struct node_info *)malloc(sizeof(struct node_info));
    info->head->data = 0;
    info->head->prev = info->head;
    info->head->next = info->head;

    info->add = list_add;
    info->add_tail = list_add_tail;
    info->del = list_del;
    info->for_each = for_each;
}

void list_destroy(struct list_info *info)
{
    struct node_info *cur = info->head->next;

    for (; cur != info->head; cur = info->head->next) {
        list_del(info, cur);
    }

    free(info->head);
}

test.c

 

#include <stdio.h>
#include "list.h"

#define LIST_LEN 10

static void print(struct node_info *node)
{
    printf("%d ", node->data);
}

int main()
{
    struct list_info list;
    list_init(&list);

    struct node_info s[LIST_LEN] = {0};

    size_t i = 0;
    for (i = 0; i < LIST_LEN; i++) {
        s[i].data = i;
        list.add_tail(&list, s + i);
    }  

    list.del(&list, s + 0);
    list.del(&list, s + 1);

/*        list.for_each(&list, print);*/
    struct node_info *cur = NULL;
    list_for_each(cur, list.head) {
        printf("%d ", cur->data);
    }  
    printf("\n");

    list_destroy(&list);

    return 0;
}


 

在Dev-C++中,双链表通常由`DLinkList`结构体表示,其中包含指向前一个节点和后一个节点的指针。`DList_TailInsert`函数用于在链表的尾部插入新节点,而`DListDelete`则负责删除指定位置的节点并返回删除的元素。 首先,我们假设`DLinkList`的基本结构类似这样: ```cpp struct Node { int data; Node* prev; Node* next; }; typedef struct Node DLinkNode; typedef DLinkNode* DLinkList; ``` `DList_TailInsert`函数实现: ```cpp void DList_TailInsert(DLinkList& L, int value) { // 如果链表为空,创建一个新的节点作为头结点 if (L == nullptr) { L = new DLinkNode{value, nullptr, nullptr}; } else { // 找到最后一个节点 DLinkNode* tail = L; while (tail->next != nullptr) tail = tail->next; // 创建新节点,并将其添加到尾部 tail->next = new DLinkNode{value, tail, nullptr}; tail->next->prev = tail; } } ``` `DListDelete`函数实现: ```cpp int DListDelete(DLinkList& L, int i, int& e) { if (i < 0 || L == nullptr) return -1; // 检查索引是否有效 int count = 0; DLinkNode* current = L; // 寻找目标节点 while (count < i && current != nullptr) { current = current->next; ++count; } if (current == nullptr) return -1; // 越界了 // 保存待删除元素的数据 e = current->data; // 删除节点 if (current->prev != nullptr) current->prev->next = current->next; else L = current->next; // 如果删除的是头节点,则更新链表头 if (current->next != nullptr) current->next->prev = current->prev; delete current; // 释放内存 return e; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值