无头单链表(插入和删除)的基本操作

实现单链表的一些基本操作:链表初始化--〉尾插--〉尾删--〉头插--〉头删--〉查找值为data的结点,返回该结点在链表中的位置--〉在链表pos位置后插入结点data,删除链表pos位置上的结点--〉销毁单链表--〉求链表中结点的个数--〉将链表中的结点清空---〉创建新结点 --〉获取链表中的最后一个结点,返回该结点的地址---等等

下面是一些简单的代码实现(C语言)

test.c: 
#define _CRT_SECURE_NO_WARNINGS 0;
 
 
#include "slist.h"
//测试尾插
void testsListPushBack(pNode *pHead)
{
	sListPushBack(pHead, 0);
	sListPushBack(pHead, 1);
	sListPushBack(pHead, 2);
	sListPushBack(pHead, 3);
	printSList(*pHead);
	printNodeNum(*pHead);
}
//测试尾删
void testSListPopBack(pNode *pHead)
{
	SListPopBack(pHead);
	SListPopBack(pHead);
	SListPopBack(pHead);
	SListPopBack(pHead);
	SListPopBack(pHead);
	printSList(*pHead);
	printNodeNum(*pHead);
}
//测试头插
void testSListPushFront(pNode *pHead)
{
	SListPushFront(pHead, 1);
	printSList(*pHead);
	printNodeNum(*pHead);
}
//测试头删
void testSListPopFront(pNode *pHead)
{
	SListPopFront(pHead);
	printSList(*pHead);
	printNodeNum(*pHead);
}
//测试任意插
void testSListInsert(pNode *pHead)
{
	pNode pCur = *pHead;
	SListInsert(pHead, pCur->_pNext, 6);
 //  SListInsert(pHead, pCur->_pNext->_pNext->_pNext->_pNext, 7);
	printSList(*pHead);
	printNodeNum(*pHead);
}
//测试任意删
void testSListErase(pNode *pHead)
{
	pNode pCur = *pHead;
	SListErase(pHead, pCur->_pNext);
	SListErase(pHead, pCur);
	printSList(*pHead);
	printNodeNum(*pHead);
}
// 销毁单链表
void testSListDestroy(pNode *pHead)
{
	SListDestroy(pHead);
}
// 查找值为data的结点,返回该结点在链表中的位置 
void testSListFind(pNode pHead)
{
	pNode pCur = NULL;
	pCur=SListFind(pHead, 2);
}
// 获取链表中的最后一个结点,返回该结点的地址 
void testSListBack(pNode pHead)
{
	pNode pCur = NULL;
	pCur=SListBack(pHead);
}
int main()
{
	pNode pHead;
	sListInit(&pHead);
	testsListPushBack(&pHead);
	/*testSListPopBack(&pHead);
	testSListPushFront(&pHead);
	testSListPopFront(&pHead);
	testSListInsert(&pHead);
	testSListErase(&pHead);
	testSListDestroy(&pHead);*/
	testSListFind(pHead);
	testSListBack(pHead);
	system("pause");
	return 0;
}


slist.c 
 
 
#define _CRT_SECURE_NO_WARNINGS 0;
#include "slist.h"

//初始化
void sListInit(pNode *pHead)
{
	assert(pHead);
	*pHead = NULL;
}
//创建新结点
pNode CreateNewNode( DataType data)
{
	pNode pNewNode = (pNode)malloc(sizeof(Node));
	if (NULL == pNewNode)
		return NULL;
		pNewNode->_data = data;
		pNewNode->_pNext = NULL;
	return pNewNode;
}
//尾插
void sListPushBack(pNode *pHead, DataType data)
{
	assert(pHead);
	pNode pNewNode = NULL;
	pNewNode = CreateNewNode(data);
	if (NULL == pNewNode)
	{
		return;
	}

	if (NULL==*pHead)   	//空链表
	{
		*pHead = pNewNode;
	}
	else                  	//非空链表
	{
		pNode pcur = *pHead;
		while (pcur->_pNext)
		{
			pcur = pcur->_pNext;
		}
		pcur->_pNext = pNewNode;
	}
}

//打印单链表
void printSList(pNode pHead)
{
	pNode pcur = NULL;
	if (NULL == pHead)
	{
		return;
	}
	pcur = pHead;
	while (pcur)
	{
		printf("%d-->", pcur->_data);
		pcur = pcur->_pNext;
	}
	printf("NULL\n");
}
// 求链表中结点的个数 
int SListSize(pNode pHead)
{
	int count = 0;
	pNode pcur = NULL;
	if (NULL == pHead)
		return 0;

	pcur= pHead;
	while (pcur)
	{
		count++;
		pcur = pcur->_pNext;
	}
	return count;
}

