日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++鏈表類的封裝詳情介紹_C 語言

作者:學習要充足 ? 更新時間: 2022-06-25 編程語言

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

欄目分類
最近更新