链表的概念和结构
概念
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表
中的指针链接次序实现的 。
注意:
1. 从上图可看出,链式结构在逻辑上是连续的,但是在物理上不一定连续
2. 现实中的结点一般都是从堆上申请出来的
3. 从堆上申请的空间,是按照一定的策略来分配的,两次申请的空间可能连续,也可能不连续
结构
今天我们主要来实现一下无头单向非循环链表的增删改查
无头单向非循环链表:
结构简单,一般不会单独用来存数据。实际中更多作为其他数据结构的子结构,如哈希桶、图的邻接表等等。
单链表的实现
首先我们需要创建三个源文件:SList.c SList.h test.c
SList.h中我们用来创建结构体并且对函数进行声明
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SLNDataType;
typedef struct SListNode
{
SLNDataType val;
struct SListNode* next;
}SLNode;
void SLTPrint(SLNode* phead); //打印
SLNode* CreateNode(SLNDataType x); //创建新节点
void SLTPushBack(SLNode** pphead, SLNDataType x); //尾插
void SLTPushFront(SLNode** pphead, SLNDataType x); //头插
void SLTPopBack(SLNode** pphead); //尾删
void SLTPopFront(SLNode** pphead); //头删
SLNode* SLTFind(SLNode* phead, SLNDataType x); //查找
void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x); //中间插入
void SLTEraseAfter(SLNode* pos); //中间删除 删除pos后的位置
void SLTDestory(SLNode** pphead); //销毁
在SList.c中 对函数进行定义
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
void SLTPrint(SLNode* phead) //打印
{
SLNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->val);
cur = cur->next;
}
printf("NULL\n");
}
SLNode* CreateNode(SLNDataType x) //创建新节点
{
SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
void SLTPushBack(SLNode** pphead, SLNDataType x) //尾插
{
assert(pphead);
SLNode* newnode = CreateNode(x);
if (*pphead == NULL)
*pphead = newnode;
else
{
//找尾
SLNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SLTPushFront(SLNode** pphead, SLNDataType x) //头插
{
assert(pphead);
SLNode* newnode = CreateNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLTPopBack(SLNode** pphead) //尾删
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void SLTPopFront(SLNode** pphead) //头删
{
assert(pphead);
assert(*pphead);
SLNode* tmp = *pphead;
*pphead = (*pphead)->next;
free(tmp);
}
SLNode* SLTFind(SLNode* phead, SLNDataType x) //查找
{
SLNode* cur = phead;
while (cur)
{
if (cur->val == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x) //中间插入 pos前
{
assert(pphead);
assert(*pphead);
assert(pos);
if (*pphead == pos)
{
//头插
void SLTPushFront(pphead, x);
}
else
{
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
SLNode* newnode = CreateNode(x);
newnode->next = pos;
prev->next = newnode;
}
}
void SLTErase(SLNode** pphead, SLNode* pos) //中间删除 删除pos当前位置
{
assert(pphead);
assert(*pphead);
assert(pos);
if (*pphead == pos)
{
void SLTPopFront(pphead); //头删
}
else
{
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
void SLTInsertAfter(SLNode* pos,SLNDataType x) //中间插入 pos后
{
assert(pos);
SLNode* newnode = CreateNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SLTEraseAfter(SLNode* pos) //中间删除 删除pos后的位置
{
assert(pos);
assert(pos->next);
SLNode* tmp = pos->next;
pos->next = pos->next->next;
free(tmp);
tmp = NULL;
}
void SLTDestory(SLNode** pphead) //销毁
{
assert(pphead);
SLNode* cur = *pphead;
while (cur)
{
SLNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
test.c中对所写的函数进行测试
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
int main()
{
SLNode* plist = NULL;
SLTPushBack(&plist, 1);
SLTPushBack(&plist, 2);
SLTPushBack(&plist, 3);
SLTPushBack(&plist, 4);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPrint(plist);
SLTPushFront(&plist, 5);
SLTPrint(plist);
SLTPopFront(&plist);
SLTPrint(plist);
SLNode* pos = SLTFind(plist, 3);
SLTInsert(&plist, pos, 30);
SLTPrint(plist);
return 0;
}
结果
本文使用C语言对单链表的增删改查进行了模拟实现。
单链表(Singly Linked List)是一种数据结构,在该结构中,每个节点都包含一个指向下一个节点的指针。它由一系列的节点组成,每个节点都有一个存储数据元素和一个指向下一个节点的指针。单链表的特点是插入和删除的时间复杂度为O(1),但访问某个元素的时间复杂度为O(n)。由于内存不需要连续,因此单链表能够动态地分配内存,可以随时进行节点的插入和删除,故单链表在很多场合下被广泛使用。
文章如有需要改正或提高的地方请大家指正出来,谢谢大家的阅读!