数据结构基础--链表

Posted 你快看看我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构基础--链表相关的知识,希望对你有一定的参考价值。

什么是链表

链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
无头+单向+非循环链表增删查改实现

链表的初始化

typedef int SLTDateType;
typedef struct SListNode

	SLTDateType data;
	struct SListNode* next;
SListNode;

动态申请一个新的节点

SListNode* BuySListNode(SLTDateType x)// 动态申请一个节点

	SListNode* node = (SListNode*)malloc(sizeof(SListNode));
	node->data = x;
	node->next = NULL;
	return node;

打印单链表

void SListPrint(SListNode* plist)

	SListNode* cur = plist;
	while (cur != NULL)
	
		printf("%d->", cur->data);
		cur = cur->next;
	
	printf("NULL");

尾插

void SListPushBack(SListNode** pplist, SLTDateType x)

	SListNode* newnode = BuySListNode(x);

	if (*pplist == NULL)
	
		*pplist = newnode;
	
	else
	
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		
			tail = tail->next;
		
		tail->next = newnode;
	

头插

void SListPushFront(SListNode** pplist, SLTDateType x)

	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;

尾删

void SListPopBack(SListNode** pplist)

	if (*pplist == NULL)//plist为空指针的时候
	
		return;
	
	else if ((*pplist)->next == NULL)//plist只有一个结点的是时候
	
		free(*pplist);
		*pplist = NULL;
	
	else
	
		SListNode* prve = NULL;
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		
			prve = tail;
			tail = tail->next;
		
		free(tail);
		tail = NULL;

		prve->next = NULL;
	

头删

void SListPopFront(SListNode** pplist)

	
		if (*pplist == NULL)
		
			return;
		
		else
		
			SListNode* next = (*pplist)->next;
			free(*pplist);

			*pplist = next;
		
	

链表的查找

SListNode* SListFind(SListNode* plist, SLTDateType x)

	
		SListNode* cur = plist;
		//while (cur != NULL)
		while (cur)
		
			if (cur->data == x)
			
				return cur;
			

			cur = cur->next;
		

		return NULL;
	

在pos位置之后插入x

void SListInsertAfter(SListNode* pos, SLTDateType x)

	
		assert(pos);

		SListNode* newnode = BuySLTNode(x);
		newnode->next = pos->next;
		pos->next = newnode;
	

在pos位置之前插入x

void SListInsertBefore(SListNode** pplist, SListNode* pos, SLTDateType x)

	assert(pos);
	SListNode* newnode = BuySListNode(x);
	if (pos == *pplist)
	
		newnode->next = pos;
		*pplist = newnode;
	
	else
	
		SListNode* prev = NULL;
		SListNode* cur = *pplist;
		while (cur == pos)
		
			prev = cur;
			cur = cur->next;
		
		prev->next = newnode;
		newnode->next = pos;
	

删除pos位置后面的值

void SListEraseAfter(SListNode* pos)

		assert(pos);
		if (pos->next == NULL)
		
			return;
		
		else
		
			SListNode* next = pos->next;
			pos->next = next->next;
			free(next);
		

以上是关于数据结构基础--链表的主要内容,如果未能解决你的问题,请参考以下文章

数据结构--;链表_01

链表是啥!那个编程语言中有的,和数组有啥区别

单链表

链表

日常学习随笔-用链表的形式实现普通二叉树的新增查找遍历(前中后序)等基础功能(侧重源码+说明)

链表(转)