单链表的基本接口

SList.h

#ifndef _SLIST_H_
#define _SLIST_H_

#include<stdio.h>
#include<windows.h>


typedef int SLTDataType;

typedef struct SListNode {
	SLTDataType data;
	struct SListNode* next;
}SListNode;

void SListInit(SListNode** pphead);
void SListDestory(SListNode** pphead);

SListNode* BuySListNode(SLTDataType x);
void SListPushFront(SListNode** pphead, SLTDataType x);
void SListPopFront(SListNode** pphead);
void SListInsertAfter(SListNode* pos, SLTDataType x);
void SListEraseAfter(SListNode* pos);
SListNode* SListFind(SListNode* phead, SLTDataType x);
void SListRemove(SListNode** pphead, SLTDataType x);

void SListPrint(SListNode* phead);

void SListReverse(SListNode **pphead);
void SListReverse2(SListNode **pphead);


#endif /*_SLIST_H_*/ //_SLIST_H_

SList.c

#include "SList.h"

SListNode* BuySListNode(SLTDataType x) //创建新的节点
{
	SListNode* res = (SListNode*)malloc(sizeof(SListNode));
	res->data = x;
	res->next = NULL;
	return res;
}

void SListInit(SListNode** pphead) //初始化
{
	*pphead = NULL;
}

void SListDestory(SListNode** pphead)  //销毁
{
	if (*pphead == NULL)
	{
		return;
	}

	while ((*pphead)->next)
	{
		SListEraseAfter(*pphead);
	}
	free(*pphead);
	*pphead = NULL;
}


void SListPushFront(SListNode** pphead, SLTDataType x) //头插
{
	SListNode* tmp = BuySListNode(x);
	tmp->next = *pphead;
	*pphead = tmp;
}

void SListPopFront(SListNode** pphead) //头删
{
	if (*pphead == NULL)
	{
		return;
	}
	SListNode* tmp = (*pphead)->next;
	free(*pphead);
	*pphead = tmp;
}

SListNode* SListFind(SListNode* phead, SLTDataType x) //查找值为x的元素
{
	SListNode* tmp;
	for (tmp = phead; tmp; tmp = tmp->next)
	{
		if (tmp->data == x)
		{
			return tmp;
		}
	}
	return NULL;
}

void SListInsertAfter(SListNode* pos, SLTDataType x) //后插,在第pos个节点后插入值为x的节点
{
	SListNode* tmp = BuySListNode(x);
	tmp->next = pos->next;
	pos->next = tmp;
}

void SListEraseAfter(SListNode* pos) //后删,删除节点pos后的节点
{
	SListNode* tmp = pos->next;
	if (tmp == NULL)
	{
		return;
	}
	pos->next = tmp->next;
	free(tmp);
}


void SListRemoveAll(SListNode** pphead, SLTDataType x) //删除全部值为x的节点
{
	SListNode* tmp;
	while (*pphead && (*pphead)->data == x)
	{
		SListPopFront(pphead);
	}

	for (tmp = *pphead; tmp && tmp->next;)
	{
		if (tmp->next->data == x)
		{
			SListEraseAfter(tmp);
		}
		else
		{
			tmp = tmp->next;
		}
	}
}

void SListRemove(SListNode** pphead, SLTDataType x) //删除第一个值为x的元素
{
	SListNode* tmp;
	if (*pphead == NULL)
	{
		return;
	}

	if ((*pphead)->data == x)
	{
		SListPopFront(pphead);
		return;
	}

	for (tmp = *pphead; tmp->next; tmp = tmp->next)
	{
		if (tmp->next->data == x)
		{
			SListEraseAfter(tmp);
			return;
		}
	}
}

void SListPrint(SListNode* phead)  //输出函数
{
	SListNode* tmp;
	for (tmp = phead; tmp; tmp = tmp->next)
	{
		printf("%d->", tmp->data);
	}
	printf("NULL\n");
}

//实现单链表的逆序
//方法一:尾删头插
void SListReverse(SListNode **pphead)
{
	SListNode *head = *pphead;   //此指针在每次循环中始终指向当前链表的头
	SListNode *tmp = head->next; //此指针在每次循环中始终指向要被后删头插的节点
	SListNode *oldh = *pphead;   //此指针在每次循环中始终指向原本的头结点,不会改变指向

	while (tmp) //如果tmp为空,则代表逆序结束,旧头的next已经是空的了,成为新链表的末尾
	{
		oldh->next = tmp->next; //将tmp架空,实际是后删操作的一部分
		tmp->next = head; //让tmp变成新的头,实际是头插操作的一部分 
		head = tmp; //换头
		tmp = oldh->next; //让tmp变成下次循环中待删除的节点
	}
	*pphead = head;
}

//方法二:头尾转换
void SListReverse2(SListNode **pphead)
{
	SListNode *pre = *pphead;   //被执行操作的前一个节点
	SListNode *cur = pre->next; //被执行操作的节点
	SListNode *next = cur;      //被执行操作的后一个节点,暂时指在cur,在循环开始的时候跳转到其后一个节点

	pre->next = NULL; //此时的头,将会是转换后的尾,这里是在设置链表尾节点
	while (next)
	{
		next = next->next; //先让next变成下一个节点,之所以不放在最后,是因为会有next为空的限制
		cur->next = pre; //让原本指着后面的指到前面来(先后转)
		pre = cur; //为了下次循环而传递数据,这里是设置下次循环的上一个节点(本次执行操作的节点将会成下次循环的上一个节点)
		cur = next; //同上(本次的下一个节点将会成为下次的被执行节点)
	}

	*pphead = pre; //循环跳出后cur和next都已经指向空了,pre才是新的头
}

main.c

#include "SList.h"

int main()
{
	SListNode *phead;

	SListInit(&phead);

	SListPushFront(&phead, 1);
	SListPushFront(&phead, 2);
	SListPushFront(&phead, 3);
	SListPushFront(&phead, 4);
	SListPushFront(&phead, 5);
	SListPushFront(&phead, 6);
	SListPushFront(&phead, 7);
	SListPushFront(&phead, 8);
	SListPushFront(&phead, 9);
	//SListPopFront(&phead);
	//SListPopFront(&phead);
	/*
	SListInsertAfter(SListFind(phead, 6), 9);
	SListEraseAfter(SListFind(phead, 4));
	SListRemove(&phead, 7);
	SListPrint(phead);
	*/
	//SListRemoveAll(&phead, 8);
	SListReverse2(&phead);
	SListPrint(phead);


	SListDestory(&phead);

	//SListPrint(phead);
	system("pause");
	return 0;
}
/*
约瑟夫环的单链表实现

int _main()
{
	SListNode *phead;
	SListNode *plast;
	SListNode *cur;
	int m = 6, n = 5;
	int i;
	SListInit(&phead);

	SListPushFront(&phead, m);
	plast = phead;
	for (i = m - 1; i >= 1; i--)
	{
		SListPushFront(&phead, i);
	}
	plast->next = phead;

	cur = plast;
	for (; m > 1; m--)
	{
		for (i = 1; i < n; i++)
		{
			cur = cur->next;
		}
		SListEraseAfter(cur);
	}

	printf("%d", cur->data);

	free(cur);

	system("pause");
	return 0;
}*/

THE END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值