C++~无头单链表基本操作(全面版)

来源:互联网 发布:为什么黑客都用python 编辑:程序博客网 时间:2024/06/05 06:38

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

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;}


原创粉丝点击