C++鏈表類的封裝詳情介紹
更新時間:2022年04月27日 10:30:18 作者:學(xué)習(xí)要充足
這篇文章主要介紹了C++鏈表類的封裝,文章基于C++的相關(guān)資料展開主題的詳細(xì)內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
1.CList.h
#ifndef CLIST_H #define CLIST_H ? class CNode ? ? ? ? //節(jié)點類 { public: ?? ?CNode(); ? ? ~CNode(); ?? ?void *data; ? ? //數(shù)據(jù)域 ?節(jié)點數(shù)據(jù)的地址 ?? ?CNode *pnext; ? //指針域 ?保存下一個節(jié)點的地址 protected: private: }; ? class CList ? ? ? ? //鏈表類 { public: ?? ?CList(); ?? ?~CList(); ?? ?void addList(void *data); ? ? ? ? ? ? ? ? ?//在尾部添加節(jié)點 ?? ?int getListCount(); ? ? ? ? ? ? ? ? ? ? ? ?//獲取節(jié)點的個數(shù) ?? ?int insertListByPos(int pos,void *data); ? //根據(jù)pos插入節(jié)點 ?? ?int deleteListByPos(int pos); ? ? ? ? ? ? ?//刪除節(jié)點 ?? ?void *getNodeByPos(int pos); ? ? ? ? ? ? ? //獲取節(jié)點數(shù)據(jù) ?? ?void *freeList(); ? ? ? ? ? ? ? ? ? ? ? ? ?//釋放鏈表 protected: private: ?? ?CNode *head; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //鏈表頭 ?? ?int count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //節(jié)點個數(shù) }; ? #endif
2.CList.cpp
#include"CList.h" #include<stdio.h> #include<cstring>//memset頭文件 ? CNode::CNode() { ?? ?this->data = NULL; ?? ?this->pnext = NULL; } ? CNode::~CNode() { } ? CList::CList() { ?? ?this->head = new CNode; ?? ?this->count = 0; } ? CList::~CList() { } ? //在尾部添加節(jié)點 void CList::addList(void *data) { ?? ?CNode *tmp = this->head; ?? ?while(tmp->pnext!=NULL) ?? ?{ ?? ??? ?tmp = tmp->pnext;?? ? ?? ?} ?? ?CNode *newNode = new CNode;//創(chuàng)建新節(jié)點 ?? ?tmp->pnext = newNode; ?? ?newNode->data = data; ? ? ++(this->count); } ? //獲取節(jié)點的個數(shù) int CList::getListCount() { ?? ?return this->count; } ? //根據(jù)pos插入節(jié)點 int CList::insertListByPos(int pos,void *data) { ?? ?int num = 0; ?? ?CNode* tmp = this->head; ?? ?while(tmp->pnext!=NULL) ?? ?{ ?? ??? ?count++; ?? ??? ?tmp = tmp->pnext; ?? ??? ?if(pos == count) ?? ??? ?{ ?? ??? ??? ?CNode* newNode = new CNode; ?//新節(jié)點 ?? ??? ??? ?memset(newNode,'\0',sizeof(CNode)); ?? ??? ??? ?newNode->data = data; ?? ??? ??? ?newNode->pnext = tmp->pnext; ?? ??? ??? ?tmp->pnext = newNode; ?? ??? ??? ?return 1; ?? ??? ?} ?? ?} ?? ?return 0; } ? //刪除節(jié)點 int CList::deleteListByPos(int pos) { ?? ?int count = 0; ?? ?CNode* tmp = head->pnext,*pre = head; ?? ?while(tmp!=NULL) ?? ?{ ?? ??? ?count++; ?? ??? ?if(count == pos) ?? ??? ?{ ?? ??? ??? ?pre->pnext = tmp->pnext; ?? ??? ??? ?//tmp數(shù)據(jù)域釋放掉 ?? ??? ??? ?delete tmp->data; ?? ??? ??? ?delete tmp; ?? ??? ??? ?return 1; ?? ??? ?} ?? ??? ?pre = pre->pnext; ?? ??? ?tmp = tmp->pnext; ?? ?} ?? ?return -1; } ? //獲取節(jié)點數(shù)據(jù) void* CList::getNodeByPos(int pos) { ?? ?int count = 0; ?? ?CNode* tmp = head; ?? ?while(tmp->pnext!=NULL) ?? ?{ ?? ??? ?count++; ?? ??? ?tmp = tmp->pnext; ?? ??? ?if(pos == count) ?? ??? ?{ ?? ??? ??? ?return tmp->data;?? ? ?? ??? ?} ?? ?} ?? ?return NULL; } ? //釋放鏈表 void* CList::freeList() { ?? ?CNode* tmp = head; ?? ?while(tmp!=NULL) ?? ?{ ?? ??? ?head = head->pnext; ?? ??? ?delete tmp->data; ?? ??? ?delete tmp; ?? ??? ?tmp = head;?? ? ?? ?} ?? ?return this->head; }
3.main.cpp
計算總節(jié)點數(shù):
#include<iostream> using namespace std; #include"CTools.h" #include "CLabel.h" #include"CEdit.h" #include"CButton.h" #include"CtrBase.h" #include"CLogin.h" ? ? ?//顯示登錄窗口 #include"CIndexWin.h" ? //管理員主界面窗口 #include"CManagerWin.h" //經(jīng)理主界面窗口 #include"CWaiterWin.h" ?//服務(wù)員主界面窗口 #include<stdlib.h> #include"CList.h" ? int main() { ?? ?CLoginWin *login = new CLoginWin(12,5,30,20); ? ? CIndexWin *index = new CIndexWin(3,3,25,23); ?? ?CManagerWin *manager = new CManagerWin(3,3,25,23); ?? ?CWaiterWin *waiter = new CWaiterWin(3,3,25,30);?? ? ? ?? ?CList *myList = new CList; ?? ?myList->addList(login); ?? ?myList->addList(index); ?? ?myList->addList(manager); ?? ?myList->addList(waiter); ?? ?cout<<myList->getListCount()<<endl;//4 ?? ?return 0; }
到此這篇關(guān)于C++鏈表類的封裝詳情介紹的文章就介紹到這了,更多相關(guān)C++鏈表類封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)inline hook的原理及應(yīng)用實例
這篇文章主要介紹了C++實現(xiàn)inline hook的原理及應(yīng)用,需要的朋友可以參考下2014-08-08解決Microsoft?Visual?C++?2010?Express?運行及調(diào)試問題
這篇文章主要介紹了解決Microsoft?Visual?C++?2010?Express?運行及調(diào)試問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01C語言編程技巧 關(guān)于const和#define的區(qū)別心得
盡量用const和inline而不用#define 這個條款最好稱為:“盡量用編譯器而不用預(yù)處理”,因為#define經(jīng)常被認(rèn)為好象不是語言本身的一部分。這是問題之一。再看下面的語句:2013-02-02