单链表的基本操作

#单链表头文件

//SList
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define _CRT_SECURE_NO_WARNINGS 1

typedef int DataType;

typedef struct SListNode
{
	DataType data;
	struct Node *next;
} SListNode ;
// 初始化链表 
void SListInit(SListNode** pHead);

// 单链表尾插 
void SListPushBack(SListNode** pHead, DataType data);

// 单链表尾删 
void SListPopBack(SListNode** pHead);

// 单链表头插 
void SListPushFront(SListNode** pHead, DataType data);

// 单链表头删 
void SListPopFront(SListNode** pHead);

// 单链表销毁 
void SListDestroy(SListNode** pHead);

// 单链表中查找值为data的元素,查到后返回该结点,否则返回NULL 
SListNode* SListFind(SListNode* pHead, DataType data);

// 获取单链表最后一个节点 
SListNode* SListBack(SListNode* pHead);
SListNode* BuySListNode(DataType data);

#所需要的操作

// 初始化链表 
SListNode* BuySListNode(DataType Data)
{
	SListNode *cur = (SListNode*)malloc(sizeof(SListNode));
	if (cur == NULL)
	{
		printf(" BuyNode :: malloc failed ! \n");
		return;
	}
	cur->data = Data;
	cur->next = NULL;
	return cur;
}
void SListInit(SListNode** pHead)
{
	assert(pHead);
	(*pHead) = NULL;
}
// 单链表尾插 
void SListPushBack(SListNode** pHead, DataType data)
{
	assert(pHead);
	SListNode *cur = BuySListNode(data);
	if (cur == NULL)
	{
		printf("SListPushBack :: BuySListNode falied ! \n");
		return;
	}
	if ((*pHead) == NULL)
	{
		(*pHead) = cur;
		return;
	}
	else
	{
		SListNode *pcur = (*pHead);
		while (pcur->next != NULL)
		{
			pcur = pcur->next;
		}
		pcur->next = cur;
	}
	return;
}
// 单链表尾删 
void SListPopBack(SListNode** pHead)
{
	assert(pHead);
	if ((*pHead) == NULL)
	{
		printf("单链表为空无法尾删 !\n");
		return;
	}
	SListNode *cur = (*pHead);
	if(cur ->next == NULL)
	{
		free(cur);
		(*pHead) = NULL;
	}
	else
	{
		SListNode *pre = cur;
		cur = cur->next;
		while (cur->next != NULL)
		{
			pre = pre->next;
			cur = cur->next;
		}
		pre->next = NULL;
		free(cur);
		cur = NULL;
	}
}
// 单链表头插 
void SListPushFront(SListNode** pHead, DataType data)
{
	assert(pHead);
	SListNode *cur = BuySListNode(data);
	if (cur == NULL)
	{
		printf("SListPushBack :: BuySListNode falied ! \n");
		return;
	}
	cur->next = (*pHead);
	(*pHead) = cur;
	return;
}
// 单链表头删 
void SListPopFront(SListNode** pHead)
{
	assert(pHead);
	if ((*pHead) == NULL)
	{
		printf("链表为空无法删除 ! \n");
		return;
	}
	else
	{
		SListNode *temp = (*pHead);
		(*pHead) = (*pHead)->next;
		free(temp);
		temp = NULL;
	}
}
void SListDestroy(SListNode** pHead)
{
	assert(pHead);
	SListNode *cur = (*pHead);
	SListNode *tmp = NULL;
	if (cur == NULL)
	{
		return;
	}
	while (cur->next != NULL)
	{
		tmp = cur;
		cur = cur->next;
		free(tmp);
		tmp = NULL;
	}
	(*pHead) = NULL;
	return;
}
SListNode* SListFind(SListNode* pHead, DataType data)
{
	assert(pHead);
	SListNode *cur = pHead;
	if (cur == NULL)
	{
		printf("链表为空 ! \n");
		return NULL;
	}
	while (cur != NULL)
	{
		if (cur->data == data)
		{
			break;
		}
		cur = cur->next;
	}
	return cur;
}
SListNode* SListBack(SListNode* pHead)
{
	assert(pHead);
	if (pHead == NULL)
	{
		printf("链表为空,无法获取队尾元素 !\n");
		return NULL;
	}
	SListNode *cur = pHead;
	if (cur->next == NULL)
	{
		return cur;
	}
	else
	{
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		return cur;
	}
}
```c
#验证操作
`#include "SList.h"
void Print(SListNode *pHead)
{
	SListNode *cur = pHead;
	while (cur != NULL)
	{
		printf(" %d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
int main()
{
	SListNode *pHead;
	SListInit(&pHead);
	SListPushBack(&pHead, 1);
	SListPushBack(&pHead, 2);
	SListPushBack(&pHead, 3);
	Print(pHead);
	SListPopBack(&pHead);
	Print(pHead);
	SListPushFront(&pHead, 1);
	SListPushFront(&pHead, 2);
	SListPushFront(&pHead, 3);
	Print(pHead);
	SListPopFront(&pHead);
	Print(pHead);

	SListNode *pcur = SListFind (pHead , 1);
	printf("pcur = %d \n", pcur->data);
	pcur = SListBack(pHead);
	printf("pcur = %d \n", pcur->data);
	system("pause");
	return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值