C language imitation C ++ closed Dynamic Array

Source: Internet
Author: User

# Include <iostream>
# Include <stdlib. h>
Using namespace STD;
Typedef int datatype; // is it used like this?

Struct darray
{
Int capacity; // array capacity
Datatype * parray; // a dynamic array is an internal pointer.
Int count; // number of used elements
Void (* init_array) (struct darray * pthis, int size); // initialize the array size.
Void (* Insert) (struct darray * pthis, int index, datatype element );
Datatype (* get_element) (struct darray * pthis, int index );
Void (* change_size) (struct darray * pthis, int newsize );
INT (* get_capacity) (struct darray * pthis );
Void (* free) (struct darray * pthis );

}; // The semicolon after the struct must not be forgotten ..

Void array_change_size (struct darray * pthis, int newsize)
{
Int Limit = 0;
Int I;
If (newsize <= 0) | (newsize = pthis-> capacity ))
{
Printf ("array_change_size error! : Newsize <= 0 | newsize = pthis-> capacity/N ");
Return;
}
Datatype * pnew = (datatype *) malloc (sizeof (datatype) * newsize );
If (pnew = NULL)
{
Printf ("memory exhaust! /N ");
Exit (1 );
}
Limit = (newsize> pthis-> capacity )? Pthis-> capacity: newsize;
For (I = 0; I <limit; I ++)
{
Pnew [I] = (pthis-> parray) [I];
}
Free (pthis-> parray );
Pthis-> parray = pnew;
Pthis-> capacity = newsize;
}

Void array_insert (struct darray * pthis, int index, datatype element) // This is the case when an element is added.
{
# Ifdef debugarray // used to debug the Array Index
If (index <0)
{
Printf ("array_insert error: index <0/N ");
Exit (1 );
}
# Endif
(Pthis-> parray) [Index] = element;
+ Pthis-> count; // record the number of valid elements in the array
}

Datatype array_get_element (struct darray * pthis, int index)
{
# Ifdef debugarray // used to debug the Array Index
If (index <0)
{
Printf ("array_insert error: index <0/N ");
Exit (1 );
}
# Endif
-- Pthis-> count;
Return (pthis-> parray) [Index];
}

Void array_free (struct darray * pthis)
{
Free (pthis-> parray );
Pthis-> parray = NULL; // do not remember this step.
Pthis-> capacity = 0;
Pthis-> COUNT = 0;
}

Int array_get_capacity (struct darray * pthis)
{
Return pthis-> capacity;
}

Void array_init_array (struct darray * pthis, int size)
{
Int capacity;
Capacity = (size> 1 )? Size: 2; // The initial capacity of the array must be a multiple of 2.
Pthis-> capacity = capacity;
Pthis-> parray = (datatype *) malloc (sizeof (datatype) * capacity );
If (pthis-> parray = NULL)
{
Printf ("memory exhoust! /N ");
Exit (1 );
}
Pthis-> COUNT = 0;
Pthis-> init_array = array_init_array; // Initialize all function pointers.
Pthis-> insert = array_insert;
Pthis-> get_element = array_get_element;
Pthis-> change_size = array_change_size;
Pthis-> free = array_free;
Pthis-> get_capacity = array_get_capacity;
}

 

Int main ()
{
Struct darray array;
Array_init_array (& array, 2); // The initial size is a multiple of 2.
For (INT I = 0; I <1000; I ++)
{
If (I = array. get_capacity (& array ))
{
Array. change_size (& array, I <1); // If the array capacity is full, double the size.
}
Array. insert (& array, I, I );
}
Int trysize = array. get_capacity (& array); // array capacity.
Int num = 0;
Cout <"trysize:" <trysize <Endl;
For (Int J = 999; j>-1; j --)
{
Num = J + 1; // number of elements. is the cause of this error so careless? ? ? Otherwise, the pointer is not positioned with a star. You must pay attention to the variable name during code copy. You must be sure to think about where I used these variables.
While (Num <= trysize> 2 & trysize> 2)
{
Trysize> = 1;
Cout <"newsize:" <trysize <Endl;
}
If (trysize <array. get_capacity (& array ))
{
// Cout <"newsize:" <trysize <Endl;
Array. change_size (& array, trysize );
}
Cout <array. get_element (& array, j) <"";
}
Cout <Endl;
Return 0;
}

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.