//打印结点个数
void  printNodeNum(pNode pHead)
{
	printf("结点的个数是-->%d\n", SListSize(pHead));
}

//尾删
void SListPopBack(pNode *pHead)
{
	assert(pHead);
	pNode pcur = NULL;
	if (NULL == *pHead)  	//空链表
		return;

	else if ((*pHead)->_pNext == NULL)  //一个结点
	{
		free(*pHead);        //释放空间,赋值为空
		*pHead = NULL;
	}
	else     	            //多结点
	{
		pcur = *pHead;
		while (pcur->_pNext->_pNext)
		{
			pcur = pcur->_pNext;
		}
		free(pcur->_pNext);
		pcur->_pNext= NULL;
	}
}


//头插
void SListPushFront(pNode *pHead, DataType data)
{
	assert(pHead);
	pNode pNewNode = CreateNewNode(data);
	if (pNewNode == NULL)
		return;

	pNewNode->_pNext = *pHead;
	*pHead = pNewNode;
}
//头删
void SListPopFront(pNode *pHead)
{
	assert(pHead);     //判断链表是否存在
	pNode pCur = *pHead;
	if (NULL == *pHead)
	{
		return;
	}
	*pHead = pCur->_pNext;
	free(pCur);
	pCur = NULL;
}
// 在链表pos位置后插入结点data(任意插) 
void SListInsert(pNode* pHead, pNode pos, DataType data)
{
	assert(pHead);
	pNode pCur = *pHead;
	pNode pNewNode = NULL;
	if (*pHead == NULL || pos == NULL)  	//空链表或pos位置不存在
	{
		return;
	}

	pNewNode = CreateNewNode(data);
	if (NULL==pNewNode)    //判断新结点是否创建成功
		return;

	while (pCur)
	{
		if (pCur == pos)
		{
			pNewNode->_pNext = pos->_pNext;
			pos->_pNext = pNewNode;
			return;
		}
		pCur = pCur->_pNext;
	}
	free(pNewNode);        //插入结点失败,释放结点,赋为空
	pNewNode = NULL;
}
//任意删
void SListErase(pNode *pHead,pNode pos)
{
	assert(pHead);
	pNode pCur = *pHead;
	if (*pHead == NULL)  //判断是否是空链表
	{
		return;
	}
	if (pCur == pos)    //一个结点
	{
		*pHead = pCur->_pNext;
		free(pCur);
		pCur = NULL;
	}
	else             //多个结点
	{
		while (pCur && pCur->_pNext != pos)
		{
			pCur = pCur->_pNext;
		}
		if (pos != NULL)
		{
			pCur->_pNext = pos->_pNext;
			free(pos);
			pos = NULL;
		}
	}
}
// 销毁单链表 
void SListDestroy(pNode *pHead)
{
	SListClear(pHead);

}
// 查找值为data的结点,返回该结点在链表中的位置 
pNode SListFind(pNode pHead, DataType data)
{
	assert(pHead);
	pNode pCur = pHead;
	while (pCur)
	{
		if (pCur->_data == data)
		{
			return pCur;
		}
		pCur = pCur->_pNext;
	}
	return NULL;
}

// 将链表中的结点清空 
void SListClear(pNode* pHead)
{
	assert(pHead);
	pNode pCur = NULL;
	while (*pHead)
	{
		pCur = *pHead;
		*pHead= pCur->_pNext;
		free(pCur);	
	}
}

// 获取链表中的最后一个结点,返回该结点的地址 
pNode SListBack(pNode pHead)
{
	pNode pCur = pHead;
	if (pHead == NULL)
	{
		return NULL;
	}

	while (pCur->_pNext)
	{
		pCur = pCur->_pNext;
	}

	return pCur;
}

 
 
slist.h
 
 
 
 
#define _CRT_SECURE_NO_WARNINGS 0;
#pragma once
typedef int DataType; 
#include <stdio.h>
typedef struct SListNode
{
	struct SListNode* _pNext; //指向链表的的下一个结点
	DataType _data; //当前结点中所保存的元素
}Node,*pNode;
void sListInit(pNode *pHead);
void sListPushBack(pNode *pHead, DataType data);
pNode CreateNode(DataType data);
void printSList(pNode pHead);
void SListPopBack(pNode *pHead);
int SListSize(pNode pHead);
void SListPushFront(pNode *pHead, DataType data);
void SListPopFront(pNode *pHead);
void SListErase(pNode *pHead,pNode pos);
void SListDestroy(pNode *pHead);
pNode SListFind(pNode pHead, DataType data);
void SListInsert(pNode* pHead, pNode pos, DataType data);
void SListClear(pNode* pHead);
pNode SListBack(pNode pHead);






猜你喜欢

转载自blog.csdn.net/eternal_01/article/details/79701043
今日推荐