单链表详细实现

一:创建1.test.c负责测试 2.sqlist.h 负责声明函数,定义结构体,及包含头文件 3.sqlist.c 负责实现函数

准备:

test.c文件

#define _CRT_SECURE_NO_WARNINGS
#include "sqlist.h"
void test()
{

}
int main()
{
	test();
	return 0;
}

sqlist.h文件

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int date;//可实现类型替换
typedef struct SList
{
	date x;
	struct SList* nest;//指向下一个节点
}SL;

void Print(SL* ps);//打印
void Addbehind(SL** pphead, date x);//尾添
SL* Byenode(date x);//申请一个节点
void Addhead(SL** pphead, date x);//头添
void Popbehind(SL** pphead);//尾删
void Pophead(SL** pphead);//头删
SL* SLfind(SL* phead, date x);//查找
void SListInsertAfter(SL* pos,date x);

void SLTInsert(SL** pphead, SL* pos,date x);//在pos的前面插入
// 删除pos位置
void SLTErase(SL** pphead, SL* pos);

void SLTDestroy(SL** pphead);

sqlist.c

#define _CRT_SECURE_NO_WARNINGS
#include "sqlist.h"

二单链表实现

1:void Print(SL* ps)//打印
 

void Print(SL* ps)//打印
{
	assert(ps);
	SL* pcur = ps;
	while (pcur)
	{
		printf("%d->", ps->x);
		pcur = pcur->nest;
	}
	printf("\n");
}

传一级指针即可,因为不需改变指针指向。

2.

SL* Byenode(date x);//申请一个节点

SL* Beynode(date x)
{
	SL* new = (SL*)malloc(sizeof(SL));
	if (new == NULL)
	{
		perror("Beynode()::malloc");
		exit(1);
	}
	new->x = x;
	new->nest = NULL;
	return new;
}

3.

void Addhead(SL** pphead, date x);//头添

void Addbehind(SL** pphead, date x)//尾添
{

	assert(pphead);
	SL* newnode = Beynode(x);
	if (*pphead==NULL)
	{
		*pphead= newnode;
	}
	else
	{
		SL* pcur = *pphead;
		while (pcur->nest)
		{
			pcur = pcur->nest;
		}
		pcur->nest = newnode;
	}
}


4.void Addhead(SL** pphead, date x)//头添

void Addhead(SL** pphead, date x)//头添
{
	assert(pphead && *pphead);
	SL* newnode = Beynode(x);
	newnode->nest = *pphead;
	*pphead = newnode;
}

5.void Popbehind(SL** pphead)//尾删

void Popbehind(SL** pphead)//尾删
{
	assert(pphead&&*pphead);
	SL* ptr = *pphead;
	SL* pcur = (*pphead)->nest;
	
	while (pcur->nest)
	{

		ptr = ptr->nest;
		pcur = pcur->nest;
	}
	free(pcur);
	ptr->nest = NULL;
}

6.void Pophead(SL** pphead);//头删
 

void Pophead(SL** pphead)//头删
{
	assert(*pphead && pphead);
	if ((*pphead)->nest == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SL* pcur = (*pphead)->nest;
		free(*pphead);
		*pphead = NULL;
		*pphead = pcur;
	}
}

7.SL* SLfind(SL* phead, date x)//查找
 

SL* SLfind(SL* phead, date x)//查找
{
	assert(phead);
	SL* pcur = phead;
	while (pcur)
	{
		if (pcur->x == x)
			return pcur;
		pcur = pcur->nest;
	}
	return NULL;
}

8.void SListInsertAfter(SL* pos,date x);
 

void SListInsertAfter(SL* pos, date x)
{
	assert(pos);
	SL* pcur = pos->nest;
	pos->nest = pos->nest->nest;
	free(pcur);
	pcur = NULL;
}

9.void SLTInsert(SL** pphead, SL* pos, date x)//在pos的前面插入
 

void SLTInsert(SL** pphead, SL* pos, date x)//在pos的前面插入
{
	assert(pphead && *pphead);
	assert(pos);
	SL* pcur = *pphead;
	SL* new = Byenode(x);

	if ((*pphead)==pos)
	{
		new->nest = *pphead;
		*pphead = new;
	}
	else
	{
		while (pcur->nest != pos)
			pcur = pcur->nest;
		pcur->nest = new;
		new->nest = pos;
	}
}

10.void SLTErase(SL** pphead, SL* pos)
 

void SLTErase(SL** pphead, SL* pos)
{
	assert(pos);
	assert(*pphead && pphead);
	SL* ptr = *pphead;
	SL* pcur = pos->nest;
	if (*pphead == pos)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else if (pos->nest == NULL)
	{
		while (ptr->nest != pos)
			ptr = ptr->nest;
		ptr->nest = NULL;
		free(pos);
		pos = NULL;
	}
	else 
	{

		while (ptr->nest != pos)
			ptr = ptr->nest;
		ptr->nest = pcur;
		free(pos);
		pos = NULL;
	}
}

11.void SLTDestroy(SL** pphead)

void SLTDestroy(SL** pphead)
{
	assert(pphead && *pphead);
	SL* ptr = (*pphead)->nest;
	SL* pcur = *pphead;
	while (ptr)
	{
		pcur->x = 0;
		free(pcur);
		pcur = NULL;
		pcur = ptr;
		ptr = ptr->nest;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值