链表的概念
概念:链表是一种物理存储结构上不连续、非顺序的存储结构,数据元素逻辑顺序是通过l链表中的指针链接次序实现的。
分类
- 单向、双向
- 带头、不带头
- 循环、非循环
单项带头不循环链表操作
定义
typedef struct Node{
int value;
struct Node* next;
}Node;
初始化
void SListInit(Node* head){
head = NULL;
}
销毁
void SListDestory(Node* head){
assert(head != NULL);
Node* cur = head;
while (cur){
Node* next = cur->next;
free(cur);
cur = next;
}
head = NULL;
}
插入
//尾插
void SListPushBack(Node* head,int v){
Node* node = (Node*)malloc(sizeof(Node));
node->next = NULL;
node->value = v;
if (head == NULL){
head = node;
return;
}
Node* cur = head;
while (cur->next){
cur = cur->next;
}
cur->next = node;
}
//头插
void SListPushFront(Node* head,int v){
assert(head != NULL);
Node* node = (Node*)malloc(sizeof(Node));
node->value = v;
node->next = head;
head = node;
}
//在目标结点之后插入
void SListInsertAfter(Node* pos, int v){
Node* node = (Node*)malloc(sizeof(Node));
assert(node);
node->value = v;
node->next = pos->next;
pos->next = node;
}
//在目标之前插入
void SListInsertBefore(Node* head,Node* pos, int v){
assert(head);
Node* node = (Node*)malloc(sizeof(Node));
assert(node);
node->value = v;
Node* cur;
for (cur = head;
cur->next != pos&&cur->next != NULL; cur = cur->next);
if (cur->next == pos){
node->next = pos;
cur->next = node;
}
else{
printf("没有找到该位置\n");
}
}
删除
//尾删
void SListPopBack(Node* head){
assert(head != NULL);
if (head->next == NULL){
free(head);
}
Node* cur = head;
while (cur->next->next){
cur = cur->next;
}
free(cur->next);
cur->next = NULL;
}
//头删
void SListPopFront(Node* head){
assert(head != NULL);
Node* node = head;
head = head->next;
free(node);
}
//删除目标后的结点
void SListEraseAfter(Node* pos){
Node* next = pos->next;
pos->next = next->next;
free(next);
}
打印
//打印
void SListPrint(const Node*head){
for (Node* cur = head; cur; cur = cur->next){
printf("%d->", cur->value);
}
printf("NULL\n");
}
查找
Node* SListFind(const Node* head, int v){
for (Node* cur = head; cur; cur->next){
if (cur->value == v){
return cur;
}
}
return NULL;
}
修改
void SListModify(Node* node, int v){
node->value = v;
}
双向无头循环链表
定义
typedef struct DLNode{
int value;
struct DLNode* prev;
struct DLNode* next;
}DLNode;
typedef struct DList{
DLNode* head;
}DList;
初始化
//创建单个结点
DLNode * DListNode(int value)
{
DLNode*node = (DLNode*)malloc(sizeof(DLNode));
node->value = value;
node->next = node->prev = NULL;
return node;
}
//初始化
void DListInit(DList* list){
assert(list);
list->head = DListNode(0);
list->head->next = list->head;
list->head->prev = list->head;
}
销毁
void DListDestroy(DList* list){
DLNode* go, *node;
go = list->head->next;
while (go != list->head)
{
node = go;
go = go->next;
free(node);
}
list->head->next = list->head->prev = list->head;
free(list->head);
list->head = NULL;
}
插入
//头插
void DListPushFront(DList* list, int v){
assert(list);
DLNode* node=DListNode(v);
node->prev = list->head;
node->next = list->head->next;
list->head->next->prev = node;
list->head->next = node;
}
//尾插
void DListPushBack(DList* list, int v){
assert(list);
DLNode* node = DListNode(v);
node->prev =list-> head->prev;
node->next = list->head;
list->head->prev->next = node;
list->head->prev = node;
}
//指定位置前插入
void DListInsert(DLNode* pos, int v){
DLNode* node = DListNode(v);
node->prev = pos->prev;
node->next = pos;
pos->prev = node;
node->prev->next = node;
}
删除
//头删
void DListPopFront(DList* list){
assert(list);
DLNode* cur = list->head->next;
list->head->next = cur->next;
cur->next->prev = list->head;
free(cur);
}
//尾删
void DListPopBack(DList* list){
assert(list);
DLNode* cur = list->head->prev;
list->head->prev = cur->prev;
cur->prev->next = list->head;
free(cur);
}
//指定位置删除
void DListDelete(DLNode* pos){
DLNode* cur = pos;
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
free(cur);
}
打印
void DListPrint(DList* list){
for (DLNode* cur = list->head->next; cur != list->head; cur = cur->next){
printf("%d->", cur->value);
}
printf("\n");
}
查找
void DListFind(DList* list,int v){
DLNode* cur;
for (cur = list->head->next; cur != list->head; cur = cur->next){
if (cur->value == v){
return cur;
}
}
return NULL;
}
修改
void DListModify(DLNode* node, int v){
node->value = v;
}