C Code Implementation Array

Source: Internet
Author: User

Directly on the code, there's nothing to say

1# include <stdio.h>2# include <malloc.h>3# include <stdlib.h>4 5 //implementing arrays with C6 7typedefstructarray{8     intLen//the length of the array9     int* PHEAD;//Array First addressTen     intCnt//array number of valid elements One}*Parr,arr; A  - BOOLInsert (PARR arr,intIndexintnum); - BOOLAdd (PARR arr,intnum); the BOOLIsEmpty (PARR arr); - voidprint (PARR arr); - voidInitarray (PARR arr,intlen); - BOOLisfull (PARR arr); + BOOLDeletearr (PARR arr,intindex); - voidsort (PARR arr); +  A intMainvoid) at { - arr arr; -Initarray (&arr,5); -     if(IsEmpty (&arr)) { -printf"array is empty \ n"); -     } in      -Add (&arr,111); toAdd (&arr,2); +Add (&arr, -); -Add (&arr,4); theAdd (&arr,5); * //Add (&arr,1); $ //Add (&arr,1);Panax NotoginsengPrint (&arr); -Sort (&arr); theDeletearr (&arr,3); +Print (&arr); A      the     return 0; + } -  $ //initializing an array $ voidInitarray (PARR arr,intlength) { -     //Request Memory -Arr->phead = (int*)malloc(sizeof(int)*length); the     if(null==arr->phead) { -printf"failed to request memory");WuyiExit (-1); the     } -Arr->len =length; Wuarr->cnt =0; - }  About   $ //iterating through an array - voidprint (PARR arr) { -     if(IsEmpty (arr)) { -printf"array is empty"); A     } +     //For Loop Print set the      for(intI=0; i<arr->cnt; i++){ -printf"%d",arr->phead[i]); $     } theprintf"\ n"); the      the } the   - //to interpret whether an array is empty in BOOLIsEmpty (PARR arr) { the     returnarr->cnt==0; the } About  the //determines whether the array is full the BOOLisfull (PARR arr) { the     returnArr->cnt==arr->Len; + } -  the //adding elements to an arrayBayi BOOLAdd (PARR arr,intnum) { the     if(Isfull (arr)) { theprintf"array is full"); -         return false; -     } the      theARR-&GT;PHEAD[ARR-&GT;CNT] =num; thearr->cnt++; the     return true; - } the  the //inserting elements into an array the BOOLInsert (PARR arr,intIndexintnum) {94     if(Isfull (arr)) { the         return false; the     } the     if(index<1|| index>arr->cnt+1){98         return false; About     } -     101     //move the number after index-1.102      for(inti=arr->cnt-1; i>=index-1; i--){103arr->phead[i+1] = arr->Phead[i];104     } the     106arr->phead[index-1] =num;107arr->cnt++;108     return true;109 } the 111 //deletes the element at the specified position the BOOLDeletearr (PARR arr,intindex) {113     if(IsEmpty (arr)) { the         return false; the     } the     if(index<1|| index>arr->cnt+1){117         return false;118     }119      -      for(intI=index; i<arr->cnt;i++){121arr->phead[i-1] = arr->Phead[i];122     }123arr->cnt--;124     return true; the }126 127 //Sort - voidsort (PARR arr) {129       the      intI,j,tem;131      for(i=0; i<arr->cnt-1; i++) { the          for(j=0; j<arr->cnt-1-i;j++){133             if(arr->phead[j]>arr->phead[j+1]){134TEM = arr->Phead[j];135ARR-&GT;PHEAD[J] = arr->phead[j+1];136arr->phead[j+1] =tem;137             }138         }139     } $ } 141  

C Code Implementation 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.