单链表C语言实现(代码可复制)

头文件代码

 包括基本的头文件和单链表的一些基本函数声明
#pragma once
#include<iostream>
#include<stdlib.h>
#include<assert.h>

typedef int Type;

typedef class SListNode
{
	Type data;
	struct SListNode* next;
}SL;

void SListPrint(SL* ps);
void SListPushBack(SL** pps, Type x);
void SListPushFront(SL** pps,Type x);

void SListPopBack(SL** pps);
void SListPopFront(SL** pps);

SL* SListFind(SL* phead, Type x);
void SListInsert(SL** pphead, SL* pos, Type x);
void SListInsertAfter(SL* pos, Type x);

void SListEraseAfter(SL* pos);
void SListErase(SL** pphead, SL* pos);
void Destroy(SL** pphead);

源文件函数实现部分

此部分包含头文件中所有接口函数的实现

#include"Slist.h"

SL* BuyListNode(Type x)
{
	SL* newnode = (SL*)malloc(sizeof(SL));
	if (newnode == NULL)
	{
		cout << "malloc fail" << endl;
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

void SListPrint(SL* phead)
{
	SL* cur = phead;
	while (cur != NULL)
	{
		cout << cur->data<<" ";
		//printf("%d", cur->data);
		cur = cur->next;
	}
	cout << endl;
}

void SListPushBack(SL** pps,Type x)
{
	SL* newnode = BuyListNode(x);

	if (*pps == NULL)
	{
		*pps = newnode;
	}
	else
	{
		//找到尾节点
		SL* tail = *pps;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}

		tail->next = newnode;
	}
}

void SListPushFront(SL** pphead, Type x)
{
	SL* newnode = BuyListNode(x);

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

void SListPopBack(SL** pphead)
{
	assert(*pphead != NULL);

	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SL* prev = NULL;
		SL* tail = *pphead;
		while (tail->next)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		tail = NULL;
		prev->next = NULL;
	}
}

void SListPopFront(SL** pphead)
{
	assert(*pphead != NULL);
	SL* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}

SL* SListFind(SL* phead, Type x)
{
	SL* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
		return NULL;
	}
}

//在pos位置之前去插入一个节点
void SListInsert(SL** pphead, SL* pos, Type x)
{
	SL* newnode = BuyListNode(x);
	if (*pphead == pos)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		//找到pos的第一个位置
		SL* posPrev = *pphead;
		while (posPrev->next != pos)
		{
			posPrev = posPrev->next;
		}
		posPrev->next = newnode;
		newnode->next = pos;
	}
	
}

//在pos的后面插入
void SListInsertAfter(SL* pos, Type x)
{
	SL* newnode = BuyListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}


void SListErase(SL** pphead, SL* pos)
{
	if (*pphead == pos)
	{
		*pphead = pos->next;
		free(pos);
	}
	else
	{
		SL* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}

		prev->next = pos->next;
		free(pos);
	}
}

void SListEraseAfter(SL* pos)
{
	assert(pos->next);

	SL* next = pos->next;
	pos->next = next->next;
	free(next);
}

void Destroy(SL** pphead) 
{
	assert(pphead);

	free(*pphead);

	SL* cur = *pphead;
	while (cur)
	{
		SL* next = cur->next;
		free(cur);
		cur = next;
	}
}

源文件住函数部分

#include"Slist.h"

void test01()
{
	SL* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);

	SListPrint(plist);

	SListPushFront(&plist, 7);
	SListPushFront(&plist, 6);
	SListPushFront(&plist, 5);
	SListPushFront(&plist, 4);

	SListPopBack(&plist);
	SListPopBack(&plist);
	SListPopBack(&plist);

	SListPrint(plist);
	//根据具体需求调用不同的接口函数
}

int main()
{
	test01();

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值