C语言实现链表的各种功能

简介: 本文详细介绍了如何使用C语言实现链表的各种功能,包括链表节点结构的定义与操作函数的实现。链表作为一种常用的数据结构,具有节点自由插入删除、动态变化等特点。文中通过`link_list.h`和`link_list.c`两个文件,实现了链表的初始化、插入、删除、查找、修改等核心功能,并在`main.c`中进行了功能测试。这些代码不仅展示了链表的基本操作,还提供了丰富的注释帮助理解,适合作为学习链表的入门资料。

C语言实现链表的各种功能

链表的定义

链表是一种数据结构,它是由一系列节点组成的线性结构。每个节点包含两个部分:数据和指针。数据部分存储着实际的数据,指针部分指向下一个节点。

链表的特点是:

  • 每个节点都可以自由地插入或删除。
  • 链表的第一个节点称为头节点,最后一个节点称为尾节点。
  • 链表中节点的数量可以动态变化。

链表的实现

链表的实现可以分为两步:

  1. 定义链表节点的结构。
  2. 实现链表的操作函数。

定义链表节点的结构

文件名:link_list.h

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

typedef int ElemType;

typedef struct linkList{
   
    ElemType data;
    struct linkList* next;
}LNode;

//初始化链表
bool initList(LNode** head);

//链表是否为空
bool isEmpty(LNode* head);

//链表长度
int length(LNode* head);

//链表头部插入元素
bool insert(LNode* head, ElemType data);

//链表尾部插入元素
bool append(LNode* head, ElemType data);

//插入链表指定位置
bool insertAt(LNode* head, ElemType data, int pos);

//链表删除元素头删
bool delete(LNode* head);

//链表删除指定位置元素
bool deleteAt(LNode* head, int pos);

//删除链表元素尾删
bool deleteTail(LNode* head);


//链表中查找指定位置元素
LNode* searchAt(LNode* head, int pos);

//修改链表指定位置元素
bool modify(LNode* head, ElemType data, int pos);

//清空链表
bool clearList(LNode* head);

//销毁链表
bool destroyList(LNode** head);

//打印链表
bool printList(LNode* head);

实现链表的操作函数

文件名:link_list.c


#include "link_list.h"

#define DEBUG_PRINT 1

