網站首頁 編程語言 正文
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;
}
原文鏈接:https://blog.csdn.net/m0_56051805/article/details/124386304
相關推薦
- 2022-12-02 批處理bat系統管理之任務計劃篇_DOS/BAT
- 2021-11-16 使用Flutter定位包獲取地理位置_Android
- 2022-07-21 windows與Linux查看端口占用并終止端口占用
- 2022-10-16 docker保存鏡像到本地并加載本地鏡像文件詳解_docker
- 2022-03-31 C#客戶端HttpClient請求認證及數據傳輸_C#教程
- 2022-10-14 Linux環境conda虛擬環境中python解釋器對應問題 + 解決后pip install 路徑
- 2023-03-02 Kotlin關于協程是什么的探究_Android
- 2022-06-27 ASP.net?Core微信平臺開發配置Token_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支