无头单向链表接口的实现

typedef int DataType;
//数据
typedef struct ListNode{
	DataType _data;
	struct ListNode* _next;
} ListNode;
//链
typedef struct List{
	struct ListNode* _head;
} List;

实现

#include "List.h"

//初始化
void ListInit(List* plist){
	assert(plist);
	plist->_head = NULL;
}
//销毁
void ListDestory(List* plist){
	assert(plist);
	ListNode* cur = plist->_head;
	ListNode* next = plist->_head;
	if (cur == NULL){
		return;
	}
	while (cur){
		next = next->_next;
		//链表只有一个元素的情况
		if (next == NULL){
			free(cur);
			plist->_head = NULL;
			break;
		}
		free(cur);
		cur = next;
	}
}

//头插
void ListPushFront(List* plist, DataType x){
	assert(plist);
	ListNode* next = plist->_head;
	ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
	plist->_head = newNode;
	newNode->_next = next;
	newNode->_data = x;
}
//尾插
void ListPushBack(List* plist, DataType x){
	assert(plist);
	ListNode* cur = plist->_head;
	ListNode* prev = plist->_head;
	//头为空的情况
	if (plist->_head == NULL){
		plist->_head = (ListNode*)malloc(sizeof(ListNode));
		plist->_head->_data = x;
		plist->_head->_next = NULL;
	}
	//头不为空的情况
	else{
		while (cur){
			prev = cur;
			cur = cur->_next;
		}
		cur = (ListNode*)malloc(sizeof(ListNode));
		prev->_next = cur;
		cur->_data = x;
		cur->_next = NULL;
	}
}
//头删
void ListPopFront(List* plist){
	assert(plist);
	ListNode* cur = plist->_head;
	if (cur == NULL){
		return;
	}
	plist->_head = cur->_next;
	free(cur);
	cur = NULL;
}
//尾删
void ListPopBack(List* plist){
	assert(plist);
	ListNode* cur = plist->_head;
	ListNode* prev = cur;
	//链表为空
	if (cur == NULL){
		return;
	}
	//链表只有一个元素
	else if (cur->_next == NULL){
		free(plist->_head);
		plist->_head = NULL;
		return;
	}
	while (cur->_next){
		prev = cur;
		cur = cur->_next;
	}
	prev->_next = NULL;
	free(cur);
	cur = NULL;
}
//查找
ListNode* ListFind(List* plist, DataType x){
	assert(plist);
	ListNode* cur = plist->_head;
	while (cur){
		if (cur->_data == x){
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

// 在pos的后面进行插入
void ListInsertAfter(ListNode* pos, DataType x){
	assert(pos);
	ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
	ListNode* next = pos->_next;
	pos->_next = newNode;
	newNode->_next = next;
	newNode->_data = x;
}
// 在pos的后面删除
void ListEraseAfter(ListNode* pos){
	assert(pos);
	if (pos == NULL || pos->_next == NULL){
		return;
	}
	//保存下下个位置
	ListNode* next1 = pos->_next;
	ListNode* next2 = pos->_next->_next;
	free(next1);
	next1 = NULL;
	pos->_next = next2;
}
//删除
void ListRemove(List* plist, DataType x){
	assert(plist);
	if (plist->_head == NULL){
		return;
	}
	ListNode* cur = plist->_head;
	ListNode* prev = NULL;
	ListNode* next = cur->_next;

	while (cur){
		if (cur->_data == x){
			//第一个就找到
			if (prev == NULL){
				free(cur);
				cur = NULL;
				plist->_head = next;
				return;
			}
			else{
				prev->_next = next;
				free(cur);
				cur = NULL;
				return;
			}
		}
		prev = cur;
		cur = next;
		if (next == NULL){
			return;
		}
		next = next->_next;
	}
}
//打印
void ListPrint(List* plist){
	assert(plist);
	ListNode* cur = plist->_head;
	while (cur){
		printf("%d - ", cur->_data);
		cur = cur->_next;
	}
}
发布了60 篇原创文章 · 获赞 5 · 访问量 2641

猜你喜欢

转载自blog.csdn.net/qq_44905386/article/details/100883839