【数据结构-链表】单链表

链表

特点

链表是通过任意的存储单元来存储线性表中的数据元素

链表组成

链表是由很对节点组成
带有头结点的单链表
这里写图片描述

typedef struct{
    struct node *head;//链表的头结点
}LinkList;

节点结构

这里写图片描述

typedef struct node{
    int data;
    struct node *next;
}Node;

链表的插入操作

  1. 首先得到插入位置的节点n
  2. 得到插入位置的前一个节点pre
  3. 讲新插入的节点的next指针指向n
  4. pre节点的next指向n
    这里写图片描述
    这里写图片描述
/**
 *  在p位置插入数值为i的节点
 *
 */
void insert(LinkList *list, int p, int x) {
    Node *node = (Node *)malloc(sizeof(Node));//创建一个新节点
    node->data = x;//给新节点的data赋值
    Node *n = list->head;//定义一个位置节点
    Node *pre = list->head;//定义位置前面的那个节点

    if(p == 0) {
        node->next = list->head->next;//头节点的后面那个节点赋给这个新节点的next
        list->head->next = node;//头结点的next指向这个节点
    } else {
        for(int i=1; i<=p; i++) {
            pre = n;//得到插入位置的前一个节点
            n = n->next;//得到插入位置的节点
            if(n == NULL) {
                printf("没有找到对应的位置\n;");
                return;
            }
        }
        pre->next = node;//前节点的next指向这个节点
        node->next = n;//这个节点的next指向以前的老节点
    }
}

删除操作

  1. 查找要删除的元素所在那个节点node
  2. 讲node的前一个节点的next指针指向node后面的节点
  3. free(node)
    这里写图片描述

C语言实现单链表

#include <stdio.h>
#include <stdlib.h>
/**
 *  定义一个节点结构
 */
typedef struct node{
    int data;
    struct node *next;
}Node;

typedef struct{
    struct node *head;//链表的头结点
}LinkList;

/**
 *  创建链表
 *
 *  @param list 要创建的这个链表指针
 *  @param n    表示创建一个含有n个元素的链表
 */
void createList(LinkList *list, int n) {
    Node *tail;//该指针始终只想末尾的那个节点
    list->head = (Node *)malloc(sizeof(Node));
    tail = list->head;
    for (int i=0; i<n; i++) {
        Node *n = (Node *)malloc(sizeof(Node));
        printf("请输入新节点的数据:\n");
        scanf("%d", &n->data);
        n->next = NULL;
        tail->next = n;
        tail = n;
    }
}

/**
 *  删除链表中值为x的节点
 */
void delete(LinkList *list, int x) {
    Node *pre;
    Node *node = list->head->next;
    pre = list->head;
    if(list == NULL) {
        printf("链表为空!");
    }

    while(node->data != x && node != NULL) {
        pre = node;
        node = node->next;
    }

    if(node == NULL) {
        printf("没有找到这个元素!\n");
    } else {
        pre->next = node->next;
        free(node);
    }
}

/**
 *  在p位置插入数值为i的节点
 *
 */
void insert(LinkList *list, int p, int x) {
    Node *node = (Node *)malloc(sizeof(Node));//创建一个新节点
    node->data = x;//给新节点的data赋值
    Node *n = list->head;//定义一个位置节点
    Node *pre = list->head;//定义位置前面的那个节点

    if(p == 0) {
        node->next = list->head->next;//头节点的后面那个节点赋给这个新节点的next
        list->head->next = node;//头结点的next指向这个节点
    } else {
        for(int i=1; i<=p; i++) {
            pre = n;//得到插入位置的前一个节点
            n = n->next;//得到插入位置的节点
            if(n == NULL) {
                printf("没有找到对应的位置\n;");
                return;
            }
        }
        pre->next = node;//前节点的next指向这个节点
        node->next = n;//这个节点的next指向以前的老节点
    }
}

/**
 *  打印链表
 *
 *  @param list 要打印的链表
 */
void display(LinkList *list) {
    Node *node = list->head->next;
    while(node != NULL) {
        printf("链表元素%d\n", node->data);
        node = node->next;
    }
}

int main() {
    LinkList *list;
    createList(list, 4);
    display(list);
    delete(list, 3);
    printf("删除后的元素\n");
    display(list);
    insert(list, 2, 3);
    printf("插入后的元素\n");
    display(list);
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值