最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++鏈表類的封裝詳情介紹

 更新時間:2022年04月27日 10:30:18   作者:學習要充足  
這篇文章主要介紹了C++鏈表類的封裝,文章基于C++的相關(guān)資料展開主題的詳細內(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)優(yōu)酷土豆去視頻廣告的方法

    C++實現(xiàn)優(yōu)酷土豆去視頻廣告的方法

    這篇文章主要介紹了C++實現(xiàn)優(yōu)酷土豆去視頻廣告的方法,實例分析了C++實現(xiàn)屏蔽功能的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C++實現(xiàn)頁面的緩沖區(qū)管理器

    C++實現(xiàn)頁面的緩沖區(qū)管理器

    這篇文章主要介紹了C++實現(xiàn)頁面的緩沖區(qū)管理器,文章圍繞主題展開詳細的內(nèi)容介紹具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • c++11&14-多線程要點匯總

    c++11&14-多線程要點匯總

    這篇文章主要介紹了c++11&14-多線程的使用方法,文中代碼非常詳細,方便大家更好的參考和學習,感興趣的朋友快來了解下
    2020-06-06
  • C++中的最小生成樹算法超詳細教程

    C++中的最小生成樹算法超詳細教程

    這篇文章主要介紹了C++中的最小生成樹算法超詳細教程,最小生成樹的最著名的算法有兩個, 一個是Prim算法, 另一個當然就是Kruskal算法, 接下來, 我將盡我所能的介紹這兩個算法, 也算是對自己學習的一個回顧吧,需要的朋友可以參考下
    2023-08-08
  • C語言中的參數(shù)傳遞機制詳解

    C語言中的參數(shù)傳遞機制詳解

    這篇文章主要介紹了C語言中的參數(shù)傳遞機制,C語言中函數(shù)參數(shù)的傳遞有:值傳遞、地址傳遞、引用傳遞這三種形式。下面我們詳細探討下
    2017-04-04
  • C語言中%c與%s的區(qū)別與劃分詳解

    C語言中%c與%s的區(qū)別與劃分詳解

    這篇文章主要介紹了C語言中%c與%s的區(qū)別與劃分詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C語言、C++內(nèi)存對齊問題詳解

    C語言、C++內(nèi)存對齊問題詳解

    這篇文章主要介紹了C語言、C++內(nèi)存對齊問題詳解,內(nèi)存對齊的問題主要存在于理解struct和union等復合結(jié)構(gòu)在內(nèi)存中的分布,需要的朋友可以參考下
    2014-10-10
  • VS及Unity安裝和使用Nuget包

    VS及Unity安裝和使用Nuget包

    本文主要介紹了VS及Unity安裝和使用Nuget包,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • C/C++詳解如何實現(xiàn)文件備份

    C/C++詳解如何實現(xiàn)文件備份

    這篇文章主要介紹了C/C++詳解如何實現(xiàn)文件備份,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • 解析C++編程中的bad_cast異常

    解析C++編程中的bad_cast異常

    這篇文章主要介紹了C++編程中的bad_cast異常,bad_cast異常通常出現(xiàn)于表達式中類型轉(zhuǎn)換錯誤時等一些場景,需要的朋友可以參考下
    2016-01-01

最新評論

界首市| 微博| 宜川县| 怀来县| 墨竹工卡县| 宣威市| 施甸县| 印江| 双峰县| 乐平市| 报价| 侯马市| 运城市| 明溪县| 威海市| 巴青县| 香河县| 曲阳县| 北票市| 博野县| 个旧市| 时尚| 米林县| 新干县| 大宁县| 巴林左旗| 阳谷县| 大理市| 白玉县| 东莞市| 滨海县| 通化县| 台南县| 贡山| 瑞丽市| 哈尔滨市| 孟连| 岑巩县| 余干县| 和林格尔县| 铁力市|