Get the App
SLTechnology News&Howtos  ›  Development  › 

How to encapsulate C++ linked list class

Shulou Source: shulou.com Published: 2022-05-31 19:13:21 10月03日 Update

This article mainly introduces the relevant knowledge of "how to encapsulate C++ linked list classes". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to encapsulate C++ linked list classes" can help you solve the problem.

1.CList.h#ifndef CLIST_H#define CLIST_H class CNode / / Node class {public: CNode (); ~ CNode (); void * data; / / address of data domain node data CNode * pnext; / / address of the next node stored in the pointer domain protected:private:}; class CList / / linked list class {public: CList (); ~ CList (); void addList (void * data) / / add node int getListCount () to the tail; / / obtain the number of nodes int insertListByPos (int pos,void * data); / / insert node int deleteListByPos (int pos) according to pos; / / delete node void * getNodeByPos (int pos); / / get node data void * freeList () / / release linked list protected:private: CNode * head; / / chain header int count; / / number of nodes}; # endif2.CList.cpp#include "CList.h" # include#include//memset header file CNode::CNode () {this- > data = NULL; this- > pnext = NULL } CNode::~CNode () {} CList::CList () {this- > head = new CNode; this- > count = 0;} CList::~CList () {} / / add a node void CList::addList (void * data) {CNode * tmp = this- > head; while (tmp- > pnextlegs null) {tmp = tmp- > pnext;} CNode * newNode = new CNode;// create a new node tmp- > pnext = newNode NewNode- > data = data; + + (this- > count);} / / get the number of nodes int CList::getListCount () {return this- > count;} / / insert node int CList::insertListByPos (int pos,void * data) {int num = 0 according to pos; CNode* tmp = this- > head; while (tmp- > pnextnodes null) {count++; tmp = tmp- > pnext If (pos = = count) {CNode* newNode = new CNode; / / new node memset (newNode,'\ 0Node journal sizeof (CNode); newNode- > data = data; newNode- > pnext = tmp- > pnext; tmp- > pnext = newNode; return 1;}} return 0 } / / Delete node int CList::deleteListByPos (int pos) {int count = 0; CNode* tmp = head- > pnext,*pre = head; while (tmpumped null) {count++; if (count = = pos) {pre- > pnext = tmp- > pnext; / / tmp data field releases delete tmp- > data; delete tmp; return 1 } pre = pre- > pnext; tmp = tmp- > pnext;} return-1;} / / get node data void* CList::getNodeByPos (int pos) {int count = 0; CNode* tmp = head; while (tmp- > pnextnodes null) {count++; tmp = tmp- > pnext; if (pos = = count) {return tmp- > data }} return NULL;} / / release linked list void* CList::freeList () {CNode* tmp = head; while (tmpumped null) {head = head- > pnext; delete tmp- > data; delete tmp; tmp = head;} return this- > head;} 3.main.cpp

Calculate the total number of nodes:

# includeusing namespace std # include "CTools.h" # include "CLabel.h" # include "CEdit.h" # include "CButton.h" # include "CtrBase.h" # include "CLogin.h" / / display login window # include "CIndexWin.h" / / Manager main interface window # include "CManagerWin.h" / / Manager main interface window # include "CWaiterWin.h" / / attendant main interface window # include#include "CList.h" int main () {CLoginWin * login = new CLoginWin (12q5) 30pr 20) CIndexWin * index = new CIndexWin; CManagerWin * manager = new CManagerWin; CWaiterWin * waiter = new CWaiterWin; CList * myList = new CList; myList- > addList (login); myList- > addList (index); myList- > addList (manager); myList- > addList (waiter); cout

Tags: Nodes data clips encapsulation number interface knowledge address tail industry different practical content practical practical pointer file article method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi NVidia OPPO Reno Shulou Technology