美文网首页
数据结构--单链表

数据结构--单链表

作者: Bean_Nan | 来源:发表于2017-04-15 21:48 被阅读0次

插入,删除,反转

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

typedef struct Node {
    int data;
    struct Node *next;
}Node, *LinkedList;
//插入方法
LinkedList insert(LinkedList head, Node *node, int index) {
    if (head == NULL) {
        if (index != 0) {
            return head;
        }
        head = node;
        return head;
    }
    if (index == 0) {
        node->next = head;
        head = node;
        return head;
    }
    Node *current_node = head;
    int count = 0;

    while (current_node->next != NULL && count < index - 1) {//双重保证 避免了index超出范围
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1) {//确保能到了指定结点前一个结点
        node->next = current_node->next;
        current_node->next = node;
    }
    return head;
}

//删除结点
LinkedList delete_node(LinkedList head, int index) {
    //1如果头结点为空的话
    if (head == NULL) {
        return head;
    }
    //2如果删除头结点
    if (index == 0) {
        head = head->next;
        return head;
    }
    //其他情况
    Node *current_node = head;
    int count = 0;

    while (current_node->next != NULL && count < index - 1) {
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1) {//记得释放删除结点的内存
        Node *delete_node = current_node->next;
        current_node->next = delete_node->next;
        free(delete_node);
    }

    return head;
}
//链表反转
LinkedList reverse(LinkedList head) {
    if (head == NULL) {
        return head;
    }
    Node *current_node, *next_node; //一个是用来保存当前遍历的结点 一个是用来存储当前遍历结点的下一个结点
    current_node = head->next; //首先移动到第2个结点
    head->next = NULL;//将首结点的下一个结点设置为空

    while (current_node != NULL) {
        next_node = current_node->next;//将当前结点的下一个结点保存起来
        current_node->next = head;//将当前结点的下一个结点设置为头结点
        head = current_node;//将头结点设置为当前结点
        current_node = next_node;//将当前结点进行更新
    }

    return head;
}
//链表内存的清空
void clear(LinkedList head) {
    Node *current_node = head;
    while (current_node != NULL) {
        Node *delete_node = current_node;
        current_node = current_node->next;
        free(delete_node);
    }
}
 

相关文章

  • 数据结构 | 其二 链表

    冰河winner - 数据结构之链表 2.1 单向链表 数据结构(一) 单链表的实现-JAVA 2.2 双端链表 ...

  • 数据结构——Golang实现单链表

    转载请注明出处:数据结构——Golang实现单链表 1. 单链表 1.1. 定义 单向链表(单链表)是链表的一种,...

  • 算法与数据结构知识汇总(二、链表)

    1、概念 2、链表的数据结构 单向链表的数据结构如下图: 上图数据结构为单向链表,简称单链表,该数据结构由若干个节...

  • 数据结构--单链表

    数据结构--单链表 单链表:单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中...

  • 2018-12-01

    数据结构使用二级指针创建单链表以及单链表的各种操作 //单链表创建 #include #include #incl...

  • 数据结构笔记

    数据结构课程概览 ================== 1.顺序表 2.链表:单链表,单向循环链表,双链表...

  • 常见数据结构和算法

    常见数据结构 线性数据结构(按顺序具有数据元素的数据结构):数组,堆栈,链表(单链表 双链表),队列非线性数据结...

  • 单链表

    1.单链表## 对数据结构一直比较欠缺,所以准备i从头学习一下数据结构。从单链表开始,链表的介绍和定义就省略了,我...

  • 链表简介

    链表简介 链表是一种线性数据结构 链表有两种分别为 单链表 伪代码如下: 双链表 伪代码如下: 链表添加操作 单链...

  • 专项练习数据结构之链表

    1.链表:单链表,双链表,循环链表 2.单链表 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表...

网友评论

      本文标题:数据结构--单链表

      本文链接:https://www.haomeiwen.com/subject/uusoattx.html