数据结构:详细的说明链表的一系列操作

本文详细介绍了链表的基本概念、操作,并提供了有头单向不循环链表的示意图。涵盖创建、清空、销毁链表以及各种插入、删除、查找和修改操作。同时,给出了C语言实现这些操作的头文件和函数文件,包括头插法、尾插法、按位置插入和删除等。此外,还涉及链表的合并、翻转和排序等高级操作。
摘要由CSDN通过智能技术生成

链表:

1.1 概念

逻辑结构:线性结构

存储结构:链式存储

1.2有头单向不循环链表的示意图

1.3链表相关的操作:

1.创建链表 

2.清空链表 

3.销毁链表 

4.头插法 

5.尾插法 

6.任意位置插入法 

7.头删法 

8.尾删法 

9.任意位置删除法 

10.查询链表中是否有想要的数据(在链表中获取指定位置的数据) 

11.按照位置修改链表中的数据 

12.按照值修改链表中的数据 

13.两个链表合并 

14.链表的排序

15.链表的翻转

16.遍历链表的节点----学习阶段看现象用的 

1.4 操作示意图

1.4.1链表插入数据示意图

 

1.4.2 链表删除数据示意图

 

1.4.3链表的排序示意图

 

1.5代码实现

1.5.1头文件:

#ifndef __LINK_LIST_H_
#define __LINKE_LIST_H_

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

typedef struct __Node{
    int data;
    struct __Node *next;
}node_t;

int create_node(node_t **p, int in_data);
int insert_data_by_head(node_t *phead, int in_data);
int print_link_list(node_t *phead);
int insert_data_by_tail(node_t *phead, int in_data);
int insert_data_by_pos(node_t *phead, int in_data, int pos);
int del_from_list_by_head(node_t *phead);
int del_from_list_by_tail(node_t *phead);
int del_from_list_by_pos(node_t *phead, int pos);
int update_list_by_pos(node_t *phead, int pos, int new_data);
int update_list_by_value(node_t *phead, int old_value, int new_value);
int clean_list(node_t *phead);
int destroy_list(node_t **phead);
int merge_two_list(node_t *phead1, node_t **phead2);
int overturn_list(node_t *phead);
int sort_list(node_t *phead);
int sort_list(node_t *phead);
#endif

1.5.2函数文件:

#include "link_list.h"

//创建头节点
int create_node(node_t **p, int in_data)
{
    if (p == NULL){
        printf("传参错误\n");
        return -1;
    }

    *p = (node_t*)malloc(sizeof(node_t));

    (*p)->next = NULL;
    (*p)->data = in_data;

    return 0;
}
//头插法
int insert_data_by_head(node_t *phead, int in_data)
{
     if (phead == NULL){
        printf("传参错误\n");
        return -1;
    }

    node_t* temp;
    create_node(&temp, in_data);

    temp->next = phead->next;
    phead->next = temp;

    return 0;
}
//打印
int print_link_list(node_t *phead)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    node_t *temp = phead;

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

    puts("");
    
    return 0;
}
//尾插法
int insert_data_by_tail(node_t *phead, int in_data)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    node_t* temp = phead;
    node_t* pnew;
    create_node(&pnew, in_data);

    while (temp->next != NULL){
        temp = temp->next;
    }

    temp->next = pnew;

    return 0;
}
//按位置插入
int insert_data_by_pos(node_t *phead, int in_data, int pos)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    node_t* temp = phead;
    node_t* pnew;
    int i;

    for (i = 0; i < pos -1; i++){
        temp = temp->next;
        if (temp == NULL){
            break;
        }
    }

    if (i < pos - 1){
        printf("位置输入错误\n");
        return -1;
    }

    create_node(&pnew, in_data);

    pnew->next = temp->next;
    temp->next = pnew;

    return 0;
}
//头删法
int del_from_list_by_head(node_t *phead)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空,不需要删除\n");
        return -1;
    }

    node_t* temp;
    temp = phead->next;

    phead->next = temp->next;
    free(temp);
    temp = NULL;

    return 0;
}
//尾删法
int del_from_list_by_tail(node_t *phead)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空,不需要删除\n");
        return -1;
    }

    node_t* temp = phead;

    while (temp->next->next != NULL){
        temp = temp->next;
    }

    free(temp->next);
    temp->next = NULL;

    return 0;
}
//按位置删除法
int del_from_list_by_pos(node_t *phead, int pos)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (pos <= 0){
        printf("插入位置不合理\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空,不需要删除\n");
        return -1;
    }

    node_t* temp = phead;
    int i;
    for (i = 0; i< pos -1; i++){
        temp = temp->next;
        if (temp->next == NULL){
            break;
        }
    }

    if (i < pos - 1){
        printf("输入位置错误\n");
        return -1;
    }

    node_t* pdel;
    pdel = temp->next;
    temp->next = pdel->next;

    free(pdel);
    pdel = NULL;

    return 0;
}
//按位置修改数据
int update_list_by_pos(node_t *phead, int pos, int new_data)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (pos <= 0){
        printf("修改位置不合理\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空,不需要删除\n");
        return -1;
    }

    node_t* temp = phead;

    for (int i=0; i < pos; i++){
        temp = temp->next;
        if (temp == NULL){
            printf("输入位置错误\n");
            break;
        }        
    }

    temp->data = new_data;

    return 0;
}
//按数据修改数据
int update_list_by_value(node_t *phead, int old_value, int new_value)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空,不需要删除\n");
        return -1;
    }

    node_t* temp = phead;
    while (temp->next != NULL){
        temp = temp->next;
        if (temp->data == old_value){
            temp->data = new_value;

            return 0;
        }
    }

    printf("没有这个数据,请重新输入\n");
    return -1;
}  
//清空除头节点的其它链表
int clean_list(node_t *phead)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空,不需要清空\n");
        return -1;
    }

    
    node_t* del;

    while(phead->next != NULL){
        del = phead->next;
        phead->next = del->next;

        free(del);
        del = NULL;      
    }


    return 0;
}
//销毁链表
int destroy_list(node_t **phead)
{
    if (NULL == phead || *phead ==NULL){
        printf("传参错误\n");
        return -1;
    }

    clean_list(*phead);

    free(*phead);
    (*phead)->next = NULL;

    return 0;
}
//合并两个链表
int merge_two_list(node_t *phead1, node_t **phead2)
{
    if (NULL == phead1 || NULL == phead2 || *phead2 == NULL){
        printf("输入参数为0,请检查\n");
        return -1;
    }

    node_t* temp = phead1;
    while (temp->next != NULL){
        temp = temp->next;
    }

    temp->next = (*phead2)->next;

    free(*phead2);
    *phead2 = NULL;

    return 0;
}
//翻转链表
int overturn_list(node_t *phead)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    node_t* p = NULL;
    node_t* q = NULL;
    p = phead->next;
    phead->next = NULL;

    while (p!=NULL){
        q=p;
        p = p->next;
        q->next = phead->next;
        phead->next = q;
    }

    return 0;
}
//对链表进行排序
int sort_list(node_t *phead)
{
    if (NULL == phead){
        printf("传参错误\n");
        return -1;
    }

    if (phead->next == NULL){
        printf("列表为空不需要排列\n");
        return -1;
    }

    if (phead->next->next == NULL){
        printf("列表就一个数据,不需要排列\n");
        return -1;
    }

    node_t* p = phead->next;
    node_t* q = p->next;
    int temp;

    while (p->next != NULL){
        while (q != NULL){
            if (p->data > q->data){
                temp = p->data;
                p->data = q->data;
                q->data = temp;
            }
            q = q->next;
        }
        p = p->next;
        q = p;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值