C + + implementation sequence table

Source: Internet
Author: User


#include <iostream>
using namespace Std;

typedef int DATATYPE;

Class Seqlist
{
Public
Seqlist ()
: _array (NULL)
, _size (0)
, _capacity (0)
{
cout << "seqlist ()" << Endl;
}

Seqlist (datatype* array, size_t size)
: _array (new datatype[size])
, _size (size)
, _capacity (size)
{
cout << "seqlist ()" << Endl;
memcpy (_array, array, sizeof (DataType) *size);
}

~seqlist ()
{
if (_array)
{
Delete[] _array;
_array = NULL; //?

_size = 0;
_capacity = 0;
}
cout << "~seqlist ()" << Endl;
}


Seqlist (const seqlist& s)
: _array (NULL)
, _size (0)
, _capacity (0)
{
Seqlist tmp (S._array, s._size);
Tmp. Swap (*this);
}


seqlist& operator= (seqlist s)
{
S.swap (*this);

return *this;
}

Public
void Print ()
{
for (int i = 0; i < _size; ++i)
{
cout << _array[i] << "";
}

cout << Endl;
}

void Pushback (const datatype& x)
{
_checkcapacity (_size + 1);

_array[_size++] = x;
}

void Popback ()
{
_size--;
}
void Pushfront (const datatype& x)
{
_checkcapacity (_size + 1);
_size++;
for (int i = _size; I >= 0; i--)
{
_array[i] = _array[i-1];
}
_array[0] = x;
}
void Popfront ()
{
for (int i = 1; i < _size; ++i)
{
_ARRAY[I-1] = _array[i];
}
_size--;
}

void Insert (size_t pos, const datatype& x)
{
for (int i = _size; I >= pos; i.)
{
_ARRAY[I+1] = _array[i];
}
_array[pos] = x;
++_size;
}
int Find (const datatype& x)
{
for (int i = 0; i < _size; ++i)
{
if (_array[i] = = x)
return i;
}
printf ("No this number!" ");
return 0;
}
void Erase (size_t pos)
{
for (int i = pos; i < _size; ++i)
{
_array[i] = _array[i + 1];
}
_size--;
}

datatype& operator[] (size_t index)
{
ASSERT (_array);
ASSERT (Index < _size);

return _array[index];
}

size_t Size ()
{
return _size;
}

size_t Capacity ()
{
return _capacity;
}

void Reserve (size_t size)
{
_checkcapacity (size);
}

void Clear ()
{
_size = 0;
}

void Swap (seqlist& s)
{
Std::swap (_array, S._array);
Std::swap (_size, s._size);
Std::swap (_capacity, s._capacity);
}

How to free up space??

Private
void _checkcapacity (size_t size)
{
if (Size > _capacity)
{
_capacity = size > 2 * _capacity? Size:2 * _capacity;

datatype* tmp = new datatype[_capacity];
memcpy (TMP, _array, sizeof (DataType) *_size);
Delete[] _array;

_array = tmp;
}
}

Private
Datatype* _array;
size_t _size;
size_t _capacity;
};



void Test1 ()
{
int array[5] = {0, 1, 2, 3, 4};
Seqlist S1 (array, 5);
S1. Print ();

S1. Pushback (5);
S1. Pushback (6);
S1. Print ();

S1. Clear ();
S1. Print ();
cout << S1. Capacity () << Endl;

{
Seqlist tmp;
Tmp. Swap (S1);
}

cout << S1. Capacity () << Endl;

Seqlist S2 (S1);
}

void Test2 ()
{
int array[5] = {0, 1, 2, 3, 4};
Seqlist S1 (array, 5);
S1. Print ();

Seqlist S2 (S1);
S2. Print ();

Seqlist S3;

s3 = S1;
S3. Print ();
}

void Test3 ()
{
int array[5] = {0, 1, 2, 3, 4};
Seqlist S1 (array, 5);
S1. Print ();

/*s1. Popback ();
S1. Print (); */

S1. Pushfront (6);
S1. Print ();

S1. Popfront ();
S1. Print ();

S1. Insert (2, 10);
S1. Print ();

S1. Find (9);

S1. Erase (2);
S1. Print ();
}

void Main ()
{
Test3 ();
}

C + + implementation sequence 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.