zoukankan      html  css  js  c++  java
  • 顺序表

    Seqlist.h

    const int DefaultSize=100;
    
    template <typename Type> class SeqList{
    public:
    	SeqList(int sz=DefaultSize)
    		:m_nmaxsize(sz),m_ncurrentsize(-1){
    		if(sz>0){
    			m_elements=new Type[m_nmaxsize];
    		}
    	}
    	~SeqList(){
    		delete[] m_elements;
    	}
    	int Length() const{					//get the length
    		return m_ncurrentsize+1;
    	}
    	int Find(Type x) const;				//find the position of x
    	int IsElement(Type x) const;		//is it in the list
    	int Insert(Type x,int i);			//insert data
    	int Remove(Type x);					//delete data
    	int IsEmpty(){
    		return m_ncurrentsize==-1;
    	}
    	int IsFull(){
    		return m_ncurrentsize==m_nmaxsize-1;
    	}
    	Type Get(int i){					//get the ith data
    		return i<0||i>m_ncurrentsize?(cout<<"can't find the element"<<endl,0):m_elements[i];
    	}
    	void Print();
    
    private:
    	Type *m_elements;
    	const int m_nmaxsize;
    	int m_ncurrentsize;
    };
    
    template <typename Type> int SeqList<Type>::Find(Type x) const{
    	for(int i=0;i<m_ncurrentsize;i++)
    		if(m_elements[i]==x)
    			return i;
    	cout<<"can't find the element you want to find"<<endl;
    	return -1;
    }
    
    template <typename Type> int SeqList<Type>::IsElement(Type x) const{
    	if(Find(x)==-1)
    		return 0;
    	return 1;
    }
    
    template <typename Type> int SeqList<Type>::Insert(Type x, int i){
    	if(i<0||i>m_ncurrentsize+1||m_ncurrentsize==m_nmaxsize-1){
    		cout<<"the operate is illegal"<<endl;
    		return 0;
    	}
    	m_ncurrentsize++;
    	for(int j=m_ncurrentsize;j>i;j--){
    		m_elements[j]=m_elements[j-1];
    	}
    	m_elements[i]=x;
    	return 1;
    }
    
    template <typename Type> int SeqList<Type>::Remove(Type x){
    	int size=m_ncurrentsize;
    	for(int i=0;i<m_ncurrentsize;){
    		if(m_elements[i]==x){
    			for(int j=i;j<m_ncurrentsize;j++){
    				m_elements[j]=m_elements[j+1];
    			}
    			m_ncurrentsize--;
    			continue;
    		}
    		i++;
    	}
    	if(size==m_ncurrentsize){
    		cout<<"can't find the element you want to remove"<<endl;
    		return 0;
    	}
    	return 1;
    }
    
    template <typename Type> void SeqList<Type>::Print(){
    	for(int i=0;i<=m_ncurrentsize;i++)
    		cout<<i+1<<":\t"<<m_elements[i]<<endl;
    	cout<<endl<<endl;
    }
    

    Test.cpp
    #include <iostream>
    #include "SeqList.h"
    
    using namespace std;
    
    int main()
    {
    	SeqList<int> test(15);
    	int array[15]={2,5,8,1,9,9,7,6,4,3,2,9,7,7,9};
    	for(int i=0;i<15;i++){
    		test.Insert(array[i],0);
    }
    	test.Insert(1,0);
    	cout<<(test.Find(0)?"can't be found ":"Be found ")<< 0 << endl<<endl;
    	test.Remove(7);
    	test.Print();
    	test.Remove(9);
    	test.Print();
    	test.Remove(0);
    	test.Print();
    	return 0;
    }
    
  • 相关阅读:
    BZOJ3992 [SDOI2015]序列统计 【生成函数 + 多项式快速幂】
    BZOJ3993 [SDOI2015]星际战争 【二分 + 网络流】
    BZOJ3325 [Scoi2013]密码 【manacher】
    BZOJ3534 [Sdoi2014]重建 【矩阵树定理】
    BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
    BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
    BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】
    BZOJ4817 [Sdoi2017]树点涂色 【LCT + 线段树】
    BZOJ1195 [HNOI2006]最短母串 【状压dp】
    malloc的使用、用malloc动态分配内存以适应用户的需求的源代码实例
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2438169.html
Copyright © 2011-2022 走看看