#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;
}