An algorithm demonstration of Hao bin data structure continuous storage array

Source: Internet
Author: User


#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-&GT;PBASE[PARR-&GT;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-&GT;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-&GT;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-&GT;PBASE[J] = t;}}}

  

An algorithm demonstration of Hao bin data structure continuous storage array

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.