单链表的表示和实现

表示

typedef struct node_t {
    data_t data;
    struct node_t *next = NULL;
} linknode_t, *linklist_t;

实现

//创建
linklist_t CreateEmptyLinklist()
{
    linklist_t list;
    list = (linklist_t)malloc(sizeof(linknode_t));
    if (NULL != list) {
        list->next = NULL;
        return list;
    } else {
        return NULL;
    }
}
//清空
int ClearLinklist(linklist_t list)
{
    linknode_t *node;
    if (NULL != list) {
        while (NULL != list->next) {
            node = list->next;
            list->next = node->next;
            free(node);
        }
        return 0;
    } else {
        return -1;
    }
}
//销毁
int DestroyLinklist(linklist_t list)
{
    if (NULL != list) {
        ClearLinklist(list);
        free(list);
        return 0;
    } else {
        return -1;
    }
}
//是否为空
int EmptyLinklist(linklist_t list)
{
    if (NULL != list) {
        if (NULL != list->next)
            return 1;
        else
            return 0;
    } else {
        return -1;
    }
}
//当前长度
int LengthLinklist(linklist_t list)
{
    int i = 0;
    linknode_t *node;
    if (NULL != list) {
        node = list->next;
        while (NULL != node) {
            node = node->next;
            i++;
        }
        return i;
    } else {
        return -1;
    }
}
//
int GetLinklist(linklist_t list, int at, data_t *x)
{
    int i = 0;
    linknode_t *node;
    if (NULL != list) {
        if (at < 0) return -1;
        node = list->next;
        while (NULL != node) {
            if (at == i) {
                if (x != NULL)
                    *x = node->data;
                return 0;
            }
            node = node->next;
            i++;
        }
    } else {
        return -1;
    }
}
//
int SetLinklist(linklist_t list, int at, data_t x)
{
    int i = 0;
    linknode_t *node;
    int found = 0;
    if (NULL != list) {
        if (at < 0) return -1;
        node = list->next;
        while (NULL != node) {
            if (at == i) {
                node->data = x;
                found = 1;
                break;
            }
            node = node->next;
            i++;
        }
    } else {
        return -1;
    }
    if (found == 1)
        return 0;
    else
        return -1;
}
//
int InsertLinklist(linklist_t list, int at, data_t x)
{
    int i = 0;
    linknode_t *node_at, *node_prev, *node_new;
    int found = 0;
    if (at < 0) return -1;
    if (NULL != list) {
        node_new = (linknode_t *)malloc(sizeof(linknode_t));
        if (NULL == node_new) {
            return -1;
        }
        node_new->data = x;
        node_new->next = NULL;
        
        node_prev = list;
        node_at = list->next;
        while (NULL != node_at) {
            if (at ==i) {
                found = 1;
                break;
            }
            node_prev = node_at;
            node_at = node_at->next;
            i++;
        }
    } else {
        return -1;
    }
    if (found) {
        node_new->next = node_prev->next;
        node_prev->next = node_new;
    } else {
        node_prev->next = node_new;
    }
    return 0;
}
//
int DeleteLinklist(linklist_t list, int at)
{
    linknode_t *node_prev, *node_at;
    int pos = 0;
    int found = 0;
    if (NULL != list) {
        node_prev = list;
        node_at = list->next;
        if (at < 0) return -1;
        while (NULL != node_at) {
            if (pos == at) {
                found = 1;
                break;
            }
            node_prev = node_at;
            node_at = node_at->next;
            pos++;
        }
    } else {
        return -1;
    }
    if (found == 1) {
        node_prev->next = node_at->next;
        free(node_at);
        return 0;
    } else {
        return -1;
    }
}
//翻转:插入
linklist_t ReverseLinklist(linklist_t list)
{
    if (NULL == list) return NULL;
    
    linknode_t *node_at, *node;
    
    node_at = list->next;
    list->next = NULL;
    while (NULL != node_at) {
        node = node_at;
        node_at = node_at->next;
        node->next = list->next;
        list->next = node;
    }
    return list;
}
//翻转:掉头
linklist_t ReverseLinklist2(linklist_t list)
{
    if (NULL != list) return NULL;
        
    linknode_t *node_prev, *node_at, *node_next;
    
    node_prev = NULL;
    node_at = list->next;
    
    while (NULL != node_at) {
        node_next = node_at->next;
        
        if (NULL == node_next) {
            list->next = node_at;
        }
        node_at->next = node_prev;
        node_prev = node_at;
        node_at = node_next;
    }
    
    
}

测试代码

void iterate_list(linklist_t list)
{
    linknode_t *node;
    
    if (!list) return;
        
    printf("list = {");
    
    /* start from the first element */
    node = list->next;
    while (NULL != node) {
        printf("%d->", node->data);
        
        /* move to the next */
        node = node->next;
    }
    
    if (LengthLinklist(list) > 0)
        printf("} 
");
    else
        printf("}
");
}

int main(int argc, char *argv[])
{
    int i;
    data_t a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    data_t x;
    
    linklist_t list;
    
    list = CreateEmptyLinklist();
    
    if (NULL == list) return -1;

    printf("insert method 1: insert each elment
");
    for (i = 0; i < 10; i++) {
        if (InsertLinklist(list, i, a[i]) < 0)
            break;
    }
    iterate_list(list);

    GetLinklist(list, 4, &x);
    printf("list[4] = %d
", x);
    
    printf("updated list[4] to 100
");
    SetLinklist(list, 4, 100);
    GetLinklist(list, 4, &x);
    printf("now list[4] = %d
", x);
    iterate_list(list);
        
    printf("removed list[4]
");
    DeleteLinklist(list, 4);
    GetLinklist(list, 4, &x);
    printf("now list[4] = %d
", x);
    printf("and total number of list is %d
", LengthLinklist(list));
    iterate_list(list);

    printf("insert "1" at the %dth position of the list
", 0);
    InsertLinklist(list, 0, 1);
    iterate_list(list);

    printf("reversed the list
");
    ReverseLinklist(list);
    iterate_list(list);
    
    ClearLinklist(list);
    printf("after clear, total number of list is %d and ", LengthLinklist(list));
    iterate_list(list);
        
    DestroyLinklist(list);
    
    return 0;
}

结果

insert method 1: insert each elment
list = {2->4->6->8->10->12->14->16->18->20} 
list[4] = 10
updated list[4] to 100
now list[4] = 100
list = {2->4->6->8->100->12->14->16->18->20} 
removed list[4]
now list[4] = 12
and total number of list is 9
list = {2->4->6->8->12->14->16->18->20} 
insert "1" at the 0th position of the list
list = {1->2->4->6->8->12->14->16->18->20} 
reversed the list
list = {20->18->16->14->12->8->6->4->2->1} 
after clear, total number of list is 0 and list = {}
原文地址:https://www.cnblogs.com/vsyf/p/4912322.html