数据结构-单链表的各种操作08

单链表的创建、增加、删除、倒转

//链表
typedef struct Node
{
	int data;
	struct Node *next;
}TyLIST;

在这里插入图片描述

//1.创建链表
TyLIST *CreateList()
{
	TyLIST *m_pHead = (TyLIST *)malloc(sizeof(TyLIST));
	m_pHead->data = 0;
	m_pHead->next = nullptr;

	int m_nvalue = 0;
	printf("请输入数字(-1:退出):");
	scanf("%d",&m_nvalue);
	TyLIST *m_pCur = m_pHead;
	while (m_nvalue != -1)
	{
		TyLIST *m_pM = (TyLIST *)malloc(sizeof(TyLIST));
		m_pM->data = m_nvalue;
		m_pM->next = nullptr;

		m_pCur->next = m_pM;

		printf("请输入数字(-1:退出):");
		scanf("%d", &m_nvalue);
		m_pCur = m_pM;
	}

	return m_pHead;
}

在这里插入图片描述

//2.打印链表
void PrintList(TyLIST *m_Phead)
{
	printf("--------------------------\n");
	TyLIST *m_pCur = m_Phead->next;
	while (m_pCur)
	{
		printf("%d ",m_pCur->data);
		m_pCur = m_pCur->next;
	}
	printf("\n--------------------------\n");

}

//3.销毁链表
void DestoryList(TyLIST *m_Phead)
{
	TyLIST *temp = nullptr;
	while (m_Phead)
	{
		//缓存后继结点位置
		temp = m_Phead->next;
		free(m_Phead);
		m_Phead = nullptr;
		//指针后移
		m_Phead = temp;

	}
}

在这里插入图片描述

//4.按值插入元素,在某一元素前
void InSertNode(TyLIST *m_Phead, int m_x, int m_y)
{
	if (!m_Phead)
	{
		return;
	}
	TyLIST *m_pCur = m_Phead->next;
	TyLIST *m_pPre = m_Phead;
	while (m_pCur)
	{
		if (m_x == m_pCur->data)
		{
			break;
		}
		m_pPre = m_pCur;
		m_pCur = m_pCur->next;
	}

	TyLIST *m_pNew = (TyLIST *)malloc(sizeof(TyLIST ));
	m_pNew->data = m_y;
	m_pNew->next = m_pCur;
	m_pPre->next = m_pNew;

}

在这里插入图片描述

//5.按值删除元素
void DelNodeByValue(TyLIST *m_pHead, int m_nvalue)
{
	TyLIST *m_pCur = m_pHead->next;
	TyLIST *m_pPre = m_pHead;
	while (m_pCur->next)
	{
		if (m_nvalue == m_pCur->data)
		{
			break;
		}
		m_pPre = m_pCur;
		m_pCur = m_pCur->next;

	}

	m_pPre->next = m_pCur->next;
	free(m_pCur);
	m_pCur = NULL;
}

在这里插入图片描述

//6.链表逆置
void ReverseList(TyLIST *m_phead)
{
	
	if (nullptr == m_phead || nullptr == m_phead->next || nullptr == m_phead->next->next)
	{
		return;
	}

	TyLIST *m_pPre = m_phead->next;
	TyLIST *m_pCur = m_phead->next->next;
	TyLIST *m_next = nullptr;
	while (m_pCur)
	{
		//1.缓存下一节点
		m_next = m_pCur->next;
		//2.重新连接
		m_pCur->next = m_pPre;
		//3.m_pPre,m_pCur下移
		m_pPre = m_pCur;
		m_pCur = m_next;
	}
	//4.让尾节点为NULL
	m_phead->next->next = nullptr;
	//5.头节点重新指向
	m_phead->next = m_pPre;

}

void main()
{
	TyLIST *m_phead = CreateList();
	PrintList(m_phead);
	

	InSertNode(m_phead,30, 29);
	PrintList(m_phead);

	DelNodeByValue(m_phead, 29);
	PrintList(m_phead);


	ReverseList(m_phead);
	PrintList(m_phead);


	DestoryList(m_phead);
	system("pause");
}

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发如雪-ty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值