链表的实现
我是在哔哩哔哩的 up主TyrantLucifer 的手撕数据结构课程上学习的,个人感觉非常不错,大家也可以去学习,适合有c语言基础的。注意:未考虑到malloc开辟空间错误或者取参数错误所发生的情况
1.定义一个结点结构体 并创建空表
include<iostream>
using namespace std;
typedef struct Node
{
//存放元素值
int data;
//指向后继点
struct Node* next;
}Node;//单链表结点类型
//创建空表
Node* intList()
{
//开辟空间给头结点
Node* list = (Node*)malloc(sizeof(Node));
list->data = 0;
list->next = NULL;
//返回空表的指针
return list;
}
2.头插法
//1.头插法
void headInsert(Node* list, int data)
{
//定义新插入结点 开辟空间
Node* node = (Node*)malloc(sizeof(Node));
//输入新结点数据
node->data = data;
//使node指向空
node->next = list->next;
//使list指向node
list->next = node;
//表中元素++ 更新链表的长度 L->data 代表着记录链表的长度 加入结点 长度+1
list->data++;
}
3.尾插法
//尾插法
void tailInsert(Node* list, int data)
{
//保存头节点地址
Node* head = list;
//定义插入新结点 开辟空间
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
//使新结点作为最后一个结点 所以指向空
node->next = NULL;
//使头节点指向第一个结点
list = list->next;
//循环 使其list 指向最后一个结点时停止循环
while (list->next)
{
list = list->next;
}
//list为最后的结点地址 指向新结点node
list->next = node;
//表中元素++ 更新链表的长度 head->data 代表着记录链表的长度 加入结点 长度+1
head->data++;
}
4.删除结点
//删除结点
void deleteData(Node* list, int data)
{
//保存头节点地址
Node* pre = list;
//创建current结点指向lsit的下一个结点 首结点
Node* current = list->next;
//循环 当current 地址为空时停止循环
while (current)
{
//如果找到相应data
if (current->data == data)
{
//使pre 指向 下下一个结点
pre->next = current->next;
free(current);
//表中元素--
list->data--;
//找到就结束
break;
}
//如果未找到 进行下一轮
//将pre 指向current current指向下一结点
pre = current;
current = current->next;
}
}
注意:若是删除多个相同元素data 则将break 删去即可
这里默认删除成功,删除失败的话,并未进行操作。
5.遍历链表
//遍历结点
void printList(Node* list)
{
//头节点 指向第一个元素
Node* current = list->next;
while (current)
{
printf("%d->", current->data);
current = current->next;
}
printf("NULL\n");
}
6.进行初始化链表
int main()
{
Node* list = intList();
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
headInsert(list, 4);
headInsert(list, 5);
tailInsert(list, 6);
tailInsert(list, 7);
tailInsert(list, 8);
tailInsert(list, 9);
tailInsert(list, 10);
printList(list);
deleteData(list, 6);
printList(list);
return 0;
}
7.运行结果
8.c语言完整代码
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
//存放元素值
int data;
//指向后继点
struct Node* next;
}Node;//单链表结点类型
//创建空表
Node* intList()
{
//开辟空间给头结点
Node* list = (Node*)malloc(sizeof(Node));
list->data = 0;
list->next = NULL;
//返回空表的指针
return list;
}
//1.头插法
void headInsert(Node* list, int data)
{
//定义新插入结点 开辟空间
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
list->next = node;
//表中元素++
list->data++;
}
//尾插法
void tailInsert(Node* list, int data)
{
//保存头节点地址
Node* head = list;
//定义插入新结点 开辟空间
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
//使新结点作为最后一个结点 所以指向空
node->next = NULL;
//使头节点指向第一个结点
list = list->next;
//循环 使其list 指向最后一个结点
while (list->next)
{
list = list->next;
}
list->next = node;
//表中元素++
head->data++;
}
void deleteData(Node* list, int data)
{
//保存头节点地址
Node* pre = list;
//list 指向的下一个结点
Node* current = list->next;
while (current)
{
if (current->data == data)
{
pre->next = current->next;
free(current);
//表中元素--
list->data--;
break;
}
//未找到 进行下一轮 将pre 指向current current指向下一结点
pre = current;
current = current->next;
}
}
//遍历结点
void printList(Node* list)
{
//头节点 指向第一个元素
Node* current = list->next;
while (current)
{
printf("%d->", current->data);
current = current->next;
}
printf("NULL\n");
}
int main()
{
Node* list = intList();
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
headInsert(list, 4);
headInsert(list, 5);
tailInsert(list, 6);
tailInsert(list, 7);
tailInsert(list, 8);
tailInsert(list, 9);
tailInsert(list, 10);
printList(list);
deleteData(list, 6);
printList(list);
return 0;
}