Implementation of C-language analog integer array for data structure

Source: Internet
Author: User

#include <stdio.h>#include<malloc.h>#include<stdlib.h>typedefstructARR {int*pbase;//The first element address of the array    intLen//Array Length    intCnt//current number of active elements} Array;voidInit_array (Array *,int);//initializing an arrayvoidShow_array (array *);//traversing a printed arrayBOOLIs_empty (Array *);//determine if an array is emptyBOOLInsert_array (Array *,intPosint);//Insert element pos to insert in front of the first few elementsBOOLAppend_array (Array *,int);//adding elementsBOOLDel_array (Array *,intPosint* val);//Delete the first few elementsBOOLIs_full (Array *);//determine if the array is fullvoidSort_arr (Array * pArr);//Default AscendingvoidInversion_arr (structARR * PARR);//invert an arrayintMain () {Array A; Init_array (&a,Ten); Append_array (&a,0); Append_array (&a,1); Append_array (&a,2); Append_array (&a,3); Append_array (&a,4); Append_array (&a,5); Inversion_arr (&a); Show_array (&a);    GetChar ();  Free((&a)pbase);}voidInversion_arr (structARR *PARR) {    inti =0; intj = parr->cnt-1; inttemp =0;  while(I <j) {Temp= parr->Pbase[i]; PARR->pbase[i] = parr->Pbase[j]; PARR-&GT;PBASE[J] =temp; ++i; --J; }}voidSort_arr (Array *PARR) {    inttemp =0;  for(inti =0; I < parr->cnt; i++){         for(intj =0; J < parr->cnt-1I J + +){            if(Parr->pbase[j] > parr->pbase[j +1]) {temp= parr->Pbase[j]; PARR-&GT;PBASE[J] = parr->pbase[j +1]; PARR->pbase[j +1] =temp; }        }    }}BOOLDel_array (array * arr,intPosint*val) {    if(Is_empty (arr)) {printf ("Array is empty\n"); return false; }    if(Pos <1|| Pos>arr->CNT) {printf ("position is error \ n"); return false; }    *val = Arr->pbase[pos-1];  for(inti = pos; I < arr->cnt; i++) {arr->pbase[i-1] = arr->Pbase[i]; } arr->cnt--; return true;}BOOLAppend_array (array * arr,intval) {    if(Is_full (arr)) {printf ("Array is full"); return false; } arr-&GT;PBASE[ARR-&GT;CNT] =Val; Arr->cnt++; return true;}BOOLIs_full (Array *arr) {    returnarr->cnt = = arr->Len;}BOOLInsert_array (Array *arr,intPosintval) {    if(Is_full (arr)) {printf ("Array is full"); return false; }    if(Pos <1|| POS > arr->cnt +1) {printf ("position is error\n"); return false; }     for(inti = arr->cnt; I >= pos-1; i--) {arr->pbase[i] = arr->pbase[i-1]; } arr->pbase[pos-1] =Val; Arr->cnt++; return true;}voidInit_array (Array *arr,intLen) {arr->pbase = (int*)malloc(sizeof(int) *Len); if(NULL = = arr->pbase) {printf ("Memory allocation Failure"); Exit (-1); }    Else{arr->cnt =0; Arr->len =Len; }}BOOLIs_empty (Array *arr) {    return 0= = Arr->CNT;}voidShow_array (Array *arr) {    if(Is_empty (arr)) {printf ("array is empty and cannot be printed"); }    Else {         for(inti =0; I < arr->cnt; i++) {printf ("%d", arr->Pbase[i]); }    }}

Implementation of C-language analog integer array for data structure

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.