//初始化链表
bool initList(LNode** head){
   
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("初始化链表的入参为空\n");
#endif
        return false;
    }
    //初始化  calloc()申请的空间自动初始化为0
    (*head)=(LNode*)calloc(1, sizeof(LNode));
    (*head)->next=NULL;
    (*head)->data=0;//头节点中数据用来保存链表中的元素个数
    return true;
}
//链表是否为空
bool isEmpty(LNode* head){
   
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("isEmpty()的入参为空\n");
#endif
        return false;
    }
    if(head->next==NULL){
   
        return true;
    }
    return false;
}
//链表长度
int length(LNode* head){
   
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("length()的入参为空\n");
#endif
        return false;
    }
    return head->data;
}
//链表头部插入元素
bool insert(LNode* head, ElemType data){
   
    //入参判断
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("insert()的入参为空\n");
#endif
        return false;
    }

    //为新节点申请空间
    LNode * newNode=(LNode*)calloc(1, sizeof(LNode));
    newNode->data=data;
    newNode->next=head->next;
    head->next=newNode;
    head->data++;
}
//链表尾部插入元素
bool append(LNode* head, ElemType data){
   
    //入参判断
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("insert()的入参为空\n");
#endif
        return false;
    }
    //为新节点申请空间
    LNode * newNode=(LNode*)calloc(1, sizeof(LNode));
    newNode->data=data;

   // LNode *temp=head->next;//head->next可能为NULL,下方对temp->next=(NULL->next),会报错
    LNode *temp=head;
    while (temp->next!=NULL){
   
        temp=temp->next;
    }
    newNode->next=temp->next;
    temp->next=newNode;
    head->data++;
    return true;
}
//插入链表指定位置
bool insertAt(LNode* head, ElemType data, int pos){
   
    //入参判断
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("insertAt()的入参为空\n");
#endif
        return false;
    }
    if(pos<0|| pos>head->data ){
   
#if DEBUG_PRINT
        printf("insertAt()的插入位置不合理\n");
#endif
        return false;
    }
    LNode *temp=head;
    int  flg=0;//标记循环的次数是否到了插入位置 ,没到说明插入位置不合理
    for (int i = 0; i < pos; ++i) {
   
        flg++;
        if(temp->next!=NULL){
   
            temp=temp->next;
        } else{
   
            break;
        }
    }
    if(flg==pos){
   
        //为新节点申请空间
        LNode * newNode=(LNode*)calloc(1, sizeof(LNode));
        newNode->data=data;
        newNode->next= temp->next;
        temp->next=newNode;
        head->data++;
        return true;
    } else{
   
        return false;
    }
}
//链表删除元素头删
bool delete(LNode* head){
   
    //入参判断
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("delete()的入参为空\n");
#endif
        return false;
    }
    if(isEmpty(head)){
   
        printf("链表已空\n");
        return true;
    }
    LNode *temp=head->next;
    head->next=head->next->next;
    free(temp);
    head->data--;
    return true;

}
//链表删除指定位置元素
bool deleteAt(LNode* head, int pos){
   
    //入参判断
    if(NULL==head ){
   
#if DEBUG_PRINT
        printf("deleteAt()的入参为空\n");
#endif
        return false;
    }
    if(NULL==head->next){
    //要判断NULL==head->next 下面temp=head->next
        printf("空表不能删除\n");
        return false;
    }
    if(pos<0|| pos >= head->data ){
   
#if DEBUG_PRINT
        printf("deleteAt()的删除位置不合理\n");
#endif
        return false;
    }
    LNode *temp=head->next; //temp=head->next保证删除0位置也正常返回
    LNode *P=head;
    int flg=0;
    for (int i = 0; i < pos; ++i) {
   
        flg++;
        if(temp->next!=NULL){
    //保证temp->next不能指空
            P=temp;
            temp=temp->next;
        } else{
   
            break;
        }
    }
    if(flg==pos){
   //查0位置不走循环,返回了temp=head->next
        P->next=temp->next;
        free(temp);
        head->data--;
        return true;
    } else{
   
        return false;
    }

}
//删除链表元素尾删
bool deleteTail(LNode* head){
   
    //入参判断
    if(NULL==head ){
   
#if DEBUG_PRINT
        printf("delete()的入参为空\n");
#endif
        return false;
    }
    //只有头节点无需删除
    if(NULL==head->next){
   
        printf("表只有头节点\n");
        return true;
    }

    //找到尾节点的前一个节点
    LNode *temp=head;
    LNode * P=NULL;
    while (temp->next!=NULL){
   
        P=temp;
        temp=temp->next;
    }
    if(P){
   
        //循环退出时P指向尾节点的前一个节点,temp就是尾节点
        P->next=NULL;
        free(temp);
        head->data--;
    }

    return true;
}
//链表中查找指定位置元素
LNode* searchAt(LNode* head, int pos){
   
    //入参判断
    if(NULL==head ){
   
#if DEBUG_PRINT
        printf("insertAt()的入参为空\n");
#endif
        return false;
    }
    if(NULL==head->next){
    //要判断NULL==head->next 下面temp=head->next
        printf("空表不能查询\n");
        return false;
    }
    if(pos<0|| pos >= head->data ){
   
#if DEBUG_PRINT
        printf("searchAt()的查找位置不合理\n");
#endif
        return false;
    }

    LNode *temp=head->next; //temp=head->next保证查0位置也正常返回
    int flg=0;
    for (int i = 0; i < pos; ++i) {
   
        flg++;
        if(temp!=NULL){
   
            temp=temp->next;
        } else{
   
            break;
        }
    }
    if(flg==pos){
   //查0位置不走循环,返回了temp=head->next
        return temp;
    } else{
   
        return false;
    }


}
//修改链表指定位置元素
bool modify(LNode* head, ElemType data, int pos){
   
    //入参判断
    if(NULL==head ){
   
#if DEBUG_PRINT
        printf("modify()的入参为空\n");
#endif
        return false;
    }
    if(NULL==head->next){
    //要判断NULL==head->next 下面temp=head->next
        printf("空表不能查询\n");
        return false;
    }
    if(pos<0|| pos >= head->data ){
   
#if DEBUG_PRINT
        printf("modify()的修改位置不合理\n");
#endif
        return false;
    }

    LNode *temp=head->next; //temp=head->next保证修改0位置也正常返回
    int flg=0;
    for (int i = 0; i < pos; ++i) {
   
        flg++;
        if(temp!=NULL){
   
            temp=temp->next;
        } else{
   
            break;
        }
    }
    if(flg==pos){
   
        temp->data=data;
        return true;
    } else{
   
        return false;
    }
}
//清空链表
bool clearList(LNode* head){
   
    //入参判断
    if(NULL==head ){
   
#if DEBUG_PRINT
        printf("clearList()的入参为空\n");
#endif
        return false;
    }
    if(NULL==head->next){
    //要判断NULL==head->next 下面temp=head->next
        printf("链表中没有数据\n");
        return true;
    }
    LNode * temp=head->next;
    LNode *P=head;
    while (temp!=NULL){
   
        P=temp;
        temp=temp->next;
        free(P);
        head->data--;
    }
    head->next=NULL;
    return true;

}

