数据结构——C语言实现无头非循环链表

**C语言实现无头非循环链表**

链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。 
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。 

函数接口(Singlelist.h)文件:

typedef int SLTDatatype;
typedef struct SListNode
{
    SLTDatatype _data;
    struct SListNode* _next;
}SListNode;
typedef struct SList
{
	struct SListNode* _head;
}SList;
void SListInit(SList* plist);//初始化无头单向非循环链表
void SListDestory(SList* plist);//摧毁无头单向非循环链表
SListNode* BuySListNode(SLTDatatype x);//定义结点
void SListPushBack(SList* plist, SLTDatatype x);//尾插
void SListPopBack(SList* plist);//尾删
void SListPushFront(SList* plist, SLTDatatype x);//头插
void SListPopFront(SList* plist);//头删
SListNode* SListFind(SList* plist, SLTDatatype);
void SListInsertAfter(SListNode* pos, SLTDatatype x);//在pos位的后面进行插入
void SListEraseAfter(SListNode* pos);//在pos位的后面删除
void SListRemove(SList* plist, SLTDatatype x);//删除某个元素
void SListPrint(SList* plist);//打印链表
void SListTest();//测试

函数实现(Singlelist.c)文件:

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include"Singlelist.h"
//无头单向非循环
//初始化单链表
void SListInit(SList* plist)
{
	assert(plist);
	plist->_head = NULL;
}
//销毁单链表
void SListDestory(SList* plist)
{
	assert(plist);
	SListNode* cur = plist->_head;
	while (cur)
	{	
		SListNode* next = cur->_next;
		free(cur);
		cur = next;
	}
	plist->_head = NULL;
}
//定义新结点
SListNode* BuySListNode(SLTDatatype x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	newnode->_data = x;
	newnode->_next = NULL;
	return newnode;
}
//尾插 O(n)
void SListPushBack(SList* plist, SLTDatatype x)
{
	assert(plist);
	//空链表
	if (plist->_head == NULL)
	{
		//SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
		//newnode->_data = x;
		//newnode->_next = NULL;
		plist->_head = BuySListNode(x);
	}
	else
	{	
		//遍历单链表
		SListNode* cur = plist->_head;
		//找到最后一个结点
		while (cur->_next)
		{
			cur = cur->_next;
		}
		//插入
		cur->_next = BuySListNode(x);
	}
}
//尾删 O(n)
void SListPopBack(SList* plist)
{
	assert(plist);
	SListNode* cur = plist->_head;
	SListNode* prev = NULL;
	if (cur == NULL)
	{
		return;
	}
	//找到最后一个结点
	while (cur->_next)
	{
		prev = cur;
		cur = cur->_next;
	}
	//删除
	free(cur);
	cur = NULL;
	//删除的是头结点
	if (prev == NULL)
	{
		plist->_head = NULL;
	}
	else
	{
		prev->_next = NULL;
	}
}
//头插O(1)
void SListPushFront(SList* plist, SLTDatatype x)
{
	assert(plist);
	SListNode* cur = plist->_head;
	SListNode* newnode = BuySListNode(x);
	newnode->_next = cur;
	plist->_head = newnode;
}
//头删O(1)
void SListPopFront(SList* plist)
{
	assert(plist);
	SListNode* cur = NULL;
	SListNode* next = NULL;
	if (plist->_head == NULL)
	{
		return;
	}
	cur = plist->_head;
	next = plist->_head->_next;
	free(cur);
	cur = NULL;
	plist->_head = next;
}
//查找
SListNode* SListFind(SList* plist, SLTDatatype x)
{
	assert(plist);
	SListNode* cur = plist->_head;
	while (cur)
	{
		if (cur->_data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->_next;
		}
	}
	return NULL;
}
void SListInsertAfter(SListNode* pos, SLTDatatype x)
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	SListNode* next = pos->_next;
	pos->_next = newnode;
	newnode->_next = next;

}
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	SListNode* next = pos->_next;
	if (next==NULL)
	{
		return;
	}
	else
	{
		pos->_next = next->_next;
		free(next);
		next = NULL;
	}
}
void SListRemove(SList* plist, SLTDatatype x)
{
	assert(plist);
	SListNode* cur = plist->_head;
	SListNode* prev = NULL;

	while (cur)
	{
		if (cur->_data == x)
		{
			if (prev == NULL)
			{
				plist->_head = cur->_next;
			}
			else
			{
				prev->_next = cur->_next;
			}
			free(cur);
			cur = NULL;
		}
		else
		{
			prev = cur;
			cur = cur->_next;
		}

	}
}
void SListPrint(SList* plist)
{
	SListNode* cur = plist->_head;
	assert(plist);
	if (plist->_head == NULL)
	{
		printf("链表为空\n");
	}
	else
	{
		while (cur)
		{
			printf("%d->", cur->_data);
			cur = cur->_next;
		}
		printf("NULL\n");
	}
}
void SListTest()
{
	SList s1;
	SListInit(&s1);
	SListPushBack(&s1, 1);
	SListPushBack(&s1, 2);
	SListPushBack(&s1, 3);
	SListPushBack(&s1, 4);
	SListPopBack(&s1);
	SListPushFront(&s1, 1);
	SListPushFront(&s1, 2);
	SListPushFront(&s1, 3);
	SListPushFront(&s1, 4);
	SListPopFront(&s1);
	SListInsertAfter(s1._head->_next->_next, 5);
	SListEraseAfter(s1._head->_next);
	SListRemove(&s1, 3);
	
	
	SListPrint(&s1);
}

main函数:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值