C语言实现单向不带头非循环链表

1、Data.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLLDatatype;
typedef struct SListNode
{
	SLLDatatype data;
	struct SListNode* next;
}SLLNode;

SLLNode* BuySLLNode(int x);

void SLLPushFront(SLLNode** pphead, int x);

void SLLPushBack(SLLNode** pphead, int x);

void SLLPopFront(SLLNode** pphead);

void SLLPopBack(SLLNode** pphead);

SLLNode* SLLFind(SLLNode* phead, SLLDatatype find);

void SLLInsertAfter(SLLNode** pphead, SLLNode* pos, SLLDatatype x);
//我们希望在pos的前一个链表插入数据
//单向链表的前向插入非常麻烦,我们一般使用后置插入
void SLLInsertBack(SLLNode* pos, SLLDatatype x);

void SLLEraseFront(SLLNode** pphead, SLLNode* pos);

void SLLEraseBack(SLLNode* pos);

void SLLLink(SLLNode** pphead);

void SLLPrint(const SLLNode* phead);

2、main.cpp

#include"Data.h"
int main()
{
	SLLNode* phead = NULL;
	SLLPushBack(&phead, 8);
	SLLPushBack(&phead, 13);
	SLLPushBack(&phead, 55);
	SLLPushBack(&phead, 7);
	SLLPushBack(&phead, 8);
	SLLPushBack(&phead, 13);
	SLLPushBack(&phead, 55);
	SLLPushBack(&phead, 7);
	SLLPrint(phead);

	SLLPopFront(&phead);
	SLLPrint(phead);

	SLLEraseBack(phead->next);
	SLLPrint(phead);
	SLLLink(&phead);
	//形成有环链表
	SLLPrint(phead);
	return 0;
}

3、Data.cpp

//#include"Data.h"
//SLLNode* BuySLLNode(int x)
//{
//	SLLNode* newnode = (SLLNode*)malloc(sizeof(SLLNode));
//	if (newnode == NULL)
//	{
//		perror("newnode fail");
//	}
//	newnode->data = x;
//	newnode->next = NULL;
//	return newnode;
//}
//
//void SLLPushFront(SLLNode** pphead, int x)
//{
//	SLLNode* newnode = BuySLLNode(x);
//	newnode->next = *pphead;
//	*pphead = newnode;
//}
//
//void SLLPushBack(SLLNode** pphead, int x)
//{
//	SLLNode* newnode = BuySLLNode(x);
//	if (*pphead == NULL)
//	{
//		*pphead = newnode;
//	}
//	else
//	{
//		SLLNode* tail = *pphead;
//		while (tail->next)
//		{
//			tail = tail->next;
//		}
//		tail->next = newnode;
//	}
//}
//
//void SLLPopFront(SLLNode** pphead)
//{
//	if (*pphead == NULL)
//	{
//		return;
//	}
//	else
//	{
//		SLLNode* next = (*pphead)->next;
//		free(*pphead);
//		*pphead = next;
//	}
//}
//void SLLPopBack(SLLNode** pphead)
//{
//	if (*pphead == NULL)
//	{
//		return;
//	}
//	else if ((*pphead)->next == NULL)
//	{
//		free(*pphead);
//		*pphead = NULL;
//	}
//	else
//	{
//		SLLNode* tail = *pphead;
//		while (tail->next->next)
//		{
//			tail = tail->next;
//		}
//		free(tail->next);
//		tail->next = NULL;
//	}
//
//}
//
//void SLLPrint(const SLLNode* phead)
//{
//	const SLLNode* cur = phead;
//	while (cur)
//	{
//		printf("%d->", cur->data);
//		cur = cur->next;
//	}
//	printf("NULL\n");
//}


#include"Data.h"
SLLNode* BuySLLNode(int x)
{
	SLLNode* newnode = (SLLNode*)malloc(sizeof(SLLNode));
	if (newnode == NULL)
	{
		perror("newnode fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SLLPushFront(SLLNode** pphead, int x)
{
	assert(pphead);
	SLLNode* newnode = BuySLLNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

void SLLPushBack(SLLNode** pphead, int x)
{	
	assert(pphead);
	SLLNode* newnode = BuySLLNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLLNode* tail = *pphead;
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

void SLLPopFront(SLLNode** pphead)
{
	assert(pphead);
	assert(*pphead);
	/*if (*pphead == NULL)
	{
		return;
	}*/
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLLNode* next = (*pphead)->next;
		free(*pphead);
		*pphead = next;
	}
}


void SLLPopBack(SLLNode** pphead)
{
	assert(pphead);
	assert(*pphead);
	/*if (*pphead == NULL)
	{
		return;
	}*/
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLLNode* tail = *pphead;
		while (tail->next->next)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}

SLLNode* SLLFind(SLLNode* phead, int find)
{
	SLLNode* pfind = phead;
	while (pfind)
	{
		if (pfind->data == find)
		{
			return pfind;
		}
		pfind = pfind->next;
	}
	return NULL;
}

void SLLInsertAfter(SLLNode** pphead, SLLNode* pos, SLLDatatype x)
{
	assert(pphead);

	if (*pphead == pos)
	{
		SLLPushFront(pphead,x);
	}
	else
	{
		SLLNode* prev = *pphead;
		while ((prev->next) != pos)
		{
			prev = prev->next;
		}
		SLLNode* newnode = BuySLLNode(x);
		prev->next = newnode;
		newnode->next = pos;
		//不能写成newnode->next = nownode->next;
		//不然就自己链接自己了(乐)
	}
}

void SLLEraseFront(SLLNode** pphead, SLLNode* pos)
{
	SLLNode* prev = *pphead;
	while (prev->next->next != pos)
	{
		prev = prev->next;
	}
	SLLNode* del = prev->next;
	prev->next = prev->next->next;
	free(del);
}


void SLLEraseBack(SLLNode* pos)
{
	assert(pos);
	assert(pos->next);
	SLLNode* del = pos->next;
	pos->next = pos->next->next;
	free(del);
}

void SLLLink(SLLNode** pphead)
{
	SLLNode* cur = *pphead;
	while (cur->next)
	{
		cur = cur->next;
	}
	cur->next = *pphead;
}

void SLLPrint(const SLLNode* phead)
{
	while (phead)
	{
		printf("%d->", phead->data);
		phead = phead->next;
	}
	printf("NULL\n");
}



































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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值