//销毁链表
bool destroyList(LNode** head){
   
    //入参判断
    if(NULL==head || NULL==*head){
   
#if DEBUG_PRINT
        printf("destroyList()的入参为空\n");
#endif
        return false;
    }
    clearList(*head);
    free(*head);
    (*head)=NULL;
    return true;
}

//打印链表
bool printList(LNode* head){
   
    //入参检查
    if(NULL==head){
   
#if DEBUG_PRINT
        printf("length()的入参为空\n");
#endif
        return false;
    }
    if(NULL==head->next){
   
        printf("表为空\n");
        return true;
    }
    LNode *temp=head->next;
    while (temp){
   
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
    return true;
}

main.c测试功能

文件名:main.c
#include "link_list.h"

int main(){
   
    LNode *head;
    initList(&head);
    printf("head=%p\n",head);
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    printf("----------------------------\n");

    //测试头插
    insert(head,100);
    insert(head,200);
    insert(head,300);
    printList(head);
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    //300 200 100
    printf("----------------------------\n");
    //测试尾插
    append(head,400);
    append(head,500);
    append(head,600);
    printList(head);
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    //300 200 100 400 500 600
    printf("----------------------------\n");
    //测试插入链表指定位置
    insertAt(head,333,1);
    insertAt(head,444,0);
    insertAt(head,555,8);//最后一个插入
    insertAt(head,666,10);//报错///insertAt()的插入位置不合理
    printList(head);//444 300 333 200 100 400 500 600 555
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    printf("----------------------------\n");
    //链表删除元素头删
    delete(head);
    delete(head);
    printList(head);//333 200 100 400 500 600 555
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    printf("----------------------------\n");
    //删除链表元素尾删
    deleteTail(head);
    printList(head);//333 200 100 400 500 600
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));//length=6
    printf("----------------------------\n");
    //链表中查找指定位置元素
    LNode *N=NULL;
    searchAt(head,-1);//位置不合理
    N=searchAt(head,6);//位置不合理
    N=searchAt(head,5);//查找数据:600
    if(N){
   
        printf("查找数据:%d\n",N->data);
    } else{
   
        printf("没查到\n");
    }
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    printf("----------------------------\n");
    //修改链表指定位置元素
    modify(head,777,0);
    modify(head,888,6);//modify()的修改位置不合理
    printList(head);//777 200 100 400 500 600
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    printf("----------------------------\n");
    //链表删除指定位置元素
    deleteAt(head,0);
    printList(head);//200 100 400 500 600
    printf("Empty=%d\n",isEmpty(head));
    printf("length=%d\n",length(head));
    printf("----------------------------\n");
    //清空链表
    clearList(head);
    printf("head=%p\n",head);//head=0000000000B11450
    printf("head->next=%p\n",head->next);//head->next=0000000000000000
    printList(head);//表为空
    printf("Empty=%d\n",isEmpty(head));//Empty=1
    printf("length=%d\n",length(head));//length=0
    printf("----------------------------\n");

    //销毁
    destroyList(&head);
    printf("head=%p\n",head);//head=0000000000000000
    printf("----------------------------\n");
}

总结

链表是一种常见的数据结构,在实际应用中有着广泛的应用。本文介绍了链表的定义、实现、操作、测试等功能。

