C语言单项链表

记录下来,以后用到了直接照抄。

代码

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


typedef struct node {
    struct node *next;
    int value;
} *SingleList;

int value_compare(struct node *a, int i)
{
    return (a->value - i);
}

struct node *sl_find(struct node **rootptr, int v, int (*compare)(struct node *, int))
{
    while(*rootptr)
    {
        if (0 == compare(*rootptr, v))
        {
            return *rootptr;
        }
        rootptr = &(*rootptr)->next;
    }

    return NULL;
}

int sl_remove(struct node **rootptr, struct node *which)
{
    while(*rootptr)
    {
        if (*rootptr == which)
        {
            struct node *t = *rootptr;
            *rootptr = t->next;
            return 1;
        }
        rootptr = &(*rootptr)->next;
    }

    return 0;
}

int sl_append(struct node **rootptr, struct node *n)
{
    while(*rootptr)
        rootptr = &(*rootptr)->next;

    *rootptr = n;
    return 1;
}

struct node *sl_fetch(struct node **rootptr)
{
    struct node *t;

    t = *rootptr;
    if (NULL != t)
        *rootptr = t->next;
    else
        *rootptr = NULL;

    return t;
}

void single_list_test(void)
{
    const int TOTAL = 10;
    int i;
    struct node *root = NULL;
    struct node *temp;
    struct node **rootptr;
    
    printf("single list test start
");
    printf("---------------------
");
    for (i=0; i<TOTAL; i++)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->value = i;
        temp->next = NULL;
        printf("append node %d
", i);
        sl_append(&root, temp);
    }
    
    printf("---------------------
");
    if (NULL != (temp = sl_fetch(&root)))
    {
        printf("fetch node %d
", temp->value);
        free(temp);
    }
    if (NULL != (temp = sl_fetch(&root)))
    {
        printf("fetch node %d
", temp->value);
        free(temp);
    }
    if (NULL != (temp = sl_find(&root, 2, value_compare)))
    {
        printf("delete node %d
", temp->value);
        sl_remove(&root,temp);
        free(temp);
    }
    if (NULL != (temp = sl_find(&root, 12, value_compare)))
    {
        printf("delete node %d
", temp->value);
        sl_remove(&root,temp);
        free(temp);
    }
    if (NULL != (temp = sl_find(&root, 8, value_compare)))
    {
        printf("delete node %d
", temp->value);
        sl_remove(&root,temp);
        free(temp);
    }
    if (NULL != (temp = sl_find(&root, 5, value_compare)))
    {
        printf("delete node %d
", temp->value);
        sl_remove(&root,temp);
        free(temp);
    }

    printf("---------------------
");
    rootptr = &root;
    while(*rootptr)
    {
        temp = *rootptr;
        *rootptr = temp->next;
        printf("free %d
", temp->value);
        free(temp);
    }

    printf("---------------------
");
    printf("single list test finish

");
}

int main(void)
{
    single_list_test();
    printf("press ENTER key to exit!
");
    getchar();
    return 0;
}

截图

如果转载,请注明出处。https://www.cnblogs.com/ssdq/
原文地址:https://www.cnblogs.com/ssdq/p/13186719.html