2024.01.24作业

1.单链表按任意元素查找

Linklist search_element(Linklist head, datatype element)
{
    if (NULL == head)
    {
        puts("按元素查找:链表为空");
        return head;
    }

    int i = 1;
    Linklist q = head;
    for (i = 1; q != NULL; i++)
    {
        if (q->data == element)
        {
            break;
        }

        q = q->next;
    }

    if (NULL == q)
    {
        printf("按元素查找:%d 不存在\n", element);
        return head;
    }

    printf("按元素查找:%d 在 %d 位\n", element, i);

    return head;
}

2.单链表按任意元素修改

Linklist change_element(Linklist head, datatype old_element, datatype element)
{
    if (NULL == head)
    {
        puts("按元素修改:链表为空");
        return head;
    }

    int i = 1;
    Linklist q = head;
    for (i = 1; q != NULL; i++)
    {
        if (q->data == old_element)
        {
            break;
        }

        q = q->next;
    }

    if (NULL == q)
    {
        printf("按元素修改:%d 不存在\n", element);
        return head;
    }

    q->data = element;
    printf("按元素修改:%d 位的 %d 修改为 %d\n", i, old_element, element);

    return head;
}

3.单链表按任意元素删除

Linklist delete_element(Linklist head, datatype element)
{
    if (NULL == head)
    {
        puts("按元素删除:链表为空");
        return head;
    }

    int i = 1;
    Linklist q = head;
    for (i = 1; q != NULL; i++)
    {
        if (q->data == element)
        {
            break;
        }

        q = q->next;
    }

    if (NULL == q)
    {
        printf("按元素删除:%d 不存在\n", element);
        return head;
    }

    if (head->data == element)
    {
        q = head;
        head = head->next;
        free(q);
        q = NULL;
        printf("按元素删除:删除在 %d 位的 %d\n", i, element);
        return head;
    }

    Linklist p = head;
    while (p->next != q)
    {
        p = p->next;
    }

    p->next = q->next;
    free(q);
    q = NULL;
    printf("按元素删除:删除在 %d 位的 %d\n", i, element);

    return head;
}

4.单链表排序

Linklist sort(Linklist head)
{
    if (NULL == head)
    {
        puts("排序:链表为空");
        return head;
    }

    if (NULL == head->next)
    {
        puts("排序:链表只有一个元素");
        return head;
    }

    Linklist p, q;
    p = head;
    while (p->next != NULL)
    {
        q = p->next;

        while (q != NULL)
        {
            if (p->data > q ->data)
            {
                datatype t = p->data;
                p->data = q->data;
                q->data = t;
            }

            q = q->next;
        }

        p = p->next;
    }
    puts("排序:完成");

    return head;
}

5.单链表释放内存

Linklist free_space(Linklist head)
{
    puts("清空链表");
    if (NULL == head)
    {
        return NULL;
    }

    Linklist p;
    while (head != NULL)
    {
        p = head;
        head = head->next;
        free(p);
        p = NULL;
    }

    head = NULL;

    return head;
}

源代码

myfunc.h

#ifndef __MYFUNC_H__
#define __MYFUNC_H__

#include 
#include 
#include 

enum num
{
    FALSE = -1,
    SUCCESS
};

typedef int datatype;
typedef struct Node
{
    datatype data;
    struct Node* next;
} *Linklist;

Linklist create();
Linklist insert_head(Linklist head, datatype element);
Linklist insert_rear(Linklist head, datatype element);
Linklist delete_head(Linklist head);
Linklist delete_rear(Linklist head);
Linklist insert_pos(Linklist head, datatype element, int pos);
Linklist delete_pos(Linklist head, int pos);

Linklist search_element(Linklist head, datatype element);
Linklist change_element(Linklist head, datatype old_element, datatype element);
Linklist delete_element(Linklist head, datatype element);
Linklist sort(Linklist head);
Linklist free_space(Linklist head);


void show(Linklist head);
int length(Linklist head);


#endif

myfunc.c

