无头单链表

本文详细介绍了C语言中的单链表结构,包括创建新节点、打印链表、尾插、头插、头删、尾删等基本操作,以及如何查找、插入和删除节点。并提供了一个使用单链表的示例程序。
摘要由CSDN通过智能技术生成

1.单链表的基本结构

链表如其名,是一种像锁链一般的结构,环环相扣。即要保留数组的连续访问,还要解决容量大小的问题。在数组中,可以通过下标去访问数据,那我们就可以通过地址来找到数据。在C语言中,指针可以用来存储地址,指针就可以作为链表的链扣,将数据串联起来。

单链表
typedef int SLDataType;
struct SList 
{
	SLDataType val;
	struct SList* next;
};
typedef struct SList SL;

2.单链表的接口函数

创建新结点

SL* newSList(SLDataType x);

SL* newSList(SLDataType x)
{
	SL* new=(SL*)malloc(sizeof(SL));
	if (new == NULL)
	{
		printf("malloc fail!");
		exit(-1);
	}
	else
	{
		new->val = x, new->next = NULL;
		return new;
	}	
}
打印链表

void SListPrint(SL* phead);

void SListPrint(SL* phead)
{
	assert(phead);
	while (phead->next)
	{
		printf("%d->", phead->val);
		phead = phead->next;
	}
	printf("%d", phead->val);
}
尾插

void SListPushBack(SL** pphead, SLDataType x);

void SListPushBack(SL** pphead, SLDataType x)
{
	SL* new = newSList(x);
	if (*pphead == NULL)
	{
		*pphead = new;
	}
	else
	{
		SL* cur = *pphead;
		while (cur->next)
		{
			cur = cur->next;
		}
		cur->next = new;
	}
}
头插

void SListPushFront(SL** pphead, SLDataType x);

void SListPushFront(SL** pphead, SLDataType x)
{
	SL* new = newSList(x);
	new->next = *pphead;
	*pphead = new;
}
头删

void SListPopFront(SL** pphead);

void SListPopFront(SL** pphead)
{
	if (*pphead == NULL)
		return;
	SL* cur = (*pphead)->next;
	free(*pphead);
	*pphead = cur;
}
尾删

void SListPopBack(SL** pphead);

void SListPopBack(SL** pphead)
{
	if (*pphead == NULL)
		return;
	else 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);
		prev->next = NULL;
	}	
}
查找数据为x的结点

SL* SListFind(SL* phead, SLDataType x);

SL* SListFind(SL* phead, SLDataType x)
{
	while (phead)
	{
		if (phead->val == x)
			return phead;
		phead = phead->next;
	}
	return NULL;
}
 在pos的前面插入x

void SListInsert(SL** phead, SL* pos, SLDataType x);

void SListInsert(SL** pphead, SL* pos, SLDataType x)
{
	if (pos == *pphead)
	{
		SListPushFront(pphead, x);
	}
	else
	{
		SL* cur = *pphead,*new= newSList(x);
		while (cur&&cur->next != pos)
		{
			cur = cur->next;
		}
		if (cur == NULL)
			return;
		new->next = pos;
		cur->next = new;
	}	
}
删除pos位置的值

void SListErase(SL** phead, SL* pos);

void SListErase(SL** pphead, SL* pos)
{
	if (pos == *pphead)
	{
		SListPopFront(pphead);
	}
	else
	{
		SL* cur = *pphead;
		while (cur&& cur->next != pos)
		{
			cur = cur->next;
		}
		if (cur == NULL)
		{
			return;
		}
		cur->next = pos->next;
		free(pos);
		pos = NULL;
	}
}

3.单链表的使用

int main()
{
	SL* sl=NULL;
	SListPushFront(&sl, 1);
	SListPushFront(&sl, 2);
	SListPushFront(&sl, 3);
	SListPushBack(&sl, 4);
	SListPushBack(&sl, 7);
	SListPushBack(&sl, 8); 
	SListPushBack(&sl, 9);
	SListPushBack(&sl, 6);
	SListPushBack(&sl, 5);
	SListPopFront(&sl);
	SListPopBack(&sl);
	SL* pos= SListFind(sl, 9);
	SListInsert(&sl, pos, 10);
	SListErase(&sl, pos);
	SListPrint(sl);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值