一个可以增删改查插入的单链表(一)

    晚上到了,还是在被催作业,那我们就开始上传一个可以增删改查指定位置插入指定位置删除的单链表吧,前提:有头结点,无环

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值