欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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++類型兼容規(guī)則詳情

    C++類型兼容規(guī)則詳情

    這篇文章主要介紹了C++類型兼容規(guī)則詳情,共有繼承時,任何需要父類對象的地方,都能使用子類對象“替代”,這就是類型兼容規(guī)則,下面一起來了解文章相關(guān)內(nèi)容吧
    2022-03-03
  • C++實現(xiàn)inline hook的原理及應(yīng)用實例

    C++實現(xiàn)inline hook的原理及應(yīng)用實例

    這篇文章主要介紹了C++實現(xiàn)inline hook的原理及應(yīng)用,需要的朋友可以參考下
    2014-08-08
  • C語言 函數(shù)缺省參數(shù)詳情

    C語言 函數(shù)缺省參數(shù)詳情

    這篇文章主要介紹了C語言 的函數(shù)缺省參數(shù)、除了介紹函數(shù)全缺省參數(shù),和半缺省參數(shù)還簡單介紹了函數(shù)聲明、函數(shù)調(diào)用等一些函數(shù)的定義,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • 詳解C語言中二分查找的運用技巧

    詳解C語言中二分查找的運用技巧

    本文主要介紹了二分查找在實際中的應(yīng)用,通過分析幾個應(yīng)用二分查找的實例,總結(jié)下能使用二分查找算法的一些共同點,感興趣的可以了解一下
    2022-03-03
  • C語言實現(xiàn)簡易版三子棋游戲

    C語言實現(xiàn)簡易版三子棋游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)簡易版三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • QT TCP實現(xiàn)簡單的通信示例

    QT TCP實現(xiàn)簡單的通信示例

    這篇文章主要為大家詳細(xì)介紹了QT TCP簡單的通信示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 解決Microsoft?Visual?C++?2010?Express?運行及調(diào)試問題

    解決Microsoft?Visual?C++?2010?Express?運行及調(diào)試問題

    這篇文章主要介紹了解決Microsoft?Visual?C++?2010?Express?運行及調(diào)試問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • C++實現(xiàn)推箱子游戲

    C++實現(xiàn)推箱子游戲

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • C語言?詳解如何刪除有序數(shù)組中的重復(fù)項

    C語言?詳解如何刪除有序數(shù)組中的重復(fù)項

    數(shù)組不擅長插入(添加)和刪除元素。數(shù)組的優(yōu)點在于它是連續(xù)的,所以查找數(shù)據(jù)速度很快。但這也是它的一個缺點。正因為它是連續(xù)的,所以當(dāng)插入一個元素時,插入點后所有的元素全部都要向后移;而刪除一個元素時,刪除點后所有的元素全部都要向前移
    2022-03-03
  • C語言編程技巧 關(guān)于const和#define的區(qū)別心得

    C語言編程技巧 關(guān)于const和#define的區(qū)別心得

    盡量用const和inline而不用#define 這個條款最好稱為:“盡量用編譯器而不用預(yù)處理”,因為#define經(jīng)常被認(rèn)為好象不是語言本身的一部分。這是問題之一。再看下面的語句:
    2013-02-02

最新評論