链表基本操作的函数实现。(1)
如下是一個(gè)模擬的鏈表的基本操作,主要有創(chuàng)建鏈表,連接鏈表,打印鏈表,刪除鏈表等,后面有關(guān)鏈表的算法都依賴(lài)這個(gè)文件中函數(shù)實(shí)現(xiàn)。
ListCommon.h
//鏈表的結(jié)構(gòu)
struct ListNode{?? ?int m_nValue;
?? ?ListNode* m_pNext;
};
//通用的操作函數(shù)
ListNode* CreateListNode(int value);
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
void PrintListNode(ListNode* pNode);
void PrintList(ListNode* pHead);
void DestoryList(ListNode* pHead);
//其中的參數(shù)是指向指針的指針,
void AddToTail(ListNode** pHead, int value);
void RemoveNode(ListNode** pHead, int value);
ListCommon.cpp
#include <iostream>
#include <stdlib.h>
#include "ListCommon.h"
using namespace std;
//鏈表的結(jié)構(gòu)
//struct ListNode{
//?? ?int m_nValue;
//?? ?ListNode* m_pNext;
//};
//通用的操作函數(shù)
//創(chuàng)建鏈表節(jié)點(diǎn)
ListNode* CreateListNode(int value){
?? ?ListNode* pNode = new ListNode();
?? ?pNode->m_nValue = value;
?? ?pNode->m_pNext = NULL;
}
//連接兩個(gè)鏈表節(jié)點(diǎn)
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext){
?? ?if(pCurrent == NULL){
?? ??? ?cout << "can not to connect two nodes ! " <<endl;
?? ??? ?exit(1);
?? ?}
?? ?pCurrent->m_pNext = pNext;
}
//打印一個(gè)鏈表節(jié)點(diǎn)
void PrintListNode(ListNode* pNode){
?? ?if(pNode == NULL){
?? ??? ?cout << "The node is NULL !" <<endl;
?? ?}else{
?? ??? ?cout << "The value of node is " << pNode->m_nValue <<endl;
?? ?}
}
//打印整個(gè)鏈表,不要用pHead節(jié)點(diǎn)直接操作,否則操作結(jié)束這個(gè)鏈表就無(wú)法獲取了。
void PrintList(ListNode* pHead){
?? ?cout << "print list begin ---" <<endl;
?? ?ListNode * pNode = pHead;
?? ?while(pNode != NULL){
?? ??? ?cout << pNode->m_nValue<< endl;
?? ??? ?pNode = pNode->m_pNext;
?? ???? if(pNode == pHead){
?? ??? ??? ?break;
?? ??? ?}
??? }
?? ?cout << "print list end " <<endl;
}
//銷(xiāo)毀鏈表
void DestoryList(ListNode* pHead){
?? ?ListNode* pNode = pHead;
?? ?while(pNode != NULL){
?? ??? ?pHead = pHead->m_pNext;
?? ??? ?delete pNode;
?? ??? ?pNode = pHead;
?? ?}
}
//從尾部添加節(jié)點(diǎn),其中的參數(shù)是指向指針的指針,因?yàn)檫@里的pHead值是可能會(huì)變化的。
void AddToTail(ListNode** pHead, int value){
?? ?ListNode* pNew = new ListNode();
?? ?pNew->m_nValue = value;
?? ?pNew->m_pNext = NULL;
//空鏈表,這時(shí)頭指針就被改變了,確切說(shuō)是頭指針指向的內(nèi)容被改變了,也即是說(shuō)這里需要指針指向的內(nèi)容,所以用了指向指針的指針。
?? ?if(*pHead == NULL){
?? ??? ?*pHead = pNew;
?? ?}else{
?? ??? ?ListNode* pNode = *pHead;
?? ??? ?while(pNode != NULL){
?? ??? ??? ?pNode = pNode->m_pNext;
?? ??? ?}?? ?
?? ??? ?pNode->m_pNext = pNew;
?? ?}
}
//刪除節(jié)點(diǎn)
void RemoveNode(ListNode** pHead, int value){
?? ?if(pHead == NULL || *pHead == NULL){
?? ??? ?return;
?? ?}
?? ?
?? ?ListNode* pToBeDeleted = NULL;
?? ?if((*pHead)->m_nValue == value){
?? ??? ?pToBeDeleted = *pHead;
?? ??? ?*pHead = (*pHead)->m_pNext;
?? ?}else{
?? ??? ?ListNode* pNode = *pHead;
?? ??? ?while(pNode->m_pNext !=NULL && pNode->m_pNext->m_nValue !=value){
?? ??? ??? ?pNode = pNode->m_pNext;
?? ??? ?}
?? ??? ?if(pNode->m_pNext !=NULL && pNode->m_pNext->m_nValue == value){
?? ??? ??? ?pToBeDeleted = pNode->m_pNext;
?? ??? ??? ?pNode->m_pNext = pNode->m_pNext->m_pNext;
?? ??? ?}
?? ?}
?? ?if(pToBeDeleted != NULL){
?? ??? ?delete pToBeDeleted;
?? ??? ?pToBeDeleted = NULL;
?? ?}
}
總結(jié)
以上是生活随笔為你收集整理的链表基本操作的函数实现。(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: caj转pdf在线转换器免费,不限制页数
- 下一篇: caj 转 pdf