每次写数据结构都有不同的收获:动态分配的内存空间,删除动态指针后内存也会消失。 如果两个指针同时指向一个动态分配的内存空间。只需要删除其中一个指针就可以了。 #ifndef DOUBLYLINKEDLIST_H #define DOUBLYLINKEDLIST_H #include "stdafx.h" #include <iostream> using namespace std; /*--definition of data type--*/ template<typename T> struct Node { T data; struct Node *LinkL; /*link to previous node*/ struct Node *LinkR; /*link to next node*/ }; /*--functions of doubly linked list--*/ template<typename T> class DoublyList { Node<T> *head; /* point to the head of the doubly linked list*/ Node<T> *tail; /* point to the tail of the doubly linked list*/ Node<T> *prev; Node<T> *behind; int length; public: DoublyList(); ~DoublyList(); Node<T> *CreateList(T elem); /*Create a doubly linked list*/ bool IsEmpty()const; /*judge a empty list*/ bool IsFull()const; /*judge no space for new node*/ int getLength()const; /*get the length of a list*/ Node<T> *DisplayList(int i); /*display every node of a list*/ Node<T> *InsertElem(int i,int num,T elem); /*insert a node to a list*/ T getBefore(T elem); /*get a front node of a node*/ T getBehind(T elem); /*get a back node of a node*/ Node<T> *DeleteNode(T elem); /*delete a node from a list*/ }; template<typename T> DoublyList<T>::DoublyList() { head=new Node<T>; head->data=0; head->LinkL=NULL; head->LinkR=NULL; tail=head; prev=head; behind=head; length=0; } template<typename T> DoublyList<T>::~DoublyList() { delete head; } template<typename T> bool DoublyList<T>::IsEmpty()const { if(length==0)return true; else return false; } template<typename T> bool DoublyList<T>::IsFull()const { try { Node<T> *R=new Node<T>; delete R; return false; } catch(std::bad_alloc exception) { return true; } } template<typename T> int DoublyList<T>::getLength()const { return length; } template<typename T> Node<T> *DoublyList<T>::CreateList(T elem) { if(!IsFull()) { Node<T> *tmp=new Node<T>; tmp->data=elem; if(IsEmpty()) { head->LinkR=tmp; tmp->LinkL=head; tmp->LinkR=NULL; tail=tmp; ++length; } else { tail->LinkR=tmp; tmp->LinkL=tail; tmp->LinkR=NULL; tail=tail->LinkR; ++length; } } return head; } template<typename T> Node<T> *DoublyList<T>::DisplayList(int i) { if(!IsFull()) { if(i==0) { prev=head->LinkR; while(prev->LinkR!=NULL) { cout<<prev->data<<" "; prev=prev->LinkR; } cout<<prev->data<<" "; return head; } else { prev=tail; while(prev->LinkL!=NULL) { cout<<prev->data<<" "; prev=prev->LinkL; } return tail; } } else return head; } template<typename T> Node<T> *DoublyList<T>::InsertElem(int i,int num,T elem) { if(!IsFull()) { Node<T> *curr=new Node<T>; curr->data=elem; curr->LinkL=NULL; curr->LinkR=NULL; if(i==0) { if(num==length) { curr->LinkL=tail; tail->LinkR=curr; curr->LinkR=NULL; tail=tail->LinkR; } else if(num==0) { behind=head->LinkR; head->LinkR=curr; curr->LinkL=head; curr->LinkR=behind; behind->LinkL=curr; } else { prev=head; int n=1; while(n<=num) { prev=prev->LinkR; ++n; } behind=prev->LinkR; prev->LinkR=curr; curr->LinkL=prev; curr->LinkR=behind; behind->LinkL=curr; } ++length; return head; } else { if(num==length) { behind=head->LinkR; head->LinkR=curr; curr->LinkL=head; curr->LinkR=behind; behind->LinkL=curr; } else if(num==0) { curr->LinkL=tail; tail->LinkR=curr; tail=tail->LinkR; } else { int n=1; prev=tail; while(n<num) { prev=prev->LinkL; ++n; } behind=prev->LinkL; prev->LinkL=curr; curr->LinkR=prev; curr->LinkL=behind; behind->LinkR=curr; } ++length; return tail; } } else return head; } template<typename T> T DoublyList<T>::getBefore(T elem) { bool flag=false; behind=head->LinkR; while((behind->data!=elem)) { if(behind->LinkR==NULL) { flag=true; break; } else behind=behind->LinkR; } if(flag) return 0; else { prev=behind->LinkL; return prev->data; } } template<typename T> T DoublyList<T>::getBehind(T elem) { bool flag=false; behind=tail; while((behind->data!=elem)) { if(behind->LinkL==NULL) { flag=true; break; } else behind=behind->LinkL; } if(flag) return 0; else { prev=behind->LinkR; return prev->data; } } template<typename T> Node<T> *DoublyList<T>::DeleteNode(T elem) { bool flag=false; behind=head->LinkR; while(behind->data!=elem) { if(behind->LinkR==NULL) { flag=true; break; } else behind=behind->LinkR; } if(flag) return 0; else { Node<T> *store=behind; prev=behind->LinkL; behind=behind->LinkR; prev->LinkR=behind; behind->LinkL=prev; delete store; --length; return head; } } #endif // doubly linked list.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "doubly linked list.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { DoublyList<int> A; int i=9; while(i!=0) { A.CreateList(i); --i; } cout<<A.getLength()<<"/n"; A.DisplayList(0); cout<<"/n"; A.DisplayList(1); A.InsertElem(0,0,3); A.InsertElem(1,0,7); cout<<"/n"; A.DisplayList(0); cout<<"/n"<<A.getLength()<<"/n"; cout<<A.getBefore(4)<<"/n"; cout<<A.getBehind(66)<<"/n"; A.DeleteNode(6); A.DisplayList(1); system("pause"); return 0; }