Data structure (array manipulation) C language implementation

Source: Internet
Author: User

# include<stdio.h># include<malloc.h># include<stdlib.h>structarr{int*pbase;//The address of the first element of the array is stored    intLen//The maximum number of elements the array can hold    intCnt//the number of valid elements in the current array};voidInit_arr (structARR *parr,intlength);BOOLAppend_arr (structARR *parr,intvalue);BOOLInsert_arr (structARR *parr,intPosintvalue);BOOLDelete_arr (structARR *parr,intPosint*pVal);BOOLIs_empty (structARR *PARR);BOOLIs_full (structARR *PARR);voidSort_arr (structARR *PARR);voidShow_arr (structARR *PARR);voidInversion_arr (structARR *PARR);intMainvoid){    structarr arr; intVal; Arr.len=5; Init_arr (&Arr,arr.len); Show_arr (&arr); Append_arr (&arr,1); Show_arr (&arr); Delete_arr (&arr,1,&val); return 0;}voidInit_arr (structARR *parr,intlength) {PARR->pbase= (int*)malloc(sizeof(int)*length); if(parr->pbase=NULL) {printf ("dynamic memory allocation failure"); Exit (-1); }    Else{PARR->len=length; PARR->cnt=0; }    return;}BOOLIs_empty (structARR *PARR) {    if(parr->cnt==0)        return true; Else        return false;}BOOLIs_full (structARR *PARR) {    if((parr->cnt) = = (parr->len)) return true; Else        return false;}voidShow_arr (structARR *PARR) {    inti; if(Is_empty (PARR))//why not &parr, because PARR is the address itself?{printf ("array is empty \ n"); }    Else    {         for(i=0; i<parr->cnt;i++) {printf ("%d",parr->Pbase[i]); }    }    }BOOLAppend_arr (structARR *parr,intvalue) {    if(Is_full (PARR)) {return false; }    Else{PARR->pbase[parr->cnt]=value; (PArr-&GT;CNT) + +; return true; }}BOOLInsert_arr (structARR *parr,intPosintvalue) {    inti; 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]=value; (PArr-&GT;CNT) + +; return true;}BOOLDelete_arr (structARR *parr,intPosint*pVal) {    inti; if(Is_empty (PARR))return false; if(pos<1|| Pos>parr->CNT)return false; *pval=parr->pbase[pos-1];  for(i=pos;i<parr->cnt;i++) {PARR->pbase[i-1]=parr->Pbase[i]; } (PARR-&GT;CNT)--;}voidInversion_arr (structARR *PARR) {    inti; intj=parr->cnt-1; intT;  while(i<j) {T=parr->Pbase[i]; PARR->pbase[i]=parr->Pbase[j]; PARR->pbase[j]=T; ++i; --J; }    return;}voidSort_arr (structARR *PARR) {    inti,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; }        }    }}

Data structure (array manipulation) C language implementation

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.