(001) data structure (struct definition, typedef)

Source: Internet
Author: User

After reading the data structure of Mr. Haobin, I am very touched. So I want to write a series of articles about the data structure-whether you simply learn the data structure, for the purpose of preparing for the preliminary test and review of the data structure of the postgraduate entrance exam, I believe you can all benefit from this series of blog posts.

Environment: windows xp (x86), vc ++ 6.0

Intention: If you want to learn the data structure well, you must be familiar with the structures, pointers, and typedef. Therefore, after referring to the data structure of Mr. Haobin, he has completed his hands-on program and hopes to lay a solid foundation before he officially starts studying the linked list of data structures.

The program below is mainly used to serve the above purpose. I hope you will be able to benefit from it, and I hope you can leave a message and communicate with me.

# Include <stdio. h> # include <malloc. h>/* Simple Description: Simulate the creation of an int-type array with a given length when the initial array is full. Inspiration: Modify the data structure from instructor Haobin, instructor Haobin's int array cannot be auto-incrementing. Author: leonard's Writing Time: March 23, 2014 */typedef struct Arr {int * pBase; // pointer to the first address of the int array int length; // The maximum length of the current array int cnt; // The number of current elements int increment; // The increment of the array} Arr; void init_arr (Arr * arr, int len ); void selfincrese_arr (Arr * arr); // when the array of the int type pointed to by arr is full, the auto-increment length bool append_arr (Arr * arr, int value) is given ); // append the value directly to bool insert_arr at the end of the array (Arr * arr, int pos, int value); // Add the value to the end of the array at the specified pos position. pos starts from 0: bool delete_arr (Arr * arr, int pos, int * dvalue); // Delete the data at the specified pos position in the array, and return the data to bool isempty (Arr * arr); bool isfull (Arr * arr ); void show_arr (Arr * arr); // print all elements of the int array in sequence void inverse_arr (Arr * arr ); // The int main (void) {Arr arr; int dtemp; init_arr (& arr, 6); append_arr (& arr, 2 ); append_arr (& arr, 5); append_arr (& arr, 7); append_arr (& arr, 4); append_ar R (& arr, 1); append_arr (& arr, 10); append_arr (& arr, 88); printf ("insert an array \ n "); show_arr (& arr); insert_arr (& arr, 3,99); printf ("inserted array \ n"); show_arr (& arr ); printf ("array \ n" after an element is deleted); delete_arr (& arr, 5, & dtemp); printf ("the deleted data is % d \ n ", dtemp); show_arr (& arr); inverse_arr (& arr); printf ("\ n" after array inversion); show_arr (& arr); return 0 ;} void init_arr (Arr * arr, int len) {if (arr-> pBase = (int *) malloc (sizeof (int) * len )) {printf ("array initialization successful... \ n "); ar R-> length = len; arr-> increment = 5; arr-> cnt = 0;} elseprintf ("dynamic memory allocation failed... \ n ");} bool isfull (Arr * arr) {if (arr-> length = arr-> cnt) return true; elsereturn false ;} bool isempty (Arr * arr) {if (0 = arr-> cnt) return true; elsereturn false;} void show_arr (Arr * arr) {for (int I = 0; I <arr-> cnt; I ++) {printf ("a [% d] = % d \ n", I, arr-> pBase [I]);} printf ("\ n");} // expand void selfincrese_arr (Arr * arr) {int * temp = (int *) ma according to the previous conventions. Lloc (sizeof (int) * arr-> length); for (int j = 0; j <arr-> length; j ++) {* (temp + j) = arr-> pBase [j];} arr-> pBase = (int *) malloc (sizeof (int) * (arr-> length + = arr-> increment )); for (int k = 0; k <arr-> length; k ++) {arr-> pBase [k] = * (temp + k );}} // Insert the new element bool append_arr (Arr * arr, int value) {if (isfull (arr) {selfincrese_arr (arr); printf ("the initial array is full, % d blank positions have been added, and the maximum number of elements has been expanded to % d \ n ", arr-> increment, arr-> length); append_arr (arr, value );} Else {arr-> pBase [arr-> cnt] = value; (arr-> cnt) ++;} return true ;} // Add an element with the value after the pos position. The pos starts from 0 and bool insert_arr (Arr * arr, int pos, int value) {if (pos <0 | pos> arr-> length | pos> arr-> cnt) {printf ("insertion cannot be completed because the location parameter is incorrect! \ N "); return false;} else {for (int l = arr-> cnt; l> = pos + 1; l --) {arr-> pBase [l] = arr-> pBase [L-1];} arr-> pBase [pos + 1] = value; (arr-> cnt) ++; return true ;}} // Delete the element at the pos position, and return the value of the deleted element to the pos from 0. bool delete_arr (Arr * arr, int pos, int * dvalue) {* dvalue = arr-> pBase [pos]; for (int m = pos; m <arr-> cnt; m ++) {arr-> pBase [m] = arr-> pBase [m + 1];} arr-> cnt --; return true ;} // reverse the specified int type array void inverse_arr (Arr * arr) {int p = 0, q = arr-> cnt-1, itemp; while (p <q) {itemp = arr-> pBase [q]; arr-> pBase [q] = arr-> pBase [p]; arr-> pBase [p] = itemp; p ++, q --;}}

Related Article

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.