单链表C语言实现

          单链表相比于顺序表来说,优点在于头部,中间插入比较高效,但操作较为复杂,空间利用率低,并且CPU顺序表比链表缓存效率高。下面来看代码实现:

#include<stdio.h>
#include<assert.h>
#include<malloc.h>

typedef int DataType;

typedef struct Node
{
	DataType data;
	struct Node* next;
}*PNode,Node;

//初始化单链表
void InitList(PNode* pHead)
{
	*pHead = NULL;
}

//创建一个节点
static PNode NewNode(DataType data)
{
	PNode newNode = (PNode)malloc(sizeof(Node));
	if (newNode)
	{
		newNode->data = data;
		newNode->next = NULL;
	}
	return newNode;
}

void PushBack(PNode* pHead, DataType data)
{
	PNode pcur = NULL;
	assert(pHead);

	if (*pHead == NULL)
	{
		*pHead = NewNode(data);
	}
	else
	{
		pcur = *pHead;
		while (pcur->next)
		{
			pcur = pcur->next;
		}
		pcur->next = NewNode(data);
	}
}

void PopBack(PNode* pHead)
{
	PNode pcur = *pHead;
	PNode pdel = NULL;
	assert(pHead);

	while (pcur->next->next)
	{
		pcur = pcur->next;
	}
	pdel = pcur->next;
	pcur->next = NULL;
	free(pdel);
}

void PushFront(PNode* pHead, DataType data)
{
	PNode pcur = *pHead;
	assert(pHead);

	*pHead = NewNode(data);
	(*pHead)->next = pcur;
}

void PopFront(PNode* pHead)
{
	PNode pcur = *pHead;
	assert(pHead);

	if (*pHead == NULL)
	{
		return;
	}

	*pHead = pcur->next;
	free(pcur);
}

PNode Find(PNode pHead, DataType data)
{
	PNode pcur = pHead;
	assert(pHead);

	while (pcur)
	{
		if (pcur->data == data)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}

void Insert(PNode pos, DataType data)
{
	PNode pnew = NULL;
	PNode pcur = pos->next;
	if (pos == NULL)
	{
		return;
	}

	pnew = NewNode(data);
	pos->next = pnew;
	pnew->next = pcur;
}

void Erase(PNode* pHead, PNode pos)
{
	PNode pcur = *pHead;
	assert(pHead);

	if (pos == NULL)
	{
		return;
	}

	while (pcur->next != pos)
	{
		pcur = pcur->next;
	}
	pcur->next = pos->next;
	free(pos);
}

void Remove(PNode* pHead, DataType data)
{
	PNode pos = NULL;
	assert(pHead);

	if (*pHead == NULL)
	{
		return;
	}

	else if ((pos = Find(*pHead, data)) == NULL)
	{
		return;
	}
	else
	{
		Erase(pHead, pos);
	}
}

void RemoveAll(PNode* pHead, DataType data)
{
	PNode ppre = NULL;
	PNode pcur = NULL;
	PNode pdel = NULL;
	assert(pHead);

	if (*pHead == NULL)
	{
		return;
	}
	else
	{
		ppre = *pHead;
		pcur = ppre->next;
		while (pcur)
		{
			if (pcur->data == data)
			{
				ppre->next = pcur->next;
				free(pcur);
				pcur = ppre->next;
			}
			else
			{
				ppre = pcur;
				pcur = pcur->next;
			}
		}
		if ((*pHead)->data == data)
		{
			pdel = *pHead;
			*pHead = pdel->next;
			free(pdel);
		}
	}
}

size_t Size(PNode pHead)
{
	PNode pcur = pHead;
	size_t count = 0;
	while (pcur)
	{
		pcur = pcur->next;
		count++;
	}
	return count;
}

PNode Front(PNode pHead)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	else
	{
		return pHead;
	}
}

PNode Back(PNode pHead)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	else
	{
		PNode pcur = pHead;
		while (pcur->next)
		{
			pcur = pcur->next;
		}
		return pcur;
	}
}

int Empty(PNode pHead)
{
	if (pHead == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void PrintList(PNode pHead)
{
	PNode pcur = pHead;
	assert(pHead);

	while (pcur)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}

	printf("NULL\n");
}

void PrintListFromTail2Head(PNode pHead)
{

	if (pHead)
	{
		PNode pcur = pHead;
		pHead = pHead->next;
		PrintListFromTail2Head(pHead);
		printf("<-%d", pcur->data);
	}
	else
	{
		printf("NULL");
	}
}

void DeleteNotTailNode(PNode pos)
{
	PNode pcur = NULL;
	if (pos == NULL)
	{
		return;
	}

	pos->data = pos->next->data;
	pcur = pos->next;
	pos->next = pcur->next;
	free(pcur);
}

void InsertNotHeadNode(PNode pos, DataType data)
{
	PNode pnew = NULL;
	if (pos == NULL)
	{
		return;
	}

	pnew = NewNode(data);
	pnew->next = pos->next;
	pos->next = pnew;
	(pos->data) ^= (pnew->data);
	(pnew->data) ^= (pos->data);
	(pos->data) ^= (pnew->data);
}

int main()
{
	return  0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值