模板写的单链表(list)
来源:互联网 发布:mac看视频卡死如何退出 编辑:程序博客网 时间:2024/06/07 00:10
模板类的声明和实现不能放到不同的文件中(声明和实现可以分开,但是最好放到一个文件中) 最好都放在.h文件中
下面用模板写的 单链表
//MYLIST_H
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T m_data;
Node * m_next;
Node(T &n);
};
template <class T>
class CMyList
{
public:
CMyList();
~CMyList();
bool push_front(T &n);
bool remove(T &n);
bool find(T &n);
void print();
private:
Node<T> *m_pHead;
int m_dataCount;
};
template <class T>
void CMyList<T>::print()
{
Node<T>* temp;
temp=m_pHead;
while(temp!=NULL)
{
cout<<temp->m_data<<endl;
temp=temp->m_next;
}
}
template <class T>
Node<T>::Node(T &n)
{
data=n;
next=NULL;
}
template <class T>
CMyList<T>::CMyList()
{
m_pHead=NULL;
m_dataCount=0;
}
template <class T>
CMyList<T>::~CMyList()
{
Node<T> * temp;
while(m_pHead!=NULL)
{
temp=m_pHead->m_next;
free(m_pHead);
m_pHead=temp;
}
/*
for(temp=m_pHead->next,m_pHead!=NULL,m_pHead=temp)
{
free(temp);
}
*/
}
template <class T>
bool CMyList<T>::push_front(T &n)
{
Node<T> * temp=(Node<T>*)malloc(sizeof(Node<T>));
temp->m_data=n;
temp->m_next=m_pHead;
m_pHead=temp;
++m_dataCount;
/*
if(m_pHead!=NULL)
{
temp->next=m_pHead;
m_pHead=temp;
}
else
{
m_pHead=temp;
temp->next=NULL;
}
*/
return true;
}
template <class T>
bool CMyList<T>::remove(T &n)
{
Node<T> * tempNode ,*preTempNode;
tempNode=preTempNode=m_pHead;
while(tempNode!=NULL)
{
if(tempNode->data==n)
{
if(tempNode!=preTempNode)
{
preTempNode->next=tempNode->next;
}
else
{
m_pHead=NULL;
}
free(tempNode);
return true;
}
else
{
preTempNode=tempNode;
tempNode=tempNode->next;
}
}
return false;
}
template <class T>
bool CMyList<T>::find(T &n)
{
Node<T> * temp=m_pHead;
while(temp!=NULL)
{
if(temp->data=n)
{
return true;
}
else
{
temp=temp->next;
}
}
return false;
}
- 模板写的单链表(list)
- 单链表(List)的模板实现
- 用C++模板技术写的单链表
- James写的CodeSmith模板(一)
- James写的CodeSmith模板(二)
- James写的CodeSmith模板(三)
- James写的CodeSmith模板(四)
- James写的CodeSmith模板(五)
- 自己写的矩阵模板
- resume模板,写的不好
- 自己写的splay模板
- c++写模板的注意事项
- 标准模板库(list)
- List的JavaExcel工具类(读,写)
- Insertion Sort List(自我感觉写的挺好)
- php 写的日历模板(未连接数据库)
- 使用template (模板引擎写的demo)
- 【c++模板】模拟实现有模板的双向链表(List)(声明和定义分离)
- snmputil命令使用-oid信息查询
- jquery和js创建节点
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- C/C++关键字
- Java Optimize
- 模板写的单链表(list)
- 设计模式学习之Observer模式
- live555学习笔记4-计划任务(TaskScheduler)深入探讨
- (转)不同linux内核映像的区别
- Linux设备驱动---获知当前时间(高于jiffies精度)
- 字符编码GBK,UTF-8
- 内存
- 每日一案:出资入股协议中欺诈行为的认定与处理---摘自“成都法院网”
- Web 不安全代码只会在使用 /unsafe 编译的情况下出现