服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++链表类的封装详情介绍

C++链表类的封装详情介绍

2022-11-20 16:30学习要充足 C/C++

这篇文章主要介绍了C++链表类的封装,文章基于C++的相关资料展开主题的详细内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

1.CList.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#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

计算总节点数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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;
}

到此这篇关于C++链表类的封装详情介绍的文章就介绍到这了,更多相关C++链表类封装内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_56051805/article/details/124386304

延伸 · 阅读

精彩推荐