您的位置:首页 > Web前端 > Node.js

改进后的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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