zoukankan      html  css  js  c++  java
  • 单链表(C++)

    数据结构单链表的C++实现:

    //公元2013年3月17日
    //Single List--By Paul
    
    
    #ifndef _SingleList_
    #define _SingleList_
    
    
    #include<iostream>
    using namespace std;
    template<typename Type> class SingleList;
    
    //结点类。。。
    template<typename Type> class ListNode{
    private:
    	Type data;
    	ListNode *pnext;
    private:
    	friend typename SingleList<Type>;
    	ListNode():pnext(null){}
    	ListNode(const Type item,ListNode<Type>*next=null):data(item),pnext(next){}
    	~ListNode()
    	{
    		pnext=null;
    	}
    public:
    	Type GetData();
    	friend ostream& operator<< <Type>(ostream&,ListNode<Type>&);
    };
    
    template<typename Type> Type ListNode<Type>::GetData()
    {
    	return this->data;
    }
    
    template<typename Type> Tostream& operator<<(ostream& os,ListNode<Type>& out)
    {
    	os<<out.data;
    	return os;
    }
    
    //单链表类
    
    template<typename Type> class SingleList
    {
    private:
    	ListNode<Type> *head;
    public:
    	SingleList():head(new ListNode<Type>()){};
    	~SingleList()
    	{
    		MakeEmpty();
    		delete head;
    	}
    	//其他的功能函数
    	void MakeEmpty();
    	int Length();
    	ListNode<Type> *Find(Type value,int n);
    	ListNode<Type> *Find(int n);
    	bool Insert(Type item,int n=0);
    	Type Remove(int n=0);
    	bool RemoveAll(Type item);
    	Type Get(int n);
    	void Print();
    };
    
    //功能函数的实现
    
    template<typename Type> void SingleList<Type>::MakeEmpty()
    {
    	ListNode<Type> *pdel;
    	while(head->pnext!=null)
    	{
    		pdel=head->pnext;
    		head->pnext=pdel->pext;
    		delete pdel;
    	}
    }
    
    template<typename Type> int SingleList<Type>::Length()
    {
    	ListNode<Type> *pmove=head->pnext;
    	int count=0;
    	while(pmove!=null)
    	{
    		pmove=pmove->pnext;
    		count++;
    	}
    	return count;
    }
    
    template<typename Type> ListNode<Type>* SingleList<Type>::Find(int n)
    {
    	if(n<0)
    	{
    		cout<<"The N is out of boundry"<<endl;
    		return null;
    	}
    	ListNode<Type> *pmove=head->pnext;
    	for(int i=0;i<n&&pmove;i++)
    	{
    		pmove=pmove->pnext;
    	}
    	if(pmove==null)
    	{
    		cout<<"The N is out of boundary"<<endl;
    		return null;
    	}
    }
    
    template<typename Type> ListNode<Type>* SingleList<Type>::Find(Type value,int n){
        if(n<1){
            cout<<"The n is illegal"<<endl;
            return NULL;
        }
        ListNode<Type> *pmove=head;
        int count=0;
        while(count!=n&&pmove){
            pmove=pmove->pnext;
            if(pmove->data==value){
                count++;
            }
     
        }
        if(pmove==NULL){
            cout<<"can't find the element"<<endl;
            return NULL;
        }
        return pmove;
    }
    template<typename Type> bool SingleList<Type>::Insert(Type item, int n){
        if(n<0){
            cout<<"The n is illegal"<<endl;
            return 0;
        }
        ListNode<Type> *pmove=head;
        ListNode<Type> *pnode=new ListNode<Type>(item);
        if(pnode==NULL){
            cout<<"Application error!"<<endl;
            return 0;
        }
        for(int i=0;i<n&&pmove;i++){
            pmove=pmove->pnext;
        }
        if(pmove==null){
            cout<<"the n is illegal"<<endl;
            return 0;
        }
        pnode->pnext=pmove->pnext;
        pmove->pnext=pnode;
        return 1;
    }
    
    #endif 
    


  • 相关阅读:
    1.在html中引入js文件和Jquery框架
    在html页面添加一个隐藏域,并渲染一个需要保存的数值,在js中需要再获取,而不影响页面结构
    Django REST framework 使用简记
    BlockingQueue-线程的阻塞队列
    ExecutorService生命周期
    Java transient关键字使用小记
    数组
    Git 常用命令
    Git 常见报错
    python 常见报错
  • 原文地址:https://www.cnblogs.com/NewWork/p/3260582.html
Copyright © 2011-2022 走看看