标签:一个 实现 return 删除节点 更新 复杂 中间 struct int
1 //单向链表节点的数据结构 2 struct SingleListNode 3 { 4 int nData;//当前节点的数据 5 Node* pNext;//指向下一个节点的指针 6 }; 7 8 //双向链表节点的数据结构 9 struct DobuleListNode 10 { 11 int nData;//当前节点的数据 12 Node* pPre;//指向上一个节点的指针 13 Node* pNext;//指向下一个节点的指针 14 };
链表的操作方式主要分为增、删、改、查,下面以单链表为例,分别以最简单的方式介绍基本用法。
首先定义一个单链表数据结构:
1 struct SingleListNode 2 { 3 int nVal; 4 SingleListNode* pNext; 5 SingleListNode():pNext(NULL){} 6 SingleListNode(int nTempValue):nVal(nTempValue),pNext(NULL){} 7 }; 8 9 class SingleList 10 { 11 public: 12 SingleList():m_pHead(NULL),m_nSize(0){} 13 ~SingleList(){} 14 public: 15 SingleListNode* Find(int nIndex); 16 void Insert(int nIndex,SingleListNode* pNode); 17 SingleListNode* Remove(int nIndex); 18 void PrintList(); 19 private: 20 SingleListNode* m_pHead; 21 int m_nSize; 22 };
链表节点的查找不能通过索引快速定位,只能从头节点开始查找。
1 SingleListNode* SingleList::Find(int nIndex) 2 { 3 if(nIndex < 0 || nIndex >= m_nSize) 4 { 5 printf("SingleList::Find:failed! the index is out of range. index is %d \n",nIndex); 6 return NULL; 7 } 8 9 if(NULL == m_pHead) 10 { 11 printf("SingleList::Find:failed! the head node is null. \n"); 12 return NULL; 13 } 14 15 SingleListNode* pRet = m_pHead; 16 for(int i = 0;i < nIndex;i++) 17 { 18 if(pRet) 19 { 20 pRet = pRet->pNext; 21 } 22 } 23 return pRet; 24 }
首先从列表中查找待更新的节点,直接把旧数据替换为新值即可。
删除分为三种情况:
1 SingleListNode* SingleList::Remove(int nIndex) 2 { 3 SingleListNode* pRet = NULL; 4 if(nIndex < 0 || nIndex >= m_nSize) 5 { 6 printf("SingleList::Remove:failed! the index is out of range, size is %d .\n ",m_nSize); 7 return pRet; 8 } 9 10 if(NULL == m_pHead) 11 { 12 printf("SingleList::Remove:failed! the head node is null. \n"); 13 return NULL; 14 } 15 16 if(nIndex == 0) 17 { 18 pRet = m_pHead; 19 m_pHead = m_pHead->pNext; 20 } 21 else 22 { 23 SingleListNode* pPreNode = Find(nIndex - 1); 24 if(pPreNode == NULL) 25 { 26 printf("SingleList::Remove:failed! the pPre node is null.\n "); 27 return pRet; 28 } 29 30 pRet = pPreNode->pNext; 31 if(pRet) 32 { 33 pPreNode->pNext = pRet->pNext; 34 m_nSize--; 35 } 36 } 37 38 return pRet; 39 }
1 void SingleList::Insert(int nIndex,SingleListNode* pNode) 2 { 3 if(NULL == pNode) 4 { 5 printf("SingleList::Insert:failed! the pNode is null.\n "); 6 return; 7 } 8 9 if(nIndex < 0 || nIndex > m_nSize) 10 { 11 printf("SingleList::Insert:failed! the index is out of range, size is %d .\n ",m_nSize); 12 return; 13 } 14 15 if(nIndex == 0) 16 { 17 //first 18 pNode->pNext = m_pHead; 19 m_pHead = pNode; 20 } 21 else 22 { 23 SingleListNode* pPre = Find(nIndex - 1); 24 if(pPre == NULL) 25 { 26 printf("SingleList::Insert:failed! the pPre node is null.\n "); 27 return; 28 } 29 30 pNode->pNext = pPre->pNext; 31 pPre->pNext = pNode; 32 } 33 m_nSize++; 34 }
常见的面试题中考察链表应用的场景有:
标签:一个 实现 return 删除节点 更新 复杂 中间 struct int
原文地址:https://www.cnblogs.com/calence/p/11409837.html