链表的实现

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

SListNode* BuySListNode(SLDataType data)
{
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
	if (NULL ==newNode)
	{
		assert(0);//调试宏,参数为0触发,非0不会触发
		return NULL;
	}
	newNode->next = NULL;
	newNode->data = data;
	return newNode;
}
void SListPushBack(SListNode** head, SLDataType data)
{
	assert(head);
	SListNode* newNode = BuySListNode(data);
	//空链表
	if (NULL == *head)
	{
		*head = newNode;
	}
	else
	{
		//找到链表最后一个节点
		SListNode* cur = *head;
		while (cur->next)
		{
			cur = cur->next;//cur++
		}
		//插入新节点
		cur->next = newNode;
	}
}
void SListPopBack(SListNode** head)
{
	assert(head);//检测非法情况
	if (NULL == *head)
	{
		//空链表
		return;
	}
	else
	{
		//链表至少有一个节点
		SListNode* cur = *head;
		SListNode* prev = NULL;
		while (cur->next)
		{
			prev = cur;
			cur = cur->next;
		}
		//最后节点找到,删除节点
		free(cur);
	}
}
void SListPushfront(SListNode** head, SLDataType data)
{
	assert(head);
	SListNode* newNode = BuySListNode(data);
	newNode->next = *head;
	*head = newNode;
	空链表
	//if (NULL == *head)
	//{
	//	*head = newNode;
	//}
	链表有多个节点
	//else
	//{
	//	newNode->next = *head;
	//	*head = newNode;
	//}
}
void SListPopfront(SListNode** head)
{
	assert(head);
	if (NULL == *head)
	{
		return NULL;

		SListNode* delNode = *head;
		*head = delNode->next;
		free(delNode);
	}
}

void SListInsertafter(SListNode* pos, SLDataType data)
{
	if (NULL == pos)
		return;
	SListNode* newNode = BuySListNode(data);
	newNode->next = pos->next;
	pos->next = newNode;
}
void SListEraseafter(SListNode* pos)
{
	if (NULL == pos)
		return;

	SListNode* delNode = pos->next;
	pos->next = delNode->next;
	free(delNode);
}

int  SListNodeSize(SListNode* head)
{

	SListNode* cur = head;
	int count = 0;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}


int SListEmpty(SListNode* head)
{
	return NULL == head;
}

SListNode* SListFind(SListNode* head, SLDataType data)
{
	SListNode* cur = head;
	while (cur)
	{
		if (cur->data == data)
			return cur;

		cur = cur->next;
	}
	return NULL;
}
void SListDestroy(SListNode** head)
{
	assert(head);
	while (*head)
	{
		SListNode* delNode = *head;
		*head = delNode->next;
		free(delNode);
	}
}

void PrintSList(SListNode* head)
{
	SListNode* cur = head;
	while (cur)
	{
		printf("%d--->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值