C + + Sequential table

Source: Internet
Author: User

Seqlist:

#ifndef Seqlist_h_#defineSeqlist_h_Const intSize = -; template<typename type>classseqlist{Private: Type*m_elements; Const intm_maxsize; intm_currentsize; Public: Seqlist (intSz=size): M_maxsize (SZ), M_currentsize (-1)    {        if(sz>0) m_elements=NewType[m_maxsize]; }    ~seqlist () {Delete[] m_elements;} intLength ()Const{returnM_currentsize+1;}//Get the length    intFind (Type x)Const;//find the position of x    intIselement (Type x)Const;//is x in the list    intInsert (Type x,inti);//Insert x    intRemove (Type x);//Delete x    intIsEmpty () {returnm_currentsize==-1;} intIsfull () {returnM_currentsize = = m_maxsize-1;} TypeGet(inti) {returni<0|| I>m_maxsize? (cout<<"can ' t find the element"<<endl,0): M_element[i]; }    voidPrint ();}; Template<typename type>intSeqlist<type>::find (Type x)Const{     for(intI=0; i<m_currentsize;i++)    {        if(M_elements[i] = =x)returni; } cout<<"can ' t find the element want to find!"<<Endl; return-1;} Template<typename type>intSeqlist<type>::iselement (Type x)Const{    if(Find (x) ==-1)        return 0; return 1;} Template<typename type>intSeqlist<type>::insert (Type x,inti) {    if(i<0|| I>m_currentsize+1|| M_currentsize = = M_maxsize-1) {cout<<"The operate is illegal!"<<Endl; return 0; } m_currentsize++;  for(intj=m_currentsize;j>i;j--) M_elements[j]= m_elements[j-1]; M_elements[i]=x; return 1;} Template<typename type>intSeqlist<type>:: Remove (Type x) {intCount =m_currentsize;  for(intI=0;i<m_currentsize;) {        if(m_elements[i]==x) { for(intj=i;j<m_currentsize;j++) M_elements[j]= m_elements[j+1]; M_currentsize--; Continue; } I++; }    if(Count = =m_currentsize) {cout<<"can ' t find the element want to remove!"<<Endl; return 0; }    return 1;} Template<typename type>voidSeqlist<type>::P rint () { for(intI=0; i<m_currentsize+1; i++) cout<<i+1<<": \ t"<<m_elements[i]<<Endl; cout<<Endl;}#endif

Test:

#include <iostream>#include"Seqlist.h"using namespacestd;intMain () {seqlist<int> Test ( the); intarray[ the] = {2,5,8,1,9,9,7,6,4,3,2,9,7,7,9};  for(intI=0;i< the; i++) test. Insert (Array[i],0); Test.    Print (); 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;}

C + + Sequential table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.