链表的实现主要是定义链表节点的结构,然后实现链表的操作函数。链表的操作函数包括:初始化链表、链表是否为空、链表长度、链表头部插入元素、链表尾部插入元素、插入链表指定位置、链表删除元素头删、链表删除指定位置元素、删除链表元素尾删、链表中查找指定位置元素、修改链表指定位置元素、清空链表、销毁链表、打印链表。

链表的操作函数都有相应的注释,可以很方便地理解各个函数的作用。

链表的测试代码主要是对链表的操作函数进行测试 ,可以作为学习链表的入门代码。

相关文章
|
1月前
|
C语言 索引
C语言编译环境中的 调试功能及常见错误提示
这篇文章介绍了C语言编译环境中的调试功能,包括快捷键操作、块操作、查找替换等,并详细分析了编译中常见的错误类型及其解决方法,同时提供了常见错误信息的索引供参考。
|
3月前
|
存储 缓存 前端开发
【数据结构/C语言】深入理解 双向链表
【数据结构/C语言】深入理解 双向链表
|
8天前
|
存储 C语言
C语言程序设计核心详解 第九章 结构体与链表概要详解
本文档详细介绍了C语言中的结构体与链表。首先,讲解了结构体的定义、初始化及使用方法,并演示了如何通过不同方式定义结构体变量。接着,介绍了指向结构体的指针及其应用,包括结构体变量和结构体数组的指针操作。随后,概述了链表的概念与定义,解释了链表的基本操作如动态分配、插入和删除。最后,简述了共用体类型及其变量定义与引用方法。通过本文档,读者可以全面了解结构体与链表的基础知识及实际应用技巧。
|
8天前
|
存储 算法 C语言
C语言手撕实战代码_循环单链表和循环双链表
本文档详细介绍了用C语言实现循环单链表和循环双链表的相关算法。包括循环单链表的建立、逆转、左移、拆分及合并等操作;以及双链表的建立、遍历、排序和循环双链表的重组。通过具体示例和代码片段,展示了每种算法的实现思路与步骤,帮助读者深入理解并掌握这些数据结构的基本操作方法。
|
13天前
|
网络协议 C语言
C语言 网络编程(十三)并发的TCP服务端-以进程完成功能
这段代码实现了一个基于TCP协议的多进程并发服务端和客户端程序。服务端通过创建子进程来处理多个客户端连接,解决了粘包问题,并支持不定长数据传输。客户端则循环发送数据并接收服务端回传的信息,同样处理了粘包问题。程序通过自定义的数据长度前缀确保了数据的完整性和准确性。
|
13天前
|
网络协议 C语言
C语言 网络编程(十四)并发的TCP服务端-以线程完成功能
这段代码实现了一个基于TCP协议的多线程服务器和客户端程序,服务器端通过为每个客户端创建独立的线程来处理并发请求,解决了粘包问题并支持不定长数据传输。服务器监听在IP地址`172.17.140.183`的`8080`端口上,接收客户端发来的数据,并将接收到的消息添加“-回传”后返回给客户端。客户端则可以循环输入并发送数据,同时接收服务器回传的信息。当输入“exit”时,客户端会结束与服务器的通信并关闭连接。
|
13天前
|
C语言
C语言 网络编程(八)并发的UDP服务端 以进程完成功能
这段代码展示了如何使用多进程处理 UDP 客户端和服务端通信。客户端通过发送登录请求与服务端建立连接,并与服务端新建的子进程进行数据交换。服务端则负责接收请求,验证登录信息,并创建子进程处理客户端的具体请求。子进程会创建一个新的套接字与客户端通信,实现数据收发功能。此方案有效利用了多进程的优势,提高了系统的并发处理能力。
|
13天前
|
C语言
C语言 网络编程(九)并发的UDP服务端 以线程完成功能
这是一个基于UDP协议的客户端和服务端程序,其中服务端采用多线程并发处理客户端请求。客户端通过UDP向服务端发送登录请求,并根据登录结果与服务端的新子线程进行后续交互。服务端在主线程中接收客户端请求并创建新线程处理登录验证及后续通信,子线程创建新的套接字并与客户端进行数据交换。该程序展示了如何利用线程和UDP实现简单的并发服务器架构。
|
3天前
|
C语言
C语言里的循环链表
C语言里的循环链表
|
1月前
|
存储 C语言
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