Sequence Table (C ++)

Source: Internet
Author: User

I am looking for a job recently. I am reviewing the data structure. Below is a class (header file) of the sequence table written in C ++)

// March 15, 2013 AD // sequence list -- by Paul # ifndef _ seqlist _ # DEFINE _ seqlist_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 {return m_ncurrentsize + 1;} int find (Type X) const; int iselement (Type X) const; int insert (Type X, int I); int remove (Type X); int isempty () {return m_ncurrentsize =-1;} int isfull () {return m_ncurrentsize = m_nmaxsize-1;} type get (int I) {return I <0 | I> m_ncurrentsize? (Cout <"can't find the element" <Endl, 0): m_element [I];} void print (); Private: type * m_element; const int m_nmaxsize; int m_ncurrentsize ;}; // implementation of some functions ..... Template <typename type> int seqlist <type>: Find (Type X) const {for (INT I = 0; I <m_ncurrentsize; I ++) {If (m_element [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> mm_ncurrentsize + 1 | m_ncurrentsize = m_nmaxsize-1) {cout <"the operation is illegal" <Endl; return 0;} m_ncurrentsize ++; For (Int J = m_ncurrentsize; j> I; j --) {m_element [J] = m_element [J-1];} m_element [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_element [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_element [I] <Endl; cout <Endl;} # endif

(It seems that I haven't written the C ++ code for a long time. I am unfamiliar with it. I am very familiar with it)

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.