单向链表分析

typedef int SLTDataType;
 
//定义一个链表节点
typedef struct SListNode 
{
	SLTDataType data;    		//数据
	struct SListNode* next;     //指针,指向下一个节点
}SLTNode;


//单链表尾插
void SListPushBack(SLTNode** phead, SLTDataType x) 
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); 
	if (newnode == NULL) 
	{
		printf("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else 
	{
		SLTNode* tail = *phead;
		while (tail->next != NULL) 
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

//单链表的头插
void SListPushFront(SLTNode** phead, SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); 
	if (newnode == NULL) 
	{
		printf("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	
	newnode->next = *phead;
	*phead = newnode;
}

//单链表的尾删
void SListPopBack(SLTNode** phead) 
{
//	assert(*phead != NULL);    //断言,不满足条件会报错
	if ((*phead)->next == NULL) 
	{
		free(*phead);
		*phead = NULL;
	}
	else 
	{
		SLTNode* tail = *phead;
		while (tail->next->next) 
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}

//单链表头删
void SListPopFornt(SLTNode** phead) 
{
//	assert(*phead != NULL);
	SLTNode* next = (*phead)->next;
	free(*phead);
	*phead = next;
}

//打印链表
void SListPrint(SLTNode* phead) 
{
	SLTNode* cur = phead;
	while (cur != NULL) 
	{
		printf("%d-> ", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

//单链表查找
SLTNode* SListFind(SLTNode* phead, SLTDataType x) 
{
	SLTNode* cur = phead;
	while (cur) 
	{
		if (cur->data == x) 
		{
			return cur;
		}
		else 
		{
			cur = cur->next;
		}
	}
	return NULL;
}


//单链表在pos位置之前插入x
void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x) 
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); 
	if (newnode == NULL) 
	{
		printf("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	
	
	if (*phead == pos) 
	{
		newnode->next = *phead;
		*phead = newnode;
	}
	else 
	{
		//找到pos的前一个位置
		SLTNode* posPrev = *phead;
		while (posPrev->next != pos) 
		{
			posPrev = posPrev->next;
		}
		posPrev->next = newnode;
		newnode->next = pos;
	}
}

//单链表删除pos位置的值
void SListErase(SLTNode** phead, SLTNode* pos) 
{
	if (*phead == pos) 
	{
		*phead = pos->next;
		free(pos);
	}
	else 
	{
		SLTNode* prev = *phead;
		while (prev->next != pos) 
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}

测试:

void listTest(void) 
{
	SLTNode* phead = NULL;
	
	printf("尾插:");
	SListPushBack(&phead, 1);
	SListPushBack(&phead, 2);
	SListPushBack(&phead, 3);
	SListPrint(phead);
	
	printf("头插:");
	SListPushFront(&phead, 4);
	SListPushFront(&phead, 5);
	SListPrint(phead);
	
	printf("尾删:");
	SListPopBack(&phead);
	SListPrint(phead);
	
	printf("头删:");
	SListPopFornt(&phead);
	SListPrint(phead);
	
	printf("在1前插入6:");
	SLTNode* find = SListFind(phead, 1);
	SListInsert(&phead, find, 6);
	SListPrint(phead);
	
	printf("删除1:");
	find = SListFind(phead, 1);
	SListErase(&phead, find);
	SListPrint(phead);
}

测试结果:
在这里插入图片描述
参考:
数据结构之——链表
【数据结构】链表
数据结构——链表

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值