#include "myfunc.h"


Linklist create()
{
    Linklist s = (Linklist)malloc(sizeof(struct Node));
    
    if (NULL == s)
    {
        return NULL;
    }

    s->data = 0;
    s->next = NULL;

    return s;
}

Linklist insert_head(Linklist head, datatype element)
{
    Linklist s = create();
    if (NULL == s)
    {
        return NULL;
    }

    s->data = element;
    if (NULL == head)
    {
        head = s;
    }
    else
    {
        s->next = head;
        head = s;
    }
    printf("头插元素 %d\n", element);

    return head;
}

void show(Linklist head)
{
    if (NULL == head)
    {
        puts("展示:链表为空");
        return;
    }

    printf("展示:链表中元素:");
    Linklist p = head;
    while (p)
    {
        printf("%2d", p->data);
        p = p->next;
    }
    printf("\n");
}

Linklist insert_rear(Linklist head, datatype element)
{
    Linklist s = create();
    if (NULL == s)
    {
        return NULL;
    }
    s->data = element;

    if (NULL == head)
    {
        head = s;
    }
    else
    {
        Linklist p = head;
        while (p->next)
        {
            p = p->next;
        }
        p->next = s;
    }
    printf("尾插:元素 %d\n", s->data);

    return head;
}

Linklist delete_head(Linklist head)
{
    if (NULL == head)
    {
        puts("头删:链表为空");
        return NULL;
    }

    Linklist s = head;
    head = s->next;
    s->next = NULL;
    printf("头删:元素 %d\n", s->data);
    free(s);
    s = NULL;

    return head;
}

Linklist delete_rear(Linklist head)
{
    if (NULL == head)
    {
        puts("尾删:链表为空");
        return NULL;
    }

    if (NULL == head->next)
    {
        printf("尾删:元素 %d\n", head->data);
        free(head);
        head = NULL;
        return head;
    }

    Linklist p = head;
    while (p->next->next)
    {
        p = p->next;
    }

    Linklist s = p->next;
    p->next = NULL;
    printf("尾删:元素 %d\n", s->data);
    free(s);
    s = NULL;

    return head;
}

int length(Linklist head)
{
    int len = 0;
    Linklist p = head;
    while (p)
    {
        p = p->next;
        len++;
    }

    return len;
}

Linklist insert_pos(Linklist head, datatype element, int pos)
{
    if (pos < 1 || pos > length(head) + 1)
    {
        puts("插入:位置不合法");
        return head;
    }

    if (1 == pos)
    {
        printf("插入:");
        head = insert_head(head, element);
        return head;
    }

    Linklist p = head;
    for (int i = 1; i < pos - 1; i++)
    {
        p = p->next;
    }

    Linklist s = create();
    s->data = element;
    s->next = p->next;
    p->next = s;
    printf("插入:在 %d 处插入元素 %d\n", pos, s->data);

    return head;
}

Linklist delete_pos(Linklist head, int pos)
{
    if (pos < 1 || pos > length(head) + 1)
    {
        puts("删除:位置不合法");
        return head;
    }

    if (1 == pos)
    {
        printf("删除:删除 1 处元素 %d\n", head->data);
        free(head);
        head = NULL;
        return head;
    }

    Linklist p = head;
    for (int i = 1; i < pos - 1; i++)
    {
        p = p->next;
    }

    Linklist s = p->next;
    p->next = s->next;
    printf("删除:删除 %d 处元素 %d\n", pos, s->data);
    free(s);
    s = NULL;

    return head;
}

Linklist change_pos(Linklist head, datatype element, int pos)
{
    if (pos < 1 || pos > length(head) + 1)
    {
        puts("删除:位置不合法");
        return head;
    }

    if (1 == pos)
    {
        printf("删除:删除 1 处元素 %d\n", head->data);
        free(head);
        head = NULL;
        return head;
    }

    Linklist p = head;
    for (int i = 1; i < pos - 1; i++)
    {
        p = p->next;
    }

    Linklist s = p->next;
    p->next = s->next;
    printf("删除:删除 %d 处元素 %d\n", pos, s->data);
    free(s);
    s = NULL;

    return head;
}

