C++链表类怎么封装
发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,这篇文章主要介绍"C++链表类怎么封装"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"C++链表类怎么封装"文章能帮助大家解决问题。1.CList.h#ifnd
千家信息网最后更新 2025年11月09日C++链表类怎么封装
这篇文章主要介绍"C++链表类怎么封装"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"C++链表类怎么封装"文章能帮助大家解决问题。
1.CList.h
#ifndef CLIST_H#define CLIST_H class CNode //节点类{public: CNode(); ~CNode(); void *data; //数据域 节点数据的地址 CNode *pnext; //指针域 保存下一个节点的地址protected:private:}; class CList //链表类{public: CList(); ~CList(); void addList(void *data); //在尾部添加节点 int getListCount(); //获取节点的个数 int insertListByPos(int pos,void *data); //根据pos插入节点 int deleteListByPos(int pos); //删除节点 void *getNodeByPos(int pos); //获取节点数据 void *freeList(); //释放链表protected:private: CNode *head; //链表头 int count; //节点个数}; #endif2.CList.cpp
#include"CList.h"#include#include //memset头文件 CNode::CNode(){ this->data = NULL; this->pnext = NULL;} CNode::~CNode(){} CList::CList(){ this->head = new CNode; this->count = 0;} CList::~CList(){} //在尾部添加节点void CList::addList(void *data){ CNode *tmp = this->head; while(tmp->pnext!=NULL) { tmp = tmp->pnext; } CNode *newNode = new CNode;//创建新节点 tmp->pnext = newNode; newNode->data = data; ++(this->count);} //获取节点的个数int CList::getListCount(){ return this->count;} //根据pos插入节点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; //新节点 memset(newNode,'\0',sizeof(CNode)); newNode->data = data; newNode->pnext = tmp->pnext; tmp->pnext = newNode; return 1; } } return 0;} //删除节点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数据域释放掉 delete tmp->data; delete tmp; return 1; } pre = pre->pnext; tmp = tmp->pnext; } return -1;} //获取节点数据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
计算总节点数:
#includeusing 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" //经理主界面窗口#include"CWaiterWin.h" //服务员主界面窗口#include #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< getListCount()< 关于"C++链表类怎么封装"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
节点
数据
C++
封装
个数
界面
知识
地址
尾部
行业
不同
实用
内容
实用性
实际
指针
文件
文章
方法
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
h3c服务器有哪些系统
北京人力资源网站服务器
vs2010内置sql数据库
陕西省林业软件开发
服务器显示网页不存在
连接服务器指令目前不可用
西瓜视频的服务器在哪
gbase数据库安装
怎么在数据库中计算
幻塔不同服务器的好可以买吗
济南凌跃网络技术有限公司
部队网络安全口号标语
福建省半岛互联网科技有限公司
即墨区苹果软件开发哪家便宜
数据库查看库名
更换MT4服务器
网络安全专题风险评估
vr全景数据库
网络安全自查自纠怎么写
先进网络技术定做价格
js 删除table数据库
ci框架 多数据库
软件开发流量考核指标有哪些
育碧服务器上不去了
网络安全超级简单手抄报
jdbc注册后怎么保存数据库
金融科技工业互联网现状
泰坦之旅手游服务器怎么调
全自动挂机赚钱软件开发
多媒体网络技术与电子工艺