单链表各种操作

typedef struct Node
{
	int data;
	Node* next;
}Node,*List;
//链表结构体定义
void InitList(List plist)//单链表初始化
{
	assert(plist!=NULL);
	if(plist==NULL)
	{
		return;
	}
	plist->next=NULL;
}
static Node* GetNode(int val)
{
	Node *pGet=(Node*)malloc(sizeof(Node));
	assert(pGet!=NULL);
	pGet->data=val;
	pGet->next=NULL;
	return pGet;
}
bool Insert_head(List plist, int val)//头插
{
	assert(plist!=NULL);
	if(plist==NULL)
	{
		return false;
	}
	Node* pGet=GetNode(val);
	pGet->next=plist->next;
	plist->next=pGet;
	return true;
}
bool Insert_tail(List plist, int val)//尾插
{
	assert(plist!=NULL);
	if(plist==NULL)
	{
		return false;
	}
	Node* pGet=GetNode(val);
	Node* p=plist;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	p->next=pGet;
	return true;
}
Node* Search_pre(List plist, int key)//查找key的前驱
{
	Node* p = plist;
	for (; p->next != NULL; p = p->next)
	{
		if (p->next->data == key)
		{
			return p;
		}
	}
	return false;
}
bool Delete(List plist, int key)//删除
{
	Node* p = Search_pre(plist, key);
	if (p == NULL)
	{
		return false;
	}
	Node* pDel = p->next;
	p->next = pDel->next;
	free(pDel);
	pDel = NULL;
	return true;
}
bool IsEmpty(List plist)//判空
{
	if (plist->next == NULL)
	{
		return true;
	}
	return false;
}
void Destroy(List plist)//销毁
{
	Node* pDel;
	while (plist->next != NULL)
	{
		pDel = plist->next;
		plist->next = pDel->next;
		free(pDel);
	}
	pDel == NULL;
}
int GetLength(List plist)//长度
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
	int count = 0;
	Node* p = plist;
	while (p->next != NULL)
	{
		p = p->next;
		count++;
	}
	return count;
}
void Reverse(List plist)//逆置
{
	Node* p = plist->next;
	Node* p_pre = NULL;
	Node* p_next = NULL;
	while (p != NULL)
	{
		p_next = p->next;
		p->next = p_pre;
		p_pre = p;
		p = p_next;
	}
	plist->next = p_pre;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值