Linklist search_element(Linklist head, datatype element)
{
    if (NULL == head)
    {
        puts("按元素查找:链表为空");
        return head;
    }

    int i = 1;
    Linklist q = head;
    for (i = 1; q != NULL; i++)
    {
        if (q->data == element)
        {
            break;
        }

        q = q->next;
    }

    if (NULL == q)
    {
        printf("按元素查找:%d 不存在\n", element);
        return head;
    }

    printf("按元素查找:%d 在 %d 位\n", element, i);

    return head;
}

Linklist change_element(Linklist head, datatype old_element, datatype element)
{
    if (NULL == head)
    {
        puts("按元素修改:链表为空");
        return head;
    }

    int i = 1;
    Linklist q = head;
    for (i = 1; q != NULL; i++)
    {
        if (q->data == old_element)
        {
            break;
        }

        q = q->next;
    }

    if (NULL == q)
    {
        printf("按元素修改:%d 不存在\n", element);
        return head;
    }

    q->data = element;
    printf("按元素修改:%d 位的 %d 修改为 %d\n", i, old_element, element);

    return head;
}

Linklist delete_element(Linklist head, datatype element)
{
    if (NULL == head)
    {
        puts("按元素删除:链表为空");
        return head;
    }

    int i = 1;
    Linklist q = head;
    for (i = 1; q != NULL; i++)
    {
        if (q->data == element)
        {
            break;
        }

        q = q->next;
    }

    if (NULL == q)
    {
        printf("按元素删除:%d 不存在\n", element);
        return head;
    }

    if (head->data == element)
    {
        q = head;
        head = head->next;
        free(q);
        q = NULL;
        printf("按元素删除:删除在 %d 位的 %d\n", i, element);
        return head;
    }

    Linklist p = head;
    while (p->next != q)
    {
        p = p->next;
    }

    p->next = q->next;
    free(q);
    q = NULL;
    printf("按元素删除:删除在 %d 位的 %d\n", i, element);

    return head;
}

Linklist sort(Linklist head)
{
    if (NULL == head)
    {
        puts("排序:链表为空");
        return head;
    }

    if (NULL == head->next)
    {
        puts("排序:链表只有一个元素");
        return head;
    }

    Linklist p, q;
    p = head;
    while (p->next != NULL)
    {
        q = p->next;

        while (q != NULL)
        {
            if (p->data > q ->data)
            {
                datatype t = p->data;
                p->data = q->data;
                q->data = t;
            }

            q = q->next;
        }

        p = p->next;
    }
    puts("排序:完成");

    return head;
}

Linklist free_space(Linklist head)
{
    puts("清空链表");
    if (NULL == head)
    {
        return NULL;
    }

    Linklist p;
    while (head != NULL)
    {
        p = head;
        head = head->next;
        free(p);
        p = NULL;
    }

    head = NULL;

    return head;
}

test.c

#include "myfunc.h"

int main()
{
    Linklist head = NULL;

    int n;
    scanf("%d", &n);

    datatype element;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &element);
        head = insert_rear(head, element);
        if (NULL == head)
        {
            printf("插入元素 %d 失败\n", element);
        }
    }
    printf("----输入完毕----\n\n");

    show(head);

    printf("输入元素:");
    scanf("%d", &element);

    head = search_element(head, element);

    int old_element;
    printf("输入元素:");
    scanf("%d", &old_element);
    printf("输入新元素:");
    scanf("%d", &element);
    head = change_element(head, old_element, element);
    show(head);

    printf("输入元素:");
    scanf("%d", &element);
    head = delete_element(head, element);
    show(head);

    head = sort(head);
    show(head);

    head = free_space(head);
    show(head);

    return 0;
}

结果

2024.01.24作业_第1张图片

思维导图

2024.01.24作业_第2张图片

你可能感兴趣的:(其他)