单链表无头

#include <stdio.h>

typedef int elem_t;

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

static void list_add(struct node_info **head,
                struct node_info *node)
{
        if (*head) {
                node->next = *head;
                *head = node;
        } else {
                node->next = NULL;
                *head = node;
        }
}

static void list_add_tail(struct node_info **head,
                struct node_info *node)
{
        struct node_info *cur = NULL;

        if (*head) {
                for (cur = *head; cur->next; cur = cur->next) {}
                node->next = NULL;
                cur->next = node;
        } else {
                node->next = NULL;
                *head = node;
        }
}

static void list_for_each(struct node_info *head,
                void (*todo)(struct node_info *))
{
        struct node_info *cur = head;

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

static void list_del(struct node_info **head,
                struct node_info *node)
{
        //node涓虹.涓€涓..?
        if (*head == node) {
                *head = node->next;
                node->next = NULL;
                return;
        }

        struct node_info *cur = NULL;

        for (cur = *head;
                cur && (cur->next != node);
                cur = cur->next) {
        }

        if (cur) {
                cur->next = node->next;
                node->next = NULL;
        }
}

static void list_del_fake(struct node_info **head,
                struct node_info *node)
{
        struct node_info *cur = NULL;

        for (cur = *head; cur; cur = cur->next) {
                if (cur == node) {
                        cur = node->next;
                        *node = *(node->next);
                        cur->next = NULL;
                        break;
                }
        }
}

static int list_isloop(struct node_info *head,
                struct node_info **entry)
{
        struct node_info *slow = head;
        struct node_info *fast = head;

        while (fast) {
                slow = slow->next;
                if (fast->next) {
                        fast = fast->next->next;
                } else {
                        fast = fast->next;
                }

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

#define LIST_LEN 10

//?..? if (slow == fast) { for (slow = head; slow != fast; slow = slow->next, fast = fast->next) {} *entry = slow; return 1; } } return 0;}


int main()
{
        struct node_info *head = NULL;

        struct node_info tab[LIST_LEN] = {0};

        size_t i = 0;
        for (i = 0; i < LIST_LEN; i++) {
                tab[i].data = i;
                list_add_tail(&head, tab + i);
        }

        list_del(&head, (struct node_info *)0x1000);

        list_del_fake(&head, tab + 0);
        list_del_fake(&head, tab + 0);

        list_for_each(head, print);
        printf("\n");

        //?堕€..?
        tab[9].next = &tab[6];

        struct node_info *entry = NULL;
        if (list_isloop(head, &entry)) {
                printf("loop entry is %d\n", entry->data);
        } else {
                printf("no loop\n");
        }

        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值