由于数据结构掌握的不太扎实,最近重新学习了一遍数据结构,因为我发现大部分数据结构的教学内容用的都是伪代码或者纯C语言,而C++的一大优点泛型编程却没怎么被提起,所以打算从头开始用泛型编程简单的去实现所有的数据结构类型。
正文:
以下是一个简单的C++泛型编程实现的链表,仅供参考。
myList.hpp
C++泛型编程的头文件是.hpp格式,具体要想了解的可以百度,这里不做多余赘述
#include
//C++ 模板链表的实现,带头结点
using namespace std;
template
class LNode {
public:
T data;
LNode* next;
};
template
class Link {
private:
LNode* frist;
int sz;
public:
Link();
Link(int sz);
~Link();
//展示数据
void show();
T& operator[](int pos);
//插入与删除
void insert(int pos,const T &elem);
void erase(int pos);
//清空
void clear();
//判断是否为空
bool isEmpty();
//获取数据元素个数
int size();
//查找下标(第一次出现)
int find(const T &elem);
//查找下标(指定起始位置)
int find(int beg,const T &elem);
//查找出现次数
int count(const T &elem);
};
template
inline Link::Link()
{
this->frist = new LNode;
this->sz = 0;
this->frist->next = NULL;
}
template
inline Link::Link(int sz)
{
//尾插法数据由用户输入
this->frist = new LNode;
this->frist->next = NULL;
this->sz = sz;
LNode* pend = this->frist;//尾结点
int i = 0;
while (i < sz)
{
LNode* pnew = new LNode;
pnew->next = NULL;
cout << "请输入: ";
cin >> pnew->data;
cout << endl;
pend->next = pnew;
pend = pnew;//尾结点等于新节点
i++;
}
}
template
inline Link::~Link()
{
if (this->sz == 0)
{
delete this->frist;
return ;
}
LNode* p = this->frist->next;//找到头结点指向的下一个结点
while (p != NULL)
{
LNode* del = p;
p = p->next;
//测试是否数据被正常析构
//cout << "数据: " << del->data << "已释放" << endl;
delete del;
}
delete this->frist;//最后释放头结点
}
template
inline void Link::show()
{
if (this->sz == 0)
{
cout << "[ ]" << endl;
return;
}
cout << "[ ";
LNode* p = this->frist->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << "]" << endl;
}
template
T& Link::operator[](int pos)
{
if (pos > this->sz - 1 || pos < 0)
{
cout << "坐标有误" << endl;
T err = 0;
return err;
}
LNode* p = this->frist;
for (int i = 0; i <= pos; i++)
{
p = p->next;
}
return p->data;
}
template
inline void Link::insert(int pos, const T& elem)
{
if (pos > this->sz || pos < 0)
{
cout << "error: the pos is error" << endl;
return ;
}
LNode* p = this->frist;
for (int i=0;i<=pos-1;i++)
{
p = p->next;
}
LNode* pnew = new LNode;
pnew->data = elem;
pnew->next = p->next;
p->next = pnew;
this->sz++;
return ;
}
template
inline void Link::erase(int pos)
{
if (pos < 0 || pos >= this->sz)
{
cout << "the pos is error" << endl;
return;
}
LNode* p = this->frist;
for (int i=0;inext;
}
//当心内存泄漏!!!
LNode* del = p->next;
p->next = p->next->next;
delete del;
this->sz--;
}
template
inline void Link::clear()
{
if (this->frist->next == NULL)
{
return;
}
LNode* p = this->frist->next;
while (p != NULL)
{
LNode *del = p;
p = p->next;
delete del;
}
this->frist->next = NULL;
this->sz = 0;
}
template
inline bool Link::isEmpty()
{
if (this->frist->next == NULL && this->sz == 0)
return true;
return false;
}
template
inline int Link::size()
{
return this->sz;
}
template
inline int Link::find(const T& elem)
{
LNode* p = this->frist->next;
int i = 0;
while (p != NULL && p->data!=elem)
{
p = p->next;
i++;
}
if (p == NULL)
{
return -1;
}
return i;
}
template
inline int Link::find(int beg, const T& elem)
{
if (beg<0 || beg>this->sz - 1)
{
cout << "起始坐标有误" << endl;
return -1;
}
LNode* p = this->frist->next;
int i = 0;
while ( i<=beg )
{
p = p->next;
i++;
}
//从前驱开始查找
while (p != NULL && p->data != elem)
{
p = p->next;
i++;
}
if (p != NULL) return i;
else return -1;
}
template
inline int Link::count(const T& elem)
{
int count=0;
LNode* p = this->frist->next;
while (p!=NULL)
{
if (p->data == elem)
{
count++;
}
p = p->next;
}
return count;
}
test.cpp
测试代码:
#pragma once
#include
#include"myList.hpp"
using namespace std;
void test01()
{
Link lnk(5);
lnk.show();
int a = lnk.find(1);
while (a != -1)
{
cout << "找到: " <<"下标:"<
注意:
测试代码只提供了一部分测试数据,想要测试自定义类型需要额外重载几个运算符,这只是一个向C++模板编程靠拢的demo版本。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)