C++~无头单链表基础操作

单链表是一种数据存储结构,是一种链式存储的线性表,用一组任意的存储单元存放线性表的元素,称存储单元为一个节点。 

1.单链表结构:

typedef int DataType;         //int类型重命名
typedef struct Node
{
    DataType data;
    struct Node* pNext;
}Node, *PNode;           //结构体重命名

2.单链表结构示意图:(值域data + 指向next的节点)


3.单链表基本操作以及详解:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<assert.h>


typedef int DataType;
typedef struct Node
{
	DataType _data;
	struct Node* _pNext;
}Node,*pNode,**ppNode;




//1.初始化链表
void InitNode(pNode pHead)
{
	pHead = NULL;
}


//2.新建节点
pNode BuyNode(DataType data)     
{
	pNode _node = (pNode)malloc(sizeof(pNode));
	if (NULL == _node)
		exit(-1);
	_node->_data = data;
	_node->_pNext = NULL;
	return _node;
}


//3. 在单链表的尾部插入一个节点
void PushBack(ppNode pHead, DataType data)          
{
	assert(pHead);
	if (NULL == *pHead)
	{
		*pHead = BuyNode(data);
	}
	else
	{
		pNode pnode = *pHead;
		while (pnode->_pNext)
		{
			pnode = pnode->_pNext;
		}
		pnode->_pNext = BuyNode(data);
	}
}





//4.单链表的冒泡排序法
pNode BubbleSortNode(pHead)
{
	pNode pTail = NULL;
	pNode pCur = pHead;
	pNode pPreCur = NULL;
	if (pCur == NULL || NULL == pCur->_pNext)
	{
		return pCur;
	}
	while (pCur != pTail)
	{
		DataType temp = pCur->_data;
		pCur->_data = pPreCur->_data;
		pPreCur->_data = temp;
	}
	pPreCur = pCur;
	pCur = pCur->_pNext;
	pTail = pPreCur;
}



//5.查找单链表倒数第K个节点
pNode SearchLestKNode(pNode pHead,  DataType k)
{
	pNode pFast = pHead;
	pNode pSlow = pHead;
	if (pHead == NULL || k <= 0)
	{
		return NULL;
	}
	while (k--)
	{
		if (pFast == NULL)
		{
			return NULL;
		}
		else
		{
			pFast = pFast->_pNext;
		}
	}
	while (pFast != NULL)
	{
		pFast = pFast->_pNext;
		pSlow = pSlow->_pNext;
	}
	return pSlow;
}


//7.删除单链表倒数第k个节点
pNode DelectLestKNode(pNode pHead, DataType k)
{
	pNode pFast = pHead;
	pNode pSlow = pHead;
	if (pHead == NULL || k <= 0)
	{
		return NULL;
	}
	while (k--)
	{
		if (pFast == NULL)
		{
			return NULL;
		}
		else
		{
			pFast = pFast->_pNext;
		}
	}
	while (pFast != NULL)
	{
		pFast = pFast->_pNext;
		pSlow = pSlow->_pNext;
	}
	return pSlow;
}




//6.头插
pNode PushFront(pNode* pHead, DataType data)
{
	assert(pHead);
	if (NULL == *pHead)
	{
		return NULL; 
	}
	else
	{
		pNode pnode = *pHead;
		while (pnode->_pNext)
		{
			pnode = pnode->_pNext;
		}
		pnode->_pNext = BuyNode(data);
	}
	return;
}


//7.头删
pNode DelFront(pNode* pHead, DataType data)
{
	if (pHead == NULL)
	{
		return;
	}
	else if ((*pHead)->_pNext == NULL)
	{
		*pHead = NULL;
	}
	else
	{
		pNode *tmp = (*pHead)->_pNext;
		free(*pHead);
		*pHead = tmp;
	}
}



//8.逆向打印单链表
void PrintBackNode(pNode pHead)     
{
	if (NULL != pHead)
	{
		if (pHead->_pNext != NULL)
		{
			PrintBackNode(pHead->_pNext);
		}
	}
	printf("%d -> ", pHead->_data);
}


//9.打印链表
void PrintNode(pNode pHead)
{
	if (NULL != pHead)
	{
		printf("%d -> ", pHead->_data);
		PrintNode(pHead->_pNext);
	}
}




//10.尾删
pNode PopDel(pNode pHead)
{
	if (pHead == NULL)
	{
		return;
	}
	else if ((pHead)->_pNext == NULL)
	{
		free(pHead);
		pHead = NULL;
	}
	else
	{
		pNode pTail = pHead;
		pNode pPos = pTail;
		while ( pTail->_pNext)
		{
			pPos = pTail;
			pTail = pTail->_pNext;
		}
		free(pTail);
		pTail = NULL;
		pPos->_pNext = NULL;
	}

}



//测试代码
test()
{
	pNode pHead = NULL;
	pNode Data;
	int key = 2;
	//pHead = BuyNode(8);
	PushBack(&pHead, 2);
	PushBack(&pHead, 5);
	PushBack(&pHead, 3);
	PushBack(&pHead, 6);
	PushBack(&pHead, 1);
	PrintNode(pHead);

	printf("\n\n逆向打印:  ");
	PrintBackNode(pHead);

	Data = SearchLestKNode(pHead, key);
	printf("\n倒数第%d个节点值为: %d \n",key, Data->_data);

	printf("冒泡排序后: ");
	BubbleSortNode(pHead);
	PrintNode(pHead);

}


int main(int argc,char* argv[])
{
	test();
	return 0;
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页