关于单链表的一些基本操作

1.首先创建一个包含数据和指向该结构体的结构体即链表的节点

#pragma one
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int DataType;
typedef struct ListNode
{
	DataType _data;
	struct ListNode* _next;
}ListNode;<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
2.链表的基本操作函数

//创造一个存储x的新节点
ListNode* BuyNode(DataType x)
{
	ListNode* newpHead = NULL;
	newpHead = (ListNode*)malloc(sizeof(ListNode));
	assert(newpHead);
	newpHead->_data = x;
	newpHead->_next = NULL;//避免newpHead->_next指向某个未知位置
	return newpHead;
}
//打印链表
void PrintListNode(ListNode* pHead)
{
	ListNode* tmp = pHead;
	while (tmp)
	{
		printf("%d->", tmp->_data);
		tmp = tmp->_next;
	}
	printf("NULL\n");
}
//摧毁链表
void DestroyList(ListNode* &pHead)
{
	ListNode* tmp = pHead;
	if (pHead == NULL)
	{
		printf("ListNode is empty\n");
		free(pHead);
		return;
	}
		while (pHead)
		{
			pHead = pHead->_next;
			free(tmp);
			tmp = pHead;
		}
		pHead = tmp;
		free(pHead);
}
//尾部插入一个节点
void PushBack(ListNode*& pHead, DataType x)
{
	if (pHead == NULL)
	{
		pHead = BuyNode(x);
	}
	else
	{
		ListNode *tail = pHead;
     	while (tail->_next != NULL)
		{
			tail = tail->_next;
		}
		tail->_next = BuyNode(x);
	}
}
//尾部删除一个节点
void PopBack(ListNode*& pHead)
{
	if (pHead == NULL)
	{
		printf("List is empty\n");
		return;
	}
	else if (pHead->_next == NULL)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		ListNode* prevTail = NULL, *tail = pHead;
		while (tail->_next != NULL)
		{
			prevTail = tail;
			tail = tail->_next;
		}
		prevTail->_next = NULL;
		free(tail);
	}
 }
//删除头部节点
void PopFront(ListNode* pHead)
{
	if (NULL == pHead->_next)
	{
		pHead = NULL;
	}
	else
		pHead = pHead->_next;
	return;
}
//寻找一个值为x的节点位置
ListNode* Find(ListNode* pHead, DataType x)
{
	ListNode* pos = pHead;
	while (pHead != NULL)
	{
		if (pHead->_data == x)
		{
			return pos;
		}
		else
		{
			pHead = pHead->_next;
			pos = pHead;
		}
	}
	return NULL;
}
//在链表中部的节点后插入新节点
void Insert(ListNode* pos, DataType x)
{
	assert(pos);
	ListNode* newNode = BuyNode(x);
	ListNode* tmp = pos->_next;
	newNode->_next = tmp;
	pos->_next = newNode;
	
}
//删除某个位置的节点
void Erase(ListNode*& pHead, ListNode* pos)
{
	ListNode* tmp = pHead;
	if (pHead == pos)
	{
		pHead = pHead->_next;
		return;
	}
	while (tmp)
	{
		if (tmp->_next == pos)
		{
			tmp->_next = tmp->_next->_next;
			free(pos);
			break;
		}
		tmp = tmp->_next;
	}
}
//删除链表中值为x的节点
void Remove(ListNode*& pHead, DataType x)
{
	ListNode* tmp = Find(pHead, x);
	Erase(pHead, tmp);
}
//逆置节点
ListNode* Reverse1(ListNode* pHead)
{
	ListNode* NewHead = NULL;
	ListNode* tmp = NULL;
	if (!pHead || !pHead->_next)
		return;
	while (pHead)
	{
		NewHead = pHead->_next;
		pHead->_next = tmp;
		tmp = pHead;
		pHead = NewHead;
	}
	return tmp;
}
//逆置节点
void Reverse(ListNode*& pHead)
{
	ListNode* Next = NULL;
	ListNode* Prev = NULL;
	if (!pHead || !pHead->_next)
       return;
	while (pHead)
	{
		Next = pHead->_next;
		pHead->_next = Prev;
		Prev = pHead;
		pHead = Next;
	}
	pHead = Prev;
}//删除无头单链表的非尾节点
void PopNoheadList(ListNode* pos)
{
	if (!pos&&!pos->_next)
		return;
	pos->_data = pos->_next->_data;
	pos->_next = pos->_next->_next;
}
//在无头单链表的一个非头节点插入一个节点
void PushNohead(ListNode* pos, DataType x)
{
	ListNode* NewNode = BuyNode(x);
	DataType tmp = 0;
	if (!pos)
		return;
	NewNode->_next = pos->_next;
	pos->_next = NewNode;
	tmp = NewNode->_data;
	NewNode->_data = pos->_data;
	pos->_data = tmp;
}
//查找单链表的中间节点,要求只能遍历一次
ListNode* FindMidNode(ListNode* pHead)
{
	ListNode* pos = NULL;
	ListNode* Fast = pHead, *Slow = pHead;
	while (Fast->_next!=NULL)
	{
		if (Fast->_next->_next != NULL)
		{
			Slow = Slow->_next;
			Fast = Fast->_next->_next;
		}
		else
			Fast = Fast->_next;
	}
	pos = Slow;
	return pos;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值