Implementing sequential tables with C + + classes

Source: Internet
Author: User

Previously written in C language implementation of the order table, now look at the C + + class implementation of it

Class seqlist{public:seqlist (int capacity=default_capacity): _capacity (capacity), _size (0), _pData (0) {_ Pdata=new datatype[_capacity];} ~seqlist () {if (_pdata!=null) {delete[] _pdata;}} Seqlist (const seqlist& s): _size (S._size), _capacity (s._capacity) {_pdata=new datatype[_ capacity];memcpy (_pdata,s._pdata,_size*sizeof (DataType));} Seqlist& operator= (const seqlist& s) {if (this != &s) {delete[] _pData; _pdata=new datatype[s._capacity];memcpy (_pdata,s._pdata,s._size*sizeof (DataType)); _size=s._size;_capacity =s._capacity;} Return *this;} Void pushback (const datatype& d) {_checkcapacity (); _pdata[_size++]=d;} Void pushfront (const datatype& d) {_checkcapacity (); Int start=_size-1;while (start>=0 ) {_pdata[start+1]=_pdata[start];start--;} _pdata[0]=d;_size++;} Void popback () {if (_pdata!=null) {_pdata[_size--];}} Void popfront () {if (_pdata!=null) {for (int i=0; i<_size-1;  i++) {_pdata[i]=_pdata[i+1];} _size--;}} Datatype& operator[] (Int index) {return  ( _pdata[index]);} Int find ( const datatype& d) {int i=0;for (i=0; i<_size-1; i++ ) { if (_pdata[i]==d) {return i;}} Return -1;} Void insert (int pos,const datatype& d) {assert (pos>=0 && pos<_ size); int move=_size-pos;int end=_size;_checkcapacity (); while (move--) {_pdata[end]=_pdata[end-1]; end--;} _pdata[pos]=d;_size++;} Void remove (const datatype& d) {int pos=find (d); Int i=0;if (Pos!=-1) {for (i=pos;  i<_size-1; i++) {_pdata[i]=_pdata[i+1];} _size--;}} Void reverse () {int left=0;int right=_size-1;while (left<right) {Datatype tmp=_pdata[left] ; _pdata[left]=_pdata[right];_pdata[right]=tmp;left++;right--;}} friend ostream& operator<< (ostream& os,const seqlist& s);p rivate: int _capacity; //Linear table Capacity int _size;    //contains the number of data datatype *_pdata;void _checkcapacity () {if (_size==_capacity) {datatype *tmp=new datatype[_capacity+default_capacity ];memcpy (tmp,_pData,_size*sizeof ( DataType));d Elete[] _pdata;_pdata=tmp;_capacity=_capacity+default_capacity ;}}};o stream& operator<< (ostream& os,const seqlist& s) {int i=0;for (i=0;  i<s._size; i++) {os<<s._pdata[i]<< '   ';} Os<<endl; return os;}


This article is from the "July boreas" blog, please be sure to keep this source http://luminous.blog.51cto.com/10797288/1750291

Implementing sequential tables with C + + classes

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.