单链表及单链表的基本操作实现

头文件

typedef int DataType;
//单链表节点
typedef struct SingleListNode
{
	DataType _data;
	struct SingleListNode* _next;
} SingleListNode;

typedef struct SingleList
{
	SingleListNode* _head;
} SingleList;

void SingleListInit(SingleList* sl);
void SingleListDestroy();

SingleListNode* CreatNode(DataType x);
void SingleListPushBack(SingleList* sl, DataType x);
void SingleListPopBack(SingleList* sl);
void SingleListPushFront(SingleList* sl, DataType x);
void SingleListPopFront(SingleList* sl);

void SingleListInsertAfter(SingleListNode* pos, DataType x);
void SingleEraseAfter(SingleListNode* pos);

void SingleListRemove(SingleList* sl, DataType x);

#include "单链表.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
//无头单向非循环
void SingleListInit(SingleList* sl)
{
	assert(sl);
	sl->_head = NULL;
}
//销毁单链表
void SingleListDestroy(SingleList* sl)
{
	assert(sl);
	SingleListNode* cur = sl->_head;
	while (cur)
	{
		SingleListNode* next = cur->_next;
		free(cur);
		cur = next;
	}
	sl->_head = NULL;
}

//尾插
void SingleListPushBack(SingleList* sl, DataType x)
{
	assert(sl);
	//空链表
	if (sl->_head == NULL)
	{
		sl->_head = CreatNode(x);
	}
	else
	{
		//遍历单链表
		SingleListNode* cur = sl->_head;
		while (cur->_next)
		{
			cur = cur->_next;
		}
		//找到最后一个节点
		cur->_next = CreatNode(x);
	}
}

//创建结点
SingleListNode* CreatNode(DataType x)
{
	SingleListNode* newNode = (SingleListNode*)malloc(sizeof(SingleListNode));
	newNode->_data = x;
	newNode->_next = NULL;
	return newNode;
}

//尾删: O(N)
void SingleListPopBack(SingleList* sl)
{
	assert(sl);
	SingleListNode* cur = sl->_head;
	SingleListNode* prev = NULL;
	if (cur == NULL)
		return;

	while (cur->_next)
	{
		prev = cur;
		cur = cur->_next;
	}

	free(cur);
	cur = NULL;
	if (prev == NULL)
	{
		//删除的为头节点
		sl->_head = NULL;
	}
	else
		prev->_next = NULL;
}
// 头插: O(1)
void SingleListPushFront(SingleList* sl, DataType x)
{
	assert(sl);
	SingleListNode* cur = sl->_head;
	SingleListNode* newNode = CreatNode(x);
	//头插
	newNode->_next = cur;
	sl->_head = newNode;
}

//头删:O(1)
void SingleListPopFront(SingleList* sl)
{
	assert(sl);
	SingleListNode* cur, * next;
	if (sl->_head == NULL)
		return;

	cur = sl->_head;
	next = sl->_head->_next;
	free(cur);
	cur = NULL;
	sl->_head = next;
}
//在指定位置pos之后插入
void SingleListInsertAfter(SingleListNode* pos, DataType x)
{
	assert(pos);
	SingleListNode* newNode = CreatNode(x);
	SingleListNode* next = pos->_next;
	pos->_next = newNode;
	newNode->_next = next;
}
//删除指定位置pos之后的值
void SingleEraseAfter(SingleListNode* pos)
{
	assert(pos);
	SingleListNode* next = pos->_next;
	if (next == NULL)
		return;
	else
	{
		SingleListNode* next2 = next->_next;
		pos->_next = next2;
		free(next);
		next = NULL;
	}
}
//删除x
void SingleListRemove(SingleList* sl, DataType x)
{
	assert(sl);
	SingleListNode* cur = sl->_head;
	SingleListNode* prev = NULL;
	while (cur)
	{
		if (cur->_data == x)
		{
			if (prev == NULL)
			{
				sl->_head = cur->_next;
			}
			else
			{
				prev->_next = cur->_next;
			}
			free(cur);
			cur = NULL;
		}
		else
		{
			prev = cur;
			cur = cur->_next;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值