C语言实现链表

来源:互联网 发布:c语言中数据类型长度 编辑:程序博客网 时间:2024/05/16 09:34

用C语言实现了链表。基本操作有:

创建:
    前插法
    后插法
查找:
    按地址查找
    按数值查找
插入:
    按地址插入
    按数值插入
删除:
    按地址删除
    按数值删除

输出

以下是程序内容:


/************************************************************ * List.c                                                   * * To create a simple list and some functions.              * * by Eric Brown.                                           * ************************************************************/#include <stdio.h>#include <stdlib.h>#include <stdbool.h>        /*C99 only*/typedef struct List{    int data;    struct List *next;}node;/*create_prev_insert: to create a list by inserting node before current node.*/node * create_prev_insert(void);/*print_list: to print a list.*/void print_list(node *list);/*create_next_insert: to create a list by inserting node next current node.*/node * create_next_insert(void);/*find_by_add: to find a node by its address.*/node * find_by_add(node *list, int add);/*find_by_data: to find a node by its data.*/node * find_by_data(node *list, int data);/*insert_by_add: to insert a node by address.*/bool insert_by_add(node **list, int data, int add);/*insert_by_data: to insert a node by data.*/bool insert_by_data(node **list, int node_data, int list_data);/*delete_by_add: to delete a node by address.*/bool delete_by_add(node **list, int add);/*delete_by_data: to delete a node by data.*/bool delete_by_data(node **list, int data);int main(void){    int data, op, add, list_data;    node *list, *temp;    list = NULL;        for (;;)    {        printf("--------------------------Menu--------------------------\n");        printf("| 1.Create a list with create_prev_insert function.    |\n");        printf("| 2.Create a list with create_next_insert function.    |\n");        printf("| 3.Find a member by its address.                      |\n");        printf("| 4.Find a member by its data.                         |\n");        printf("| 5.Insert a member by address.                        |\n");        printf("| 6.Insert a member by data.                           |\n");        printf("| 7.Delete a member by its address.                    |\n");        printf("| 8.Delete a member by its data.                       |\n");        printf("| 9.Print the list.                                    |\n");        printf("| 0.Exit.                                              |\n");        printf("--------------------------------------------------------\n");        printf("| Your selection:");        scanf("%d", &op);        fflush(stdin);                switch (op)        {            case 1:                list = create_prev_insert();                break;            case 2:                list = create_next_insert();                break;            case 3:                printf("| Please input address:");                scanf("%d", &add);                temp = find_by_add(list, add);                if (temp == NULL)                    printf("| Can't find this node!\n");                else                    printf("| The node is %d\n", temp->data);                printf("--------------------------------------------------------\n\n");                break;            case 4:                printf("| Please input the data:");                scanf("%d", &data);                temp = find_by_data(list, data);                if (temp == NULL)                    printf("| Can't find this node!\n");                else                    printf("| The node is %d\n", temp->data);                printf("--------------------------------------------------------\n\n");                break;            case 5:                printf("| Please input the data:");                scanf("%d", &data);                printf("| Please input the address:");                scanf("%d", &add);                insert_by_add(&list, data, add);                break;            case 6:                printf("| Please input the node_data:");                scanf("%d", &data);                printf("| Please input the list_data:");                scanf("%d", &list_data);                insert_by_data(&list, data, list_data);                break;            case 7:                printf("| Please input the address:");                scanf("%d", &add);                delete_by_add(&list, add);                break;            case 8:                printf("| Please input the data:");                scanf("%d", &data);                delete_by_data(&list, data);                break;            case 9:                print_list(list);                break;            case 0:                exit(EXIT_SUCCESS);            default :                printf("--------------------------------------------------------\n");                printf("| No this operation!                                   |\n");                printf("--------------------------------------------------------\n\n");        }    }}node * create_prev_insert(void){    node *head, *new_node;    head = NULL;    int n, data;        printf("| Please input the number of nodes you \n");    printf("| want to create:");    scanf("%d", &n);    if (n <= 0)    {        printf("Can't create a list with length less than 0!\n\n");        exit(EXIT_FAILURE);    }        while (n--)    {        new_node = (node *)malloc(sizeof(node));        if (new_node == NULL)        {            printf("Can't create the list!\n\n");            exit(EXIT_FAILURE);        }        scanf("%d", &data);        new_node->data = data;        new_node->next = head;        head = new_node;    }        return head;}void print_list(node *list){    int i = 0;        if (list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return ;    }    printf("--------------------------------------------------------\n");    printf("| ");    for (; list != NULL; list = list->next)    {        printf("%d\t", list->data);        if (++i % 5 == 0)            printf("\n| ");    }    printf("\n");    printf("--------------------------------------------------------\n\n");    }node * create_next_insert(void){    node *head, *cur, *next;    int n, data;        printf("| Please input the number of nodes you \n");    printf("| want to create:");    scanf("%d", &n);    if (n <= 0)    {        printf("Can't create a list with length less than 0!\n\n");        exit(EXIT_FAILURE);    }    head = (node *)malloc(sizeof(node));    if (head == NULL)    {        printf("Can't create the list!\n\n");        exit(EXIT_FAILURE);    }    cur = head;    scanf("%d", &data);    cur->data = data;    while (--n)    {        next = (node *)malloc(sizeof(node));        if (next == NULL)        {            printf("Can't create a list with length less than 0!\n\n");            exit(EXIT_FAILURE);        }        scanf("%d", &data);        next->data = data;        cur->next = next;        cur = next;    }    cur->next = NULL;        return head;}node * find_by_add(node *list, int add){    if (list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return 0;    }    if (add <= 0)    {        printf("Error address!\n\n");        exit(EXIT_FAILURE);    }    for (; --add > 0 && list != NULL; list = list->next)        ;            return list;}node * find_by_data(node *list, int data){    if (list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return 0;    }    for (;list != NULL && list->data != data; list = list->next)        ;        return list;}bool insert_by_add(node **list, int data, int add){    node *new_node, *cur;        if (*list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    if (add <= 0)    {        printf("| Error address!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    else if (add == 1)    {        cur = *list;    }    else        cur = find_by_add(*list, add - 1);    if (cur == NULL)    {        printf("Can't find this node!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    new_node = (node *)malloc(sizeof(node));    if (new_node == NULL)    {        printf("| Can't create the list!\n\n");        exit(EXIT_FAILURE);    }    new_node->data = data;    if (cur == *list)    {        new_node->next = *list;        *list = new_node;    }    else    {        new_node->next = cur->next;        cur->next = new_node;    }        return true;}bool insert_by_data(node **list, int node_data, int list_data){    node *new_node, *cur, *prev;        if (*list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    for (prev = NULL, cur = *list;         cur != NULL && cur->data != list_data;         prev = cur, cur = cur->next)        ;    if (cur == NULL)    {        printf("| Can't find the node!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    new_node = (node *)malloc(sizeof(node));    if (new_node == NULL)    {        printf("Can't create this list!\n\n");        exit(EXIT_FAILURE);    }    new_node->data = node_data;    if (prev == NULL)    {        new_node->next = *list;        *list = new_node;    }    else    {        new_node->next = cur;        prev->next = new_node;    }        return true;}bool delete_by_add(node **list, int add){    node *cur, *prev;        if (*list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    if (add <= 0)    {        printf("| Error address!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    else if (add == 1)    {        cur = *list;        *list = (*list)->next;        free(cur);    }    else    {        prev = find_by_add(*list, add - 1);        if (prev == NULL)        {            printf("| Can't find the node!\n");            printf("--------------------------------------------------------\n\n");            return false;        }        cur = prev->next;        prev->next = cur->next;        free(cur);    }        return true;}    bool delete_by_data(node **list, int data){    node *cur, *prev;        if (*list == NULL)    {        printf("| The list hasn't been initialized!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    for (prev = NULL, cur = *list;         cur != NULL && cur->data != data;         prev = cur, cur = cur->next)        ;    if (cur == NULL)    {        printf("| Can't find the node!\n");        printf("--------------------------------------------------------\n\n");        return false;    }    if (prev == NULL)    {        *list = (*list)->next;        free(cur);    }    else     {        prev->next = cur->next;        free(cur);    }        return true;}