链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
- 单向、双向
- 带头、不带头
- 循环、非循环
虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:
- 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶。
- 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了。
无头单向非循环链表
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
struct Node* _next;
DataType _data;
}Node;
//实现不带头单向非循环链表
typedef struct LinkList
{
Node* _head;
}LinkList;
//初始化
void Init(LinkList* lk)
{
lk->_head = NULL;
}
//创建节点
Node* CreatNode(DataType val)
{
Node* node = (Node*)malloc(sizeof(Node));
node->_next = NULL;
node->_data = val;
return node;
}
//尾插
void PushBack(LinkList* lk, DataType val)
{
Node* node = CreatNode(val);
if (lk->_head == NULL)
{
lk->_head = node;
}
else
{
Node* cur = lk->_head;
while (cur->_next)
{
cur = cur->_next;
}
cur->_next = node;
}
}
//尾删
void PopBack(LinkList* lk)
{
assert(lk->_head != NULL);
Node* prev = NULL;
Node* cur = lk->_head;
while (cur->_next)
{
prev = cur;
cur = cur->_next;
}
if (cur == lk->_head)
{
lk->_head = NULL;
}
else
{
prev->_next = NULL;
}
free(cur);
}
//头插
void PushFront(LinkList* lk, DataType val)
{
Node* node = CreatNode(val);
if (lk->_head == NULL)
{
lk->_head = node;
}
else
{
node->_next = lk->_head;
lk->_head = node;
}
}
//头删
void PopFront(LinkList* lk)
{
assert(lk->_head != NULL);
Node* next = lk->_head->_next;
free(lk->_head);
lk->_head = next;
}
//在pos位置后插入元素
void InsertAfter(Node* pos, DataType val)
{
if (pos == NULL)
{
return;
}
Node* node = CreatNode(val);
node->_next = pos->_next;
pos->_next = node;
}
//删除pos位置后的元素
void EraseAfter(Node* pos)
{
if (pos == NULL)
{
return;
}
Node* next = pos->_next;
if (next)
{
pos->_next = next->_next;
free(next);
}
}
//查找
Node* Find(LinkList* lk, DataType val)
{
Node* cur = lk->_head;
while (cur)
{
if (cur->_data == val)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
//销毁
void Destroy(LinkList* lk)
{
if (lk->_head)
{
Node* cur = lk->_head;
while (cur)
{
Node* next = cur->_next;
free(cur);
cur = next;
}
}
}
//打印链表
void Print(LinkList* lk)
{
Node* cur = lk->_head;
while (cur)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
printf("\n");
}
//查找链表中倒数第二个节点
Node* FindLastPrev(LinkList* lk)
{
if (lk->_head == NULL || lk->_head->_next == NULL)
{
return NULL;
}
Node* cur = lk->_head;
while (cur->_next->_next)
{
cur = cur->_next;
}
return cur;
}
//删除链表中第一个遇到的data所在的节点
void Remove(LinkList* lk, DataType val)
{
if (lk->_head == NULL)
{
return;
}
Node* cur = lk->_head;
if (lk->_head->_data == val)
{
lk->_head = cur->_next;
free(cur);
return;
}
while (cur->_next)
{
if (cur->_next->_data == val)
{
Node* next = cur->_next;
cur->_next = next->_next;
free(next);
return;
}
cur = cur->_next;
}
}
测试用例
void Test()
{
LinkList ll;
Init(&ll);
PushBack(&ll, 1);
PushFront(&ll, 2);
PushFront(&ll, 3);
PushFront(&ll, 4);
PushFront(&ll, 5);
PushFront(&ll, 6);
PushBack(&ll, 2);
PushBack(&ll, 3);
PushBack(&ll, 4);
PushBack(&ll, 5);
PushBack(&ll, 6);
PushBack(&ll, 7);
//654321234567
Print(&ll);
PopFront(&ll);
Print(&ll);
PopFront(&ll);
Print(&ll);
PopFront(&ll);
Print(&ll);
PopFront(&ll);
Print(&ll);
PopFront(&ll);
Print(&ll);
PopFront(&ll);
Print(&ll);
PopFront(&ll);
Print(&ll);
PushBack(&ll, 1);
PushFront(&ll, 2);
PushFront(&ll, 3);
PushFront(&ll, 4);
PushFront(&ll, 5);
PushFront(&ll, 6);
PushBack(&ll, 2);
PushBack(&ll, 3);
PushBack(&ll, 4);
PushBack(&ll, 5);
PushBack(&ll, 6);
PushBack(&ll, 7);
//65432345671234567
Print(&ll);
}
void Test2()
{
LinkList ll;
Init(&ll);
PushBack(&ll, 1);
PushBack(&ll, 2);
PushBack(&ll, 3);
PushBack(&ll, 4);
PushBack(&ll, 5);
PushBack(&ll, 6);
PushBack(&ll, 7);
//1234567
Print(&ll);
PopBack(&ll);
Print(&ll);
PopBack(&ll);
Print(&ll);
PopBack(&ll);
Print(&ll);
PopBack(&ll);
Print(&ll);
PopBack(&ll);
Print(&ll);
PopBack(&ll);
Print(&ll);
PopBack(&ll);
Print(&ll);
}
void Test3()
{
LinkList ll;
Init(&ll);
PushBack(&ll, 1);
PushBack(&ll, 2);
PushBack(&ll, 3);
PushBack(&ll, 4);
PushBack(&ll, 5);
PushBack(&ll, 6);
PushBack(&ll, 7);
//1234567
Print(&ll);
}
void Test4()
{
LinkList ll;
Init(&ll);
PushBack(&ll, 1);
PushBack(&ll, 2);
PushBack(&ll, 3);
PushBack(&ll, 4);
PushBack(&ll, 5);
PushBack(&ll, 6);
PushBack(&ll, 7);
//1234567
Print(&ll);
Node* pos = Find(&ll, 5);
InsertAfter(pos, 100);
Print(&ll);
}
void Test5()
{
LinkList ll;
Init(&ll);
PushBack(&ll, 1);
PushBack(&ll, 2);
PushBack(&ll, 3);
PushBack(&ll, 4);
PushBack(&ll, 5);
PushBack(&ll, 6);
PushBack(&ll, 7);
//1234567
Print(&ll);
Node* pos = FindLastPrev(&ll);
InsertAfter(pos, 600);
Print(&ll);
PushBack(&ll, 6);
Print(&ll);
Remove(&ll, 6);
Print(&ll);
}
带头双向循环链表
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
struct Node* _next;
struct Node* _prev;
DataType _data;
}Node;
//带头双向循环链表
typedef struct DoubleLoopList
{
Node* _head;
}DoubleLoopList;
//创建节点
Node* CreatNode(DataType val)
{
Node* node = (Node*)malloc(sizeof(Node));
node->_next = NULL;
node->_prev = NULL;
node->_data = val;
return node;
}
//初始化
void Init(DoubleLoopList* ls)
{
ls->_head = CreatNode(0);
ls->_head->_next = ls->_head;
ls->_head->_prev = ls->_head;
}
//尾插
void PushBack(DoubleLoopList* ls, DataType val)
{
Node* node = CreatNode(val);
Node* last = ls->_head->_prev;
last->_next = node;
node->_prev = last;
node->_next = ls->_head;
ls->_head->_prev = node;
}
//尾删
void PopBack(DoubleLoopList* ls)
{
if (ls->_head->_next == ls->_head)
{
return;
}
Node* last = ls->_head->_prev;
Node* prev = last->_prev;
prev->_next = ls->_head;
ls->_head->_prev = prev;
free(last);
}
//头插
void PushFront(DoubleLoopList* ls, DataType val)
{
Node* node = CreatNode(val);
Node* next = ls->_head->_next;
ls->_head->_next = node;
node->_prev = ls->_head;
node->_next = next;
next->_prev = node;
}
//头删
void PopFront(DoubleLoopList* ls)
{
if (ls->_head->_next == ls->_head)
{
return;
}
Node* cur = ls->_head->_next;
Node* next = cur->_next;
ls->_head->_next = next;
next->_prev = ls->_head;
free(cur);
}
//在pos位置前插入元素
void Insert(Node* pos, DataType val)
{
Node* node = CreatNode(val);
Node* prev = pos->_prev;
node->_next = pos;
pos->_prev = node;
prev->_next = node;
node->_prev = prev;
}
//删除pos所在位置的元素
void Erase(Node* pos)
{
if (pos == NULL)
{
return;
}
Node* prev = pos->_prev;
Node* next = pos->_next;
free(pos);
prev->_next = next;
next->_prev = prev;
}
//查找
Node* Find(DoubleLoopList* ls, DataType val)
{
if (ls->_head->_next == ls->_head)
{
return NULL;
}
Node* cur = ls->_head->_next;
while (cur != ls->_head)
{
if (cur->_data == val)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
//销毁
void Destroy(DoubleLoopList* ls)
{
Node* cur = ls->_head->_next;
while (cur != ls->_head)
{
Node* next = cur->_next;
free(cur);
cur = next;
}
free(ls->_head);
ls->_head = NULL;
}
//打印链表
void Print(DoubleLoopList* ls)
{
Node* cur = ls->_head->_next;
while (cur != ls->_head)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
printf("\n");
}
测试用例
void Test()
{
DoubleLoopList ls;
Init(&ls);
Print(&ls);
PushBack(&ls, 1);
Print(&ls);
PushBack(&ls, 2);
Print(&ls);
PushBack(&ls, 3);
Print(&ls);
PushBack(&ls, 4);
Print(&ls);
PopBack(&ls);
Print(&ls);
PopBack(&ls);
Print(&ls);
PopBack(&ls);
Print(&ls);
PopBack(&ls);
Print(&ls);
PopBack(&ls);
Print(&ls);
Destroy(&ls);
}
void Test2()
{
DoubleLoopList ls;
Init(&ls);
PushFront(&ls, 1);
Print(&ls);
PushFront(&ls, 2);
Print(&ls);
PushFront(&ls, 3);
Print(&ls);
PushFront(&ls, 4);
Print(&ls);
PopFront(&ls);
Print(&ls);
PopFront(&ls);
Print(&ls);
PopFront(&ls);
Print(&ls);
PopFront(&ls);
Print(&ls);
PopFront(&ls);
Print(&ls);
Destroy(&ls);
}
void Test3()
{
DoubleLoopList ls;
Init(&ls);
PushBack(&ls, 1);
PushBack(&ls, 2);
PushBack(&ls, 3);
PushBack(&ls, 4);
Print(&ls);
Node* pos = Find(&ls, 3);
Insert(pos, 20);
Print(&ls);
Erase(pos);
Print(&ls);
Destroy(&ls);
}