【C++】 链表类封装设计

源码分享,仅供学习使用

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

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;
}

main.cpp

简单测试,获取链表的总节点数量

#include<iostream>
using namespace std;
#include<stdlib.h>
#include"CList.h"
 
int main()
{
	CNode *n1 = new CNode;
	CNode *n2 = new CNode;
	CNode *n3 = new CNode;
	CNode *n4 = new CNode;
 
	CList *myList = new CList;
	myList->addList(n1);
	myList->addList(n2);
	myList->addList(n3);
	myList->addList(n4);
	cout<<myList->getListCount()<<endl;//4
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenruhan_QAQ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值