In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.