嵌入式LinuxC--数据结构--单链表所有功能的整合与实现

0.头文件

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

1.定义单链表结构体

struct Node
{
    int value;
    struct Node* next;
};

2.定义新的链表节点(申请空间)

int  init(struct Node **head)
{
    struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));//申请空间

    if (NULL == newnode)
    {
        return -1;
    }
    
    newnode ->value = 0;		//节点初始化
    newnode ->next = NULL;
    *head = newnode;
}

3.打印链表(将打印指令封装成函数,方便随时调用)

void print(struct Node *head)
{
    if (head == NULL)
    {
        printf("Node is empty!\n");
        return ;
    }
    
    while (head ->next != NULL)		//若下一个节点不为空,打印其数值
    {
        printf("%d ", head ->next ->value);
        head = head ->next;
    }

4.链表尾插法

int insert_tail(struct Node *head, int x)
{
    struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));//为将要新插入到链表末端的节点申请空间

    if (NULL == newnode)
    {
        return -1;
    }
    
    newnode ->value = x;
    newnode ->next = NULL;

    while (head->next != NULL)		//遍历找到链表表尾
    {
        head = head->next;
    }
    head->next = newnode;			//将新节点接在表尾上
}

5.链表头插法

int insert_head(struct Node *head, int x)
{
    struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));

    if (NULL == newnode)
    {
        return -1;
    }
    
    newnode ->value = x;
    newnode ->next = head ->next;	//将新节点next接口接在头节点的下一个节点上
    head ->next = newnode;			//将新节点接在头节点的next接口后
}

6.计算链表长度

int length (struct Node *head)
{
    int count = 0;
    while (head -> next !=NULL)		//遍历找到链表表尾
    {
        count ++;					//每遍历一次计数器加1
        head = head ->next;
    }
    return count;
}

7.链表中间插法

int insert_index(struct Node *head, int x, int index)
{
    if (index < 0 || index > length(head))	//插入位置不符合要求
    {
        printf("index is error!\n");
        return -1;
    }
    
    for (int i = 1; i < index ; i++)		//循环遍历到需要插入的位置
    {
        head = head ->next;
    }
    struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));

    if (NULL == newnode)
    {
        return -1;
    }
    newnode ->value = x;
    newnode ->next = head ->next;
    head ->next = newnode;
}

8.按位改动链表

int update_index(struct Node *head, int x, int index)
{
    if (index < 0 || index > length(head))
    {
        printf("index is error!\n");
        return -1;
    }
    
    for (int i = 1; i < index ; i++)
    {
        head = head ->next;
    }
    head =head -> next;
    head -> value = x;
}

9.按位删除链表

int delete_index(struct Node *head, int index)
{
    if (index < 0 || index > length(head))
    {
        printf("Index is error!\n");
        return -1;
    }
    
    for (int i = 1; i < index; i++)
    {
        head = head ->next;
    }
    struct Node *ptr = head -> next;
    head -> next = ptr -> next;
    free(ptr);
}

10.按数值查找链表

int search_value(struct Node *head, int num)
{
    int count = 0;
    int place = 0;
    while (head  != NULL)
    {
        if (head -> value == num)
        {
            count ++;
            printf("The index of the number is :%d\n", place);
        }
        head = head ->next;
        place++;
    }
    printf("The sum of the number is:%d\n", count);
}

11.按照位置来查找链表

int search_index(struct Node *head, int index)
{
    if (index < 0 || index > length(head))
    {
        printf("Index is error!\n");
    }
    for (int i = 0; i < index; i++)
    {
        head = head ->next;
    }
    printf("The value of the index %d is :%d\n", index, head ->value);
    
    return 0;
}

11.按数值更新链表

int update_value(struct Node *head , int x, int num)
{
    while (head -> next != NULL)
    {
        if (head -> next -> value == num)
        {
            head -> next -> value = x;
        }
        head = head ->next;
    }
    return 0;
}

12.按数值删除链表

int delete_value(struct Node *head, int value)
{
    int len = length(head);

    for (int i = 0; i < len; i++)
    {
        if (head -> next -> value == value)
        {
            struct Node *str = head ->next;
            head -> next = str -> next;
            free(str);
        }
        else
        {
            head = head -> next;
        }
    } 
}

12.单链表排序

int sort(struct Node *head)
{
    int i = 0;
    int j = 0;
    struct Node *p = head;
    int len = length(head);

    for ( i = 0; i < len - 1; i++)
    {
        int flag = 0;
        head = p;

        for ( j = 0; j < len - i - 1; j++)
        {
            if (head ->next ->value > head ->next ->next ->value)
            {
                struct Node *ptr1 = head ->next;
                struct Node *ptr2 = head ->next ->next;
                ptr1 ->next = ptr2 ->next;
                ptr2 ->next = ptr1;
                head ->next = ptr2;
                flag = 1;
            }
            head = head ->next;

            if (flag = 0)
            {
                break;
            }
        }   
    }
}

13.单链表倒序排序(一般方法)

void reserve1(struct Node **head)
{
    struct Node *prev = NULL;
    struct Node *cur = (*head) -> next;
    struct Node *Next = cur -> next;

    while (Next != NULL)
    {
        cur ->next = prev;
        prev = cur;
        cur = Next;
        Next = cur ->next;
    }
    if (cur != NULL)
    {
        cur ->next = prev;
    }
    (*head) ->next = cur;
}

14.单链表倒序排序(递归法)

struct Node *reserver2(struct Node *head)
{
    struct Node *Next = NULL;
    if (head ->next != NULL)
    {
        Next = reserver2(head ->next);
        head ->next ->next = head;
        head ->next = NULL;
        return Next;
    }
    return head;
}

15.程序结束释放链表空间

struct Node *freeall(struct Node *head)
{
    while (head ->next != NULL)
    {
        struct Node *ptr = head;
        head = head ->next;
        free(ptr);
    }
    free(head);
    head = NULL;
    return head;
}

16.主函数(测试部分)

int main()
{
    struct Node *head;
    init(&head);

    int i = 1;
    for ( i = 1; i < 10; i++)
    {
        insert_tail(head , i);
    }
    print(head);

    for ( i = 1; i < 10; i++)
    {
        insert_head(head , i);
    }
    print(head);

    insert_index(head, 0, 8);
    print(head);

    update_index(head, 99, 8);
    print(head);

    delete_index(head, 8);
    print(head);

    search_value(head, 9);

    search_index(head, 6);

    update_value(head, 88, 9);
    print(head);

    delete_value(head, 1);
    print(head);

    sort(head);
    print(head);

    reserve1(&head);
    print(head);

    head ->next = reserver2(head ->next);
    print(head);

    head = freeall(head);
    print(head);

    return 0; 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值