#include <stdio.h> #include <stdbool.h> #include <stdlib.h>//contains the Exit Function//defines a data type struct Arr{int * pbase;//stores the address of the first element of the array int len;//array can hold the largest number of elements int cnt;//The number of valid elements of the current array}; void Init_arr (struct arr *, int), bool Append_arr (struct arr * pArr, int val);//append bool Insert_arr (struct arr * pArr, int po s, int val); The value of//pos starts with a bool Delete_arr (struct arr * pArr, int pos, int * pval); int get (); bool Is_empty (struct arr * pArr) ; bool Is_full (struct arr * pArr); void Sort_arr (struct arr * pArr); void Show_arr (struct arr * pArr); void Inversion_arr (Stru CT arr * pArr); int main (void) {struct ARR arr;int Val;init_arr (&arr, 6); Show_arr (&arr); Append_arr (&arr, 1); Append_arr (&arr); Append_arr (&arr,-3); Append_arr (&arr, 6); Append_arr (&arr, n); Append_arr ( &arr, one); if (Delete_arr (&arr, 4, &val)) {printf ("Delete succeeded! \ n ");p rintf (" The element you deleted is:%d\n ", Val);} else {printf ("Delete failed! \ n ");} /*append_arr (&arr, 2); Append_arr (&arr, 3); Append_arr (&arr, 4); Append_arr (&arr, 5); Insert_arr (&arr, 6), Append_arr (&arr, 6), Append_arr (&arr, 7), if (Append_arr (&arr, 8)) {printf (" Append succeeded \ n ");} Else{printf ("Append failed!\n");} */show_arr (&arr); Inversion_arr (&arr);p rintf ("Array contents after inversion: \ n"); Show_arr (&arr); Sort_arr (&arr); Show_arr (&arr);//printf ("%d\n", Arr.len); return 0;} void Init_arr (struct arr *parr, int length) {parr->pbase = (int *) malloc (sizeof (int) *length); if (NULL = = Parr->pbase {printf ("Dynamic memory allocation failed!") \ n "); exit (-1);//Terminate entire program}else{parr->len = length;parr->cnt = 0;} return;} BOOL Is_empty (struct ARR * pArr) {if (0 = = parr->cnt) return true;else return 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))//NOTE here!! {printf ("The array is empty! \ n ");} Else{for (i = 0; i<parr->cnt; i++) printf ("%d", Parr->pbase[i]);//int *printf ("\ n");}} BOOL Append_arr (struct arr * pArr, int val) {//full is returned false if (Is_full (PARR)) return false;//is not satisfied when appending PARR->PBASE[PARR->CNT] = val; (parr->cnt) ++;return true;} BOOL Insert_arr (struct arr * pArr, int pos, int val) {int i;if (Is_full (PARR)) return false;if (pos<1 | | pos>parr-> cnt+1)//return false;for (i=parr->cnt-1; i>=pos-1;-I.) {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 * pval) {int i;if (Is_empty (PARR)) return false;if (pos<1 | | pos>parr-&g T;CNT) Return false;*pval = Parr->pbase[pos-1];for (i=pos; i<parr->cnt; ++i) {parr->pbase[i-1] =pArr-> Pbase[i];} Parr->cnt--;return true;} void Inversion_arr (struct arr * pArr) {int i = 0;int j = Parr->cnt-1;int T;while (i < j) {T = parr->pbase[i]; parr-& Gt;pbase[i] = parr->pbase[j]; PARR->PBASE[J] = t; ++i; --j;}} void Sort_arr (struct arr * pArr) {int I, j,t;for (i=0; i<parr->cnt; ++i) {for (j=i+1; j<parr->cnt; ++j) {if (parr- >pbase[i] > Parr->pbase[j]) {t = parr->pbase[i]; parr->pbase[i] = Parr->pbase[j]; PARR->PBASE[J] = t;}}}
An algorithm demonstration of Hao bin data structure continuous storage array