C ++ implements the Array data structure to implement the array data structure
1. Definition of Array. h, Array <T>
Template <class T> class Array {protected: T * data; // a pointer to the Array data unsigned int base; // base is the starting table of the Array unsigned int length; // length: Array length public: Array (); // default constructor Array (unsigned int, unsigned int = 0); // Array constructor ~ Array (); // The Destructor Array (Array const &); // copy the constructor Array & operator = (Array const &); // overload the equal sign operator, it is used to assign T const & operator [] (unsigned int) const to another array; // reload the brackets operator and return a T value constant. The return value cannot be changed, add const at the end of the function to indicate that this Pointer Points to const T & operator [] (unsigned int); // reload the brackets operator and return a T value constant, the returned value can be changed to T * Data () const; // the pointer data unsigned int Base () const to return the array Data; // The returned member base unsigned int Length () const; // return the member length void SetBase (unsigned int); // set the value of the member variable base void SetLength (unsigned int); // set the value of the member variable length }; // the space occupied by the dynamic array S (n) = sizeof (T *) + 2 sizeof (unsigned int) + nsizeof (T). Assuming that the space occupied by the T type is a constant, so S (n) = O (n)
2. Implementation of member functions in Array <T>
# Include "Array. h "template <class T> Array <T>: Array (): data (new T [10]), base (0), length (0) {}// the default constructor does not contain variables. You only need to give an initial value to the object's variables. The time complexity O (1) template <class T> Array <T> :: array (unsigned int n, unsigned int m): data (new T [n]), base (m), length (n) {}// initialize the Array, n is the length of the Array, and the time complexity constant O (1) template <class T> Array <T >:: Array (array <T> const & Array ): data (new T [array. length]), base (array. base), length (array. length) {for (unsigned Int I = 0; I <length; ++ I) data [I] = array. data [I];} // backup constructor, which assigns an Array to another Array. the time complexity is O (n) template <class T> Array <T> :: ~ Array () {delete [] data;} // destructor, delete the memory space occupied by the Array template <class T> T * Array <T>: Data () const {return data;} template <class T> unsigned int Array <T >:: Base () const {return base;} template <class T> unsigned int Array <T>:: Length () const {return length;} // these three are accessors and are used to return members. The time complexity is O (1) template <class T> T const & Array <T>: operator [] (unsigned int position) const {unsigned int const offset = position-base; if (Fset> = length) throw out_of_range ("invalid position"); return data [offset];} template <class T> T & Array <T> :: operator [] (unsigned int position) {unsigned int const offset = position-base; if (offset> = length) throw out_of_range ("invalid position "); return data [offset];} // both of them are used to remove the overload of the table operator. The difference is that the first return value cannot be used as the left value, and the second return value can be used as the left value, the time complexity is O (1) template <class T> void Array <T>: SetBase (unsigned int newBase) {base = NewBase;} template <class T> void Array <T>: SetLength (unsigned int newLength) {T * const newData = new T [newLength]; unsigned int const min = length <newLength? Length: newLength; for (unsigned int I = 0; I <min; ++ I) newData [I] = data [I]; delete [] data; data = newData; length = newLength;} // these two functions are used to reset the object member. The time complexity is T (m, n) = min (m, n) * T (T :: T (T &) + O (1) template <class T> Array <T> & Array <T>: operator = (Array <T> const & array) {if (this! = & Array) {delete [] data; base = array. base; length = array. length; data = new T [length]; for (unsigned int I = 0; I <length; ++ I) data [I] = array. data [I];} return this;} // overload the value assignment operator. The time complexity is O (n)
3. Test the main function main. cpp.
# Include "Array. cpp "# include <iostream> using namespace std; template <class T> void Output (Array <T> array) {cout <"data:"; for (unsigned int I = array. base (); I <array. length (); I ++) {cout <array. data () [I] <";}cout <endl; cout <" length: "<array. length () <endl; cout <"base:" <array. base () <endl;} int main () {cout <"Array () is being executed... "<Endl; Array <int> array0 = Array <int> (); Output (array0); cout <" Array (unsigned int, unsigned int = 0) executing... "<Endl; Array <int> array1 = Array <int> (10); Output (array1); cout <" Array (Array const &) is being executed... "<Endl; Array <int> array2 (array1); Output (array2); cout <"~ Array () is being executed... "<Endl; array2 .~ Array (); Output (array2); cout <"T const * Data () const, unsigned int Base () const, unsigned int Length () const, "<" T const & operator [] (unsigned int) const is executed in the Output function... "<Endl; cout <" T & operator [] (unsigned int) is being executed... "<Endl; Array <int> array3 (10); for (unsigned int I = array1.Base (); I <array1.Length ()-array1.Base (); I ++) {array3.Data () [I] = I;} Output (array3); cout <"void SetBase (unsigned int) is being executed... "<Endl; array3.SetBase (2); Output (array3); cout <" void SetLength (unsigned int) is being executed... "<Endl; array3.SetLength (7); Output (array3); getchar (); return 0 ;}
4. Test Results
Joseph's ring problem is implemented using the data structure array of C Language
# Include <iostream>
Using namespace std;
Struct Node // define a cyclic Node
{
Int number; // number
Node * next;
};
Node * CreateList (Node * L, int & n, int & m); // creates the Joseph ring function.
Void Joseph (Node * L, int n, int m); // output the number of columns each time
Node * DeleteList (Node ** L, int I, Node * q); // you can specify the number of persons in each column.
Int LengthList (Node * L); // calculates the number of people in the ring.
Void main () // main Function
{
Node * L;
L = NULL; // initialize the tail pointer
Int n, m;
Cout <"Enter N :";
Cin> n; // The length of the ring
If (n <1) {cout <"enter a positive integer! ";}// Handling of number of people exceptions
Else
{
Cout <"Enter the reported number M :";
Cin> m;
If (m <1) {cout <"enter a positive integer! ";}// Number Exception Handling
Else
{
L = CreateList (L, n, m); // assign a value to the end pointer again
Joseph (L, n, m );
}
}
System ("pause ");
}
Node * CreateList (Node * L, int & n, int & m) // create a Joseph ring (tail Insertion Method)
{
Node * q;
For (int I = 1; I <= n; I ++)
{
Node * p;
P = new Node;
P-> number = I;
P-> next = NULL;
If (I = 1) L = q = p; // initialization of the working pointer
Else
{
Q-> next = p;
Q = q-> next;
}
}
Q-> next = L;
If (L! = NULL) {return (L);} // returns the end pointer.
Else cout <"tail pointer exception! "<Endl; // tail pointer Exception Handling
}
Void Joseph (Node * L, int n, int m) // output the person from each column
{
Int k;
Cout <"Enter the first reporter :";
Cin> k;
If (k <1 | k> n) {cout <"Enter 1-" number between <n <"<endl ;}
Else
{
Cout <"\ n columns: \ n ";
For (int I = 1; I <n; I ++)
{
Node * q = new Node;
If (I = 1) q = DeleteList (& L, k + m-1, q); // number of the first column Member
Else q = DeleteList (& L, m, q );
Cout <& quo ...... remaining full text>
The following data structure can efficiently insert and delete elements at any position. Static array B Dynamic Array C linked list D Stack
B...
First, stack is impossible. Stack is advanced and later, and cannot be inserted or deleted at will.
Linked List is inefficient.
If you insert or delete a static array, you need to move data from other locations.
Therefore, I personally think B should be selected.