目录
- 1.CList.h
- 2.CList.cpp
- 3.main.cpp
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; //节点个数 }; #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() { } //在尾部添加节点 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
计算总节点数:
#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" //经理主界面窗口 #include"CWaiterWin.h" //服务员主界面窗口 #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; }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)