晚上到了,还是在被催作业,那我们就开始上传一个可以增删改查指定位置插入指定位置删除的单链表吧,前提:有头结点,无环
linklist.h
#pragma once
typedef char LinkNodeType;
typedef struct LinkNode{
LinkNodeType data;
struct LinkNode* next;
}LinkNode;
typedef LinkNode* PLinkNode;
void LinkListInit(PLinkNode*);
void LinkListDestory(PLinkNode*);
void LinkListPushBack(LinkNode** phead,LinkNodeType value);
void LinkListPopBack(LinkNode** phead);
void LinkListPushFront(LinkNode** phead,LinkNodeType value);
void LinkListPopFront(LinkNode** phead);
void LinkListInsert(LinkNode* pos,LinkNodeType value);
void LinkListInsertBefore(LinkNode* pos,LinkNodeType value);
void LinkListInsertBefore2(LinkNode* pos,LinkNodeType value);
void LinkListErase(LinkNode** phead,LinkNode* pos);
void LinkListErase2(LinkNode** phead,LinkNode* pos);
LinkNode* LinkListFind(LinkNode* head,LinkNodeType to_find);
linklist.c
#include<stdio.h>
#include<stdlib.h>
#include<linklist.h>
void linkNode* CreatNode(LinkNodeType value){
LinkNode* new_node = (LinkNode*)malloc(sizeof(LinkNode));
new_node->data = value;
new_node->next = NULL;
return new_node;
}
void DestroyNode(LinkNode* node){
free(node->data);
free(node);
}
void LinkListInit(PLinkNode* node){
*node = NULL;
}
void LinkListDestory(PLinkNode* phead){
(void)phead;
}
void LinkListPushBack(LinkNode** phead,LinkNodeType value){
if(phead == NULL){
return;
}
if(*phead == NULL){
*phead = CreatNode(value);
return;
}
LinkNode* cur = *phead;
while(cur != NULL){
cur = cur->next;
}
LinkNode* new_node = CreatNode(value);
cur->next = new_node;
return;
}
void LinkListPopBack(LinkNode** phead);{
if(phead == NULL){
return;
}
if(*phead == NULL){
return;
}
if((*phead)->next == NULL){
DestoryNode(*phead);
*phead = NULL;
return;
}
LinkNode* cur = *phead;
LinkNode* pre = NULL;
while(cur->next != NULL){
pre = cur;
cur = cur->next;
}
pre->next = NULL;
DestoryNode(cur);
return;
}
void LinkListPushFront(LinkNode** phead,LinkNodeType value){
if(phead == NULL){
return;
}
if(*phead == NULL){
LinkNode* new_node = CreatNode(value);
new_node->next = *phead;//KAN JIANGJIE
}
}
void LinkListPopFront(LinkNode** phead){//toushan
if(phead == NULL){
return;
}
if(*phead == NULL){
return;
}
LinkNode* to_erase = *phead;
*phead = (*phead)->next;
DestroyNode(to_erase);
return;
}
void LinkListInsert(LinkNode* pos,LinkNodeType value){
if(pos == NULL){
return;
}
LinkNode* new_node = CreatNode(value);
new_node->next = pos->next;
pos->next = new_node;
return;
}
void LinkListInsertBefore(LinkNode** phead,LinkNode* pos,LinkNodeType value){
if(phead == NULL || pos == NULL){
return;
}
if(*phead == pos){
LinkListPushFront(phead,value);
return;
}
LinkNode* cur = *phead;
for(;cur != NULL;cur = cur->next){
if(cur->next == pos){
break;
}
if(cur == NULL){
return;
}
LinkListInsert(cur,value);
return;
}
void LinkListInsertBefore2(LinkNode* pos,LinkNodeType value){
if(pos == NULL){
return;
}
LinkListInsert(pos,pos->data);
pos->data = value;
LinkNode* new_node = CreatNode(pos->data);
//new_node->next = pos->next;
//pos->next = new_node;
//pos->data = value;
return;
}
void LinkListErase(LinkNode** phead,LinkNode* pos){
if(phead == NULL || pos == NULL){
return;
}
if(*phead == NULL){
return;
}
LinkNode* cur = *phead;
for(;cur != NULL;cur = cur->next){
if(cur->next == pos){
break;
}
}
if(cur == NULL){
return;
}
cur->next = pos->next;
DestroyNode(pos);
return;
}
void LinkListErase2(LinkNode** phead,LinkNode* pos){
if(phead == NULL || pos == NULL){
return;
}
if(*phead == NULL){
return;
}
if(pos->next == NULL){
//zuihouyigeyuansu,zhinengshiyong weishan
LinkListPopBack(phead);
return;
}
pos->data = pos->next->data;
LinkNode* to_erase = pos->next;
pos->next = to_erase->next;
DestroyNode(to_erase);
return;
}
LinkListFind(LinkNode* head,LinkNodeType to_find){
if(head == NULL){
return;
}
LinkNode* cur = head;
while(cur != NULL){
if(cur->data == to_find){
return cur;
}
cur = cur->next;
}
return NULL;
}
喜欢的话请给我点个赞吧,觉得我的代码有错误或有改进建议请留言或私信我,萌新渴望知识ing