单链表 模板类

来源:互联网 发布:http连接数据库 编辑:程序博客网 时间:2024/05/29 13:48

 

 

 

 

 

#include <iostream>using namespace std;


 

//声明单链表类模板(为了说明友元类)template <class Type> class List;//定义链表结点类模板template <class Type> class ListNode{//单链表类是链表结点类的友元类friend class List<Type>;Type data; //结点数据域 ListNode<Type> *next; //结点指针域public://构造函数,注意赋初值的语法ListNode():next(NULL){ }ListNode(Type & item):data(item),next(NULL) {}public://成员函数//以item和next建立一个新结点ListNode<Type> * CreateNode(Type & item, ListNode<Type> *next=NULL);//取得结点中数据Type GetData() {return data};//取得结点中指针值ListNode<Type> * GetPointer() {return next};//设置结点的data值void SetDate(Type value) {data=value;}//修改结点的指针值void SetPointer(ListNode<Type> *nextT) {next=nextT;}};//成员函数:以item和next建立一个新结点template <class Type>ListNode<Type> * ListNode<Type>::CreateNode(Type & item, ListNode<Type> *next =NULL ){ListNode<Type> *newNode=new ListNode<Type>(item);// 将当前结点后面的结点 挂接在新建结点上newNode->next=next;return newNode;}


 

//定义单链表类模板template <class Type> class List{private://链表的表头指针和当前结点指针ListNode<Type> *first, *current;public://构造函数:构造带头结点的单链表(空表)List(Type & value)//头结点数据给定{first=current=new ListNode<Type>(value);}List() //头结点数据不定{first=current=new ListNode<Type>;}~List(){MakeEmpty();//将链表置为空表 delete first;//删除头结点}//将链表置为空void MakeEmpty(); //计算链表的长度int Length() const; //搜索含数据value的结点并置为当前结点ListNode<Type> *Find(Type value);//搜索第 i 个结点的地址并置为当前结点  ListNode<Type>*Locate(int i);//获得当前结点的数据域的地址Type* GetDate();//将value插在当前位置后并置为当前节点int Insert (Type value);//将链表当前结点删去,返回该结点数据Type * RemoveCurrentNode();//当前指针置于表头, 返回表头结点地址ListNode<Type> * Firster(){current=first;return first;}//获得第一个结点的数据域的地址Type *First();//获得当前结点后继的数据域地址Type *Next();bool IsEmpty(){return Length()==0?true:false;}//显示输出链表void Dispaly();};template <class Type>  //函数模板void List<Type>::MakeEmpty(){//删去链表中除表头结点外的所有其他结点ListNode<Type> * q;while(first->next!=NULL){//将表头结点后第一个结点从链中摘下q=first->next;first->next=q->next;delete q;}//当前指针置于表头结点,形成空表current=first;}template <class Type>int List<Type>::Length() const{ListNode<Type> *p=first->next;int count=0;while(p!=NULL){count++;p=p->next;}return count;}//将value插在当前位置后并置为当前节点// 创建链表时,每次都将新结点插入到末尾template<class Type>int List<Type>::Insert(Type value){if (current==NULL)return 0;ListNode<Type> *newNode= current->CreateNode(value, current->next);current=current->next=newNode;return 1;//插入成功,函数返回1}//获得当前结点的数据域的地址template <class Type>Type* List<Type>::GetDate(){if (current==NULL)return NULL;elsereturn ¤t->data;}//显示输出链表template <class Type>void List<Type>::Dispaly(){ListNode<Type> *p=current;current=first->next;while(current){cout<<*GetDate()<<" ";current=current->next;}cout<<endl;current=p;}//搜索含数据value的结点并置为当前结点template <class Type>ListNode<Type>* List<Type>::Find(Type value){current =first->next;while(current)if (current->data==value)break;elsecurrent=current->next;return current;}//搜索第 i 个结点的地址并置为当前结点template <class Type>ListNode<Type> *List<Type>::Locate(int i){if (i<0)return NULL;// Locate(0) 定位于链表头结点if (i==0){current=first;return first;}ListNode<Type> *p=first->next;int k=1;while(p!=NULL&&k<i){p=p->next;k++;}if (p!=NULL){current=p;}return p;}//将链表当前结点(current)删去,返回该结点数据template <class Type>Type * List<Type>::RemoveCurrentNode(){if (current==first)return NULL;ListNode<Type> *prior=first;//寻找当前元素的前驱元素结点while(prior->next!=current)prior=prior->next;prior->next=current->next;Type *val=¤t->data;delete current;current=prior->next;return val;}//获得第一个结点的数据域的地址template <class Type>Type* List<Type>::First(){if(IsEmpty())return NULL;current=first->next;return ¤t->data;}//获得当前结点后继的数据域地址template <class Type>Type* List<Type>::Next(){if (current==NULL||current->next==NULL)return NULL;current=current->next;return ¤t->data;}


 

void main(){//定义两个单链表,头结点数据不同int v=0;List <int> L1(v); // 空表 List <char> L2;// 创建链表时,每次都将新结点插入到链表末尾for (int i=1;i<=10;i++)L1.Insert(i);/*// 创建链表时,每次都将新结点插入到链表头for (int i=1;i<=10;i++){current=first;     // 将当前位置定位到表头  或使用 L1.Locate(0)定位于头结点; L1.Insert(i);}*/for (char i='a';i<'f';i++)L2.Insert(i);L1.Dispaly();L2.Dispaly();//搜索数据ListNode<char> *p=L2.Find('c');if (p){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}ListNode<int> *q=L1.Locate(5);if(q)cout<<"Yes"<<endl;elsecout<<"No"<<endl;//删除当前结点(current)L1.RemoveCurrentNode();L1.Dispaly();L2.RemoveCurrentNode();L2.Dispaly();//获得第一个结点的数据项地址int *p1=L1.First();cout<<*p1<<endl;while(getchar()!='a');}