改进后的Mylist(双链表)--添加了在指定位置后插入,析构里删除节点MyNode
2012-03-09 15:37
393 查看
#if !defined(AFX_MYNODE_H__9D25A9D2_B0CB_4903_A7B2_63009846E9DA__INCLUDED_)
#define AFX_MYNODE_H__9D25A9D2_B0CB_4903_A7B2_63009846E9DA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <class T>
class MyNode{
public:
T* data;//到底使用指针还是 T,这里是指针
MyNode *pre;
MyNode *next;
public :
MyNode(){
this->data=NULL;
this->pre=NULL;
this->next=NULL;
}
MyNode(T *&data){
this->data=data;
this->pre=NULL;
this->next=NULL;
}
T* getData(){
return data;
}
virtual ~MyNode(){
// if(pre!=NULL)
// delete pre;
// if(next!=NULL)
// delete next;
}
};
// MyList.h: interface for the MyList class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYLIST_H__46366E9A_BF6C_4026_8BB7_F2C5DA65F703__INCLUDED_)
#define AFX_MYLIST_H__46366E9A_BF6C_4026_8BB7_F2C5DA65F703__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "MyNode.h"
template <class T>
class MyList
{
//表头和表尾
private:
int len;
MyNode<T> *head;
MyNode<T> *tail;
public:
void deleteData(int i){
MyNode<T> *tmp=NULL;
MyNode<T> *prev=NULL;
tmp=head;
if(i==0)
{
if(head!=NULL)
{
head=tmp->next;
delete tmp;
return ;
}
}
for(int j=0;j<i;j++){
if(tmp->next!=NULL)
{
prev=tmp;
tmp=tmp->next;
}
}
prev->next=tmp->next;
delete tmp;
}
MyNode<T>* get(int i){
MyNode<T> *tmp=NULL;
MyNode<T> *pre=NULL;
tmp=head;
if(i==0)
return tmp;
for(int j=0;j<i;j++){
if(tmp->next!=NULL)
{
pre=tmp;
tmp=tmp->next;
}
}
return tmp;
}
MyList(){
len=0;
head=tail=NULL;
}
MyList(T *&data){
len=0;
head=tail=NULL;
if(head==NULL)
{
head=tail=new MyNode<T>(data);
head->pre=NULL;
head->next=NULL;
}
len++;
}
//最前面插入
void insertBefore(T* data){
MyNode<T> *tmp=NULL;
if(head==NULL)
{
tmp=new MyNode<T>(data);
head=tail=tmp;
head->pre=NULL;
head->next=NULL;
}else{
tmp=new MyNode<T>(data);
tmp->next=head;
tmp->pre=NULL;
head->pre=tmp;
head=tmp;
}
len++;
}
//在i前面插入
void insertAfter(T* data,int i){
MyNode<T> *tmp=NULL;
MyNode<T> *tmpnew=NULL;
if(head==NULL)
{
tmp=new MyNode<T>(data);
head=tail=tmp;
head->pre=NULL;
head->next=NULL;
}else{
tmp=head;
for(int j=0;(j<i&&i<len);j++)
{
tmp=tmp->next;
}
tmpnew=new MyNode<T>(data);
tmpnew->pre=tmp;
tmpnew->next=tmp->next;
tmp->next->pre=tmpnew;
tmp->next=tmpnew;
}
len++;
}
//查找索引i位置的节点
MyNode<T>* Find(int i){
MyNode<T> *tmp=NULL;
tmp=head;
for(int j=0;(j<i&&i<len);j++)
{
tmp=tmp->next;
}
return tmp;
}
int getLen(){
return len;
}
~MyList(){
MyNode<T>* tmp=this->head;
MyNode<T>* tmppre=NULL;
while(tmp!=NULL){
tmppre=tmp;
tmp=tmp->next;
if(tmppre!=NULL)
delete tmppre;
}
// delete tmp;
}
};
#endif // !defined(AFX_MYLIST_H__46366E9A_BF6C_4026_8BB7_F2C5DA65F703__INCLUDED_)
调用
CString *a=new CString("A");
CString *b=new CString("B");
CString *c=new CString("c");
MyList<CString> *mylist=new MyList<CString>(a);
mylist->insertBefore(c);
mylist->insertAfter(b,0);
// mylist->deleteData(0);
for(int i=0;i<=mylist->getLen()-1;i++){
CString* tmp=((MyNode<CString>*)mylist->get(i))->getData();
::MessageBox(NULL,tmp->GetBuffer(10)
,"test",2);
}
#define AFX_MYNODE_H__9D25A9D2_B0CB_4903_A7B2_63009846E9DA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <class T>
class MyNode{
public:
T* data;//到底使用指针还是 T,这里是指针
MyNode *pre;
MyNode *next;
public :
MyNode(){
this->data=NULL;
this->pre=NULL;
this->next=NULL;
}
MyNode(T *&data){
this->data=data;
this->pre=NULL;
this->next=NULL;
}
T* getData(){
return data;
}
virtual ~MyNode(){
// if(pre!=NULL)
// delete pre;
// if(next!=NULL)
// delete next;
}
};
// MyList.h: interface for the MyList class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYLIST_H__46366E9A_BF6C_4026_8BB7_F2C5DA65F703__INCLUDED_)
#define AFX_MYLIST_H__46366E9A_BF6C_4026_8BB7_F2C5DA65F703__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "MyNode.h"
template <class T>
class MyList
{
//表头和表尾
private:
int len;
MyNode<T> *head;
MyNode<T> *tail;
public:
void deleteData(int i){
MyNode<T> *tmp=NULL;
MyNode<T> *prev=NULL;
tmp=head;
if(i==0)
{
if(head!=NULL)
{
head=tmp->next;
delete tmp;
return ;
}
}
for(int j=0;j<i;j++){
if(tmp->next!=NULL)
{
prev=tmp;
tmp=tmp->next;
}
}
prev->next=tmp->next;
delete tmp;
}
MyNode<T>* get(int i){
MyNode<T> *tmp=NULL;
MyNode<T> *pre=NULL;
tmp=head;
if(i==0)
return tmp;
for(int j=0;j<i;j++){
if(tmp->next!=NULL)
{
pre=tmp;
tmp=tmp->next;
}
}
return tmp;
}
MyList(){
len=0;
head=tail=NULL;
}
MyList(T *&data){
len=0;
head=tail=NULL;
if(head==NULL)
{
head=tail=new MyNode<T>(data);
head->pre=NULL;
head->next=NULL;
}
len++;
}
//最前面插入
void insertBefore(T* data){
MyNode<T> *tmp=NULL;
if(head==NULL)
{
tmp=new MyNode<T>(data);
head=tail=tmp;
head->pre=NULL;
head->next=NULL;
}else{
tmp=new MyNode<T>(data);
tmp->next=head;
tmp->pre=NULL;
head->pre=tmp;
head=tmp;
}
len++;
}
//在i前面插入
void insertAfter(T* data,int i){
MyNode<T> *tmp=NULL;
MyNode<T> *tmpnew=NULL;
if(head==NULL)
{
tmp=new MyNode<T>(data);
head=tail=tmp;
head->pre=NULL;
head->next=NULL;
}else{
tmp=head;
for(int j=0;(j<i&&i<len);j++)
{
tmp=tmp->next;
}
tmpnew=new MyNode<T>(data);
tmpnew->pre=tmp;
tmpnew->next=tmp->next;
tmp->next->pre=tmpnew;
tmp->next=tmpnew;
}
len++;
}
//查找索引i位置的节点
MyNode<T>* Find(int i){
MyNode<T> *tmp=NULL;
tmp=head;
for(int j=0;(j<i&&i<len);j++)
{
tmp=tmp->next;
}
return tmp;
}
int getLen(){
return len;
}
~MyList(){
MyNode<T>* tmp=this->head;
MyNode<T>* tmppre=NULL;
while(tmp!=NULL){
tmppre=tmp;
tmp=tmp->next;
if(tmppre!=NULL)
delete tmppre;
}
// delete tmp;
}
};
#endif // !defined(AFX_MYLIST_H__46366E9A_BF6C_4026_8BB7_F2C5DA65F703__INCLUDED_)
调用
CString *a=new CString("A");
CString *b=new CString("B");
CString *c=new CString("c");
MyList<CString> *mylist=new MyList<CString>(a);
mylist->insertBefore(c);
mylist->insertAfter(b,0);
// mylist->deleteData(0);
for(int i=0;i<=mylist->getLen()-1;i++){
CString* tmp=((MyNode<CString>*)mylist->get(i))->getData();
::MessageBox(NULL,tmp->GetBuffer(10)
,"test",2);
}
相关文章推荐
- js 给json添加新的字段,或者添加一组数据,在JS数组指定位置删除、插入、替换元素
- 将目标元素插入指定位置,删除子节点,替换子节点、应用之城市联动框
- 【链表面试题】删除无头单链表的非尾节点,插入一个元素到无头链表指定位置
- 【链表面试题】删除无头单链表的非尾节点,插入一个元素到无头链表指定位置
- Dom4j向XML中指定位置添加、删除、修改节点——(五)
- Java-集合 list对象进行指定位置插入、删除、迭代、遍历输出(具体习题讲解)
- redis 集群之动态添加redis节点,删除指定ID的redis节点,以及查看redis集群中各个节点的信息
- 单向链表小练习--》创建链表头,添加删除链表节点,释放链表内存,选择插入数据
- 创建一个块定义,并添加实体插入到指定位置
- 使用jQuery创建节点、将节点插入到指定的位置
- js 对表格的动态操作(动态添加行,删除该行,在指定位置添加控件)
- JS 在数组指定位置插入/删除数据的方法
- 使用jQuery创建节点、将节点插入到指定的位置
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- 添加、删除、插入和替换[文本节点]
- 数据结构:单链表(二)之链表节点排序,升序插入数据,删除指定的所有节点,翻转链表操作
- 数据结构复习————单链表在指定位置插入删除元素
- xml文件指定位置插入节点+小结
- 在数组中指定位置添加和删除元素
- 无序单链表的首部,尾部,指定位置的插入,删除,查找等函数