苏嵌实训-嵌入式linuxC第 8 天

项目名称 苏嵌实训-嵌入式linuxC第 8 天
今日进度以及任务 今日进度学习的是链表,包括列表的创建,增删改查
本日任务完成情况 通过老师的代码以及练习对链表更加了解熟悉
本日开发中出现的问题汇总
本日未解决的问题
本日开发收获 学会了链表,以及链表的增删改查
其他
//含表头
#include 
#include 
//1\定义节点类型:只要确定数据域保存哪些数据,链接域是固定的!<节点的指针>
struct node
{
    int num; //数据域

    struct node *next; //链接域:保存下一个节点的地址
};

typedef struct node Node;
typedef Node *Link;

//Link head; 尽量不要使用全局变量,不安全;
void is_malloc(Link p)
{
    if (NULL == p)
    {
        printf("malloc error!\n");
        exit(1);
    }
}

void print_error(int ret)
{

    switch (ret)
    {
    case 0:
    {
        printf("handle success!\n");
        break;
    }
    case -1:
    {
        printf("link is null!\n");
        break;
    }
    case -2:
    {
        printf("node is not find!\n");
        break;
    }
    default:
    {
        printf("not error!\n");
        exit(1);
    }
    }
}

void create_link(Link *head)
{
    *head = (Link)malloc(sizeof(Node));
    is_malloc(*head);
    (*head)->next = NULL;
}

void insert_head_node(Link newnode, Link *head)
{
    newnode->next = (*head)->next;
    (*head)->next = newnode;
}

void insert_tail_node(Link newnode, Link *head)
{
    Link temp = *head;
    //对空表做处理
    while (temp->next != NULL)
    {
        temp = temp->next;
    }

    temp->next = newnode;
    newnode->next = NULL;
}

int insert_mid_node(int num, Link newnode, Link *head)
{
    Link temp = (*head)->next;

    while (temp != NULL)
    {
        if (temp->num == num)
        {
            newnode->next = temp->next;
            temp->next = newnode;
            return 0;
        }
        temp = temp->next;
    }

    return -2;
}

int delete_node(int num, Link *head)
{
    Link temp = *head;
    //形成前后指针
    Link p = temp;
    temp = temp->next;

    while (temp != NULL)
    {
        if (temp->num == num)
        {
            p->next = temp->next;
            free(temp);
            temp = NULL;
            return 0;
        }

        p = temp;
        temp = temp->next;
    }

    return -2;
}

void display_link(Link head)
{
    Link temp = head->next;

    while (temp != NULL)
    {
        printf("%d\n", temp->num);
        temp = temp->next;
    }
}

int main(int argc, char *argv[])
{
    //2、定义头指针
    Link head;
    Link newnode;

    create_link(&head);

    if (NULL == head)
    {
        printf("init link success!\n");
    }
    else
    {
        printf("init link failed!\n");
    }
    for (int i = 0; i < 10; i++)
    {
        newnode = (Link)malloc(sizeof(Node));
        if (NULL == newnode)
        {
            printf("malloc error!\n");
            exit(1);
        }
        newnode->num = i + 1;
        insert_head_node(newnode, &head);
        //insert_tail_node(newnode, &head);
    }

    newnode = (Link)malloc(sizeof(Node));
    newnode->num = 15;

    int ret = insert_mid_node(5, newnode, &head);

    print_error(ret);

    ret = delete_node(3, &head);

    print_error(ret);

    display_link(head);

    return 0;
}
//不含表头
#include 
#include 
//1\定义节点类型:只要确定数据域保存哪些数据,链接域是固定的!<节点的指针>
struct node
{
    int num; //数据域

    struct node *next; //链接域:保存下一个节点的地址
};

typedef struct node Node;
typedef Node *Link;

//Link head; 尽量不要使用全局变量,不安全;

void print_error(int ret)
{

    switch (ret)
    {
    case 0:
    {
        printf("handle success!\n");
        break;
    }
    case -1:
    {
        printf("link is null!\n");
        break;
    }
    case -2:
    {
        printf("node is not find!\n");
        break;
    }
    default:
    {
        printf("not error!\n");
        exit(1);
    }
    }
}

void create_link(Link *head)
{
    *head = NULL;
}

void insert_head_node(Link newnode, Link *head)
{
    newnode->next = *head;
    *head = newnode;
}

void insert_tail_node(Link newnode, Link *head)
{
    Link temp = *head;
    //对空表做处理
    if (*head == NULL)
    {
        newnode->next = NULL;
        *head = newnode;
    }
    else
    {
        while (temp->next != NULL)
        {
            temp = temp->next;
        }

        temp->next = newnode;
        newnode->next = NULL;
    }
}

int insert_mid_node(int num, Link newnode, Link *head)
{
    Link temp = *head;

    if (*head == NULL)
    {
        return -1;
    }

    while (temp != NULL)
    {
        if (temp->num == num)
        {
            newnode->next = temp->next;
            temp->next = newnode;
            return 0;
        }
        temp = temp->next;
    }

    return -2;
}

int delete_node(int num, Link *head)
{
    if (*head == NULL)
    {
        return -1;
    }

    Link temp = *head;

    if (temp->num == num) //对第一个节点做判断
    {
        *head = (*head)->next;
        free(temp);
        temp = NULL;
        return 0;
    }

    //形成前后指针
    Link p = temp;
    temp = temp->next;

    while (temp != NULL)
    {
        if (temp->num == num)
        {
            p->next = temp->next;
            free(temp);
            temp = NULL;
            return 0;
        }

        p = temp;
        temp = temp->next;
    }

    return -2;
}

void display_link(Link head)
{
    Link temp = head;

    while (temp != NULL)
    {
        printf("%d\n", temp->num);
        temp = temp->next;
    }
}

int main(int argc, char *argv[])
{
    //2、定义头指针
    Link head;
    Link newnode;

    create_link(&head);

    if (NULL == head)
    {
        printf("init link success!\n");
    }
    else
    {
        printf("init link failed!\n");
    }
    for (int i = 0; i < 10; i++)
    {
        newnode = (Link)malloc(sizeof(Node));
        if (NULL == newnode)
        {
            printf("malloc error!\n");
            exit(1);
        }
        newnode->num = i + 1;
        //insert_head_node(newnode,&head);
        insert_tail_node(newnode, &head);
    }

    newnode = (Link)malloc(sizeof(Node));
    newnode->num = 15;

    int ret = insert_mid_node(5, newnode, &head);

    print_error(ret);

    ret = delete_node(3, &head);

    print_error(ret);

    display_link(head);

    return 0;
}

你可能感兴趣的:(苏嵌实训-嵌入式linuxC第 8 天)