One of the major drawbacks of the array is that the length definition can no longer be changed, this program implements a dynamic array, similar to the structure of ArrayList in Java, has the function of increment, delete, sort, traverse, enlarge and append.
Implementation of dynamic arrays:
/*2013 February 16 19:18:35 This program appends, deletes, sorts, and iterates through the elements in the array. The method in Java is the same as the methods in Java. */# include <stdio.h># include <malloc.h># include <stdlib.h>struct arr{int * pbase;int len;int cnt;//i NT increment; Automatic growth factor, which facilitates the fast increase of array length and efficiency. The};//function declares void Init_arr (struct arr *); Creates and initializes a dynamic array of bool Is_empty (struct ARR *); Determines whether the array is empty bool Is_full (struct ARR *); Determines whether the array is full bool Changelen_arr (struct arr *); Change the length of the array bool Append_arr (struct arr *); Append array element bool Insert_arr (struct arr *); Insert array element bool Delete_arr (struct arr *); Delete array element int get (struct ARR *); Gets the array element void Sort_arr (struct arr *); sort void Show_arr (struct arr *); Traverse output void Inversion_arr (struct arr *); inverted int Find_val_arr (struct arr *); Look for the element that contains this value bool Delete_all_arr (struct arr *); Delete all elements that contain this value int main (void) {struct ARR Arr;init_arr (&arr); Show_arr (&arr); Changelen_arr (&arr); append_ Arr (&arr); Insert_arr (&arr);d Elete_arr (&arr); Show_arr (&arr); Inversion_arr (&arr); Show_arr ( &arr); Sort_arr (&arr);p rintf ("The result of sorting is: \ n "); Show_arr (&arr); Show_arr (&arr); return 0;} void Init_arr (struct arr * pArr) {int I;int length;parr->pbase = (int *) malloc (sizeof (int)); if (NULL = = parr->pbase) { printf ("Memory allocation failed!") \ n "); exit (-1);} printf ("Please enter the length of the node you need:"), scanf ("%d", &length);p arr->pbase = (int *) malloc (sizeof (int) *length);p Arr->len = length;parr->cnt = 0;for (i=0; i<length; ++i) {printf ("Please enter%d data:", i+1); scanf ("%d", &parr->pbase[i]);( PARR->CNT) + +;} return;} BOOL Is_empty (struct ARR * pArr) {if (parr->cnt = = 0) {printf ("Array is empty! \ n "); return true;} Elsereturn false;} BOOL Is_full (struct ARR * pArr) {if (parr->cnt = = Parr->len) {return true;} Elsereturn false;} void Show_arr (struct arr * pArr) {int i;if (Is_empty (PARR)) {printf ("The array is empty!") \ n "); return;} printf ("\ nthe data in array: \ n"), for (i=0; i<parr->cnt; ++i) {printf ("%d", Parr->pbase[i]);} printf ("\ n"); return;} BOOL Changelen_arr (struct arr * pArr) {int len;printf ("Please enter modified length: len ="), scanf ("%d", &len); ReAlloc (Parr->pbase, sizeof (int) *lEN);p Arr->len = len;if (NULL = = parr->pbase) {printf ("Memory reallocation failed! \ n "); return false;} Elsereturn true;} BOOL Append_arr (struct arr * pArr) {int val;if (Is_full (PARR)) {printf ("The array is full!") \ n "); return false;} else{printf ("Please enter data to append: val ="); scanf ("%d", &val);p arr->pbase[parr->cnt] = val; (parr->cnt) ++;return true;}} BOOL Insert_arr (struct arr * pArr) {int I, T;int pos, Val;if (Is_full (PARR)) {printf ("The array is full and cannot be inserted!") \ n "); return false;} input:printf ("Please enter a location to insert: pos ="), scanf ("%d", &pos);p rintf ("Enter data to be inserted: val ="); scanf ("%d", &val); if (Pos < 1 | | POS > Parr->cnt+1) {printf ("Input insertion position is incorrect!) \ n "); goto input;} For (i=parr->cnt-1; i<pos-1;-i)//insert and delete elements require a cyclic displacement and are inefficient. {parr->pbase[i+1] = parr->pbase[i];} Parr->pbase[pos-1] = val; (parr->cnt) ++;return true;} BOOL Delete_arr (struct arr * pArr) {int pos;int i;int val;if (Is_empty (PARR)) {printf ("The array is empty and cannot be deleted!") "); return false;} printf ("Please enter the location of the element you need to delete:"); scanf ("%d", &pos); val = parr->pbase[pos-1];for (i=pos; i<parr->cnt-1; ++i)//INSERT and delete elements require a cyclic displacement and are inefficient. {Parr->pbase[i-1] = parr->pbase[i];} printf ("Delete succeeded, the element you deleted is:%d", Val); return true;} int get (struct ARR * pArr) {int pos;printf ("Enter the position of the element you need to get:"), scanf ("%d", &pos);p rintf ("element data:%d\n", parr->pbase [Pos-1]); return parr->pbase[pos-1];} Bubble Sort method void Sort_arr (struct arr * pArr) {int I, J, T;for (i=0; i<parr->cnt; ++i) for (j=0; j<parr->cnt-1-i; ++j) {if (Parr->pbase[j] > Parr->pbase[j+1]) {t = Parr->pbase[j];p arr->pbase[j] = parr->pbase[j+1];p arr- >pbase[j+1] = t;}} return;} void Inversion_arr (struct arr * pArr) {int I, j, t;i = 0;j = Parr->cnt;while (i < J) {Parr->pbase[i] = PARR->PBA Se[j];++i;--j;} return;} int Find_val_arr (struct arr * pArr) {int I;int val;int count = 0;printf ("Enter the value of the element you want to find: val ="); scanf ("%d", &val); for (i = 0; i<parr->cnt; ++i) {if (val = = Parr->pbase[i]) {printf ("element subscript is:%d", i); count++;}} printf ("Number of values with this element is:%d", count); return count;} BOOL Delete_all_arr (struct arr * pArr) {int I, j;int Val;int count = 0;if (Is_empty (PARR)) {printf ("The array is empty and cannot be deleted!") "); return false;} printf ("Please enter the value of the element to be deleted: val ="); scanf ("%d", &val); for (i=0; i<parr->cnt; ++i) {if (val = parr->pbase[i]) {for (j=i; j<parr->cnt-1; ++j) {parr->pbase[j-1] = parr->pbase[j];} printf ("Delete succeeded! "); count++;(p arr->cnt)--;}} printf ("Total number of deleted elements:%d", count); return true;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Getting started with data structures-dynamic arrays