C++鏈表類的封裝詳情介紹
1.CList.h
#ifndef CLIST_H
#define CLIST_H
?
class CNode ? ? ? ? //節(jié)點(diǎn)類
{
public:
?? ?CNode();
? ? ~CNode();
?? ?void *data; ? ? //數(shù)據(jù)域 ?節(jié)點(diǎn)數(shù)據(jù)的地址
?? ?CNode *pnext; ? //指針域 ?保存下一個(gè)節(jié)點(diǎn)的地址
protected:
private:
};
?
class CList ? ? ? ? //鏈表類
{
public:
?? ?CList();
?? ?~CList();
?? ?void addList(void *data); ? ? ? ? ? ? ? ? ?//在尾部添加節(jié)點(diǎn)
?? ?int getListCount(); ? ? ? ? ? ? ? ? ? ? ? ?//獲取節(jié)點(diǎn)的個(gè)數(shù)
?? ?int insertListByPos(int pos,void *data); ? //根據(jù)pos插入節(jié)點(diǎn)
?? ?int deleteListByPos(int pos); ? ? ? ? ? ? ?//刪除節(jié)點(diǎn)
?? ?void *getNodeByPos(int pos); ? ? ? ? ? ? ? //獲取節(jié)點(diǎn)數(shù)據(jù)
?? ?void *freeList(); ? ? ? ? ? ? ? ? ? ? ? ? ?//釋放鏈表
protected:
private:
?? ?CNode *head; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //鏈表頭
?? ?int count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //節(jié)點(diǎn)個(gè)數(shù)
};
?
#endif2.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é)點(diǎn)
void CList::addList(void *data)
{
?? ?CNode *tmp = this->head;
?? ?while(tmp->pnext!=NULL)
?? ?{
?? ??? ?tmp = tmp->pnext;?? ?
?? ?}
?? ?CNode *newNode = new CNode;//創(chuàng)建新節(jié)點(diǎn)
?? ?tmp->pnext = newNode;
?? ?newNode->data = data;
? ? ++(this->count);
}
?
//獲取節(jié)點(diǎn)的個(gè)數(shù)
int CList::getListCount()
{
?? ?return this->count;
}
?
//根據(jù)pos插入節(jié)點(diǎn)
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é)點(diǎn)
?? ??? ??? ?memset(newNode,'\0',sizeof(CNode));
?? ??? ??? ?newNode->data = data;
?? ??? ??? ?newNode->pnext = tmp->pnext;
?? ??? ??? ?tmp->pnext = newNode;
?? ??? ??? ?return 1;
?? ??? ?}
?? ?}
?? ?return 0;
}
?
//刪除節(jié)點(diǎn)
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é)點(diǎn)數(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
計(jì)算總節(jié)點(diǎn)數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)inline hook的原理及應(yīng)用實(shí)例
這篇文章主要介紹了C++實(shí)現(xiàn)inline hook的原理及應(yīng)用,需要的朋友可以參考下2014-08-08
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
QT TCP實(shí)現(xiàn)簡(jiǎn)單的通信示例
這篇文章主要為大家詳細(xì)介紹了QT TCP簡(jiǎn)單的通信示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
解決Microsoft?Visual?C++?2010?Express?運(yùn)行及調(diào)試問(wèn)題
這篇文章主要介紹了解決Microsoft?Visual?C++?2010?Express?運(yùn)行及調(diào)試問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C語(yǔ)言?詳解如何刪除有序數(shù)組中的重復(fù)項(xiàng)
數(shù)組不擅長(zhǎng)插入(添加)和刪除元素。數(shù)組的優(yōu)點(diǎn)在于它是連續(xù)的,所以查找數(shù)據(jù)速度很快。但這也是它的一個(gè)缺點(diǎn)。正因?yàn)樗沁B續(xù)的,所以當(dāng)插入一個(gè)元素時(shí),插入點(diǎn)后所有的元素全部都要向后移;而刪除一個(gè)元素時(shí),刪除點(diǎn)后所有的元素全部都要向前移2022-03-03
C語(yǔ)言編程技巧 關(guān)于const和#define的區(qū)別心得
盡量用const和inline而不用#define 這個(gè)條款最好稱為:“盡量用編譯器而不用預(yù)處理”,因?yàn)?define經(jīng)常被認(rèn)為好象不是語(yǔ)言本身的一部分。這是問(wèn)題之一。再看下面的語(yǔ)句:2013-02-02

