Array sequence table

Array sequence table

  . 1 #include <stdio.h>
   2 #include <stdlib.h>
   . 3 #include < the malloc .h>
   . 4   
  . 5  struct Arr   
   . 6  {
   . 7      int * pBase;    // address of the first storage element in the array 
  . 8      int len;    / / maximum number of elements in the array can be accommodated 
  . 9      int CNT;    // this active element array 
10  };
 . 11  
12 is  void init_arr ( struct Arr Parr *, int length);
 13 is  BOOL append_arr ( struct Arr Parr *,int val);  //追加
 14 bool insert_arr(struct Arr * pArr,int pos,int val);  
 15 bool delet_arr(struct Arr * pArr,int pos,int *pVal);
 16 bool is_empty(struct Arr * pArr);
 17 bool is_full(struct Arr * pArr);
 18 void sort_arr(struct Arr * pArr);
 19 void show_arr(struct* ARR Parr);
 20 is  void inversion_arr ( struct ARR * Parr);
 21 is  
22 is   int main ()
 23 is  {
 24      struct ARR ARR;
 25      int Val;
 26 is      
27      the printf ( " -------- order table initialized - ------ \ n- " ); 
 28      init_arr (& ARR, . 6 );
 29      show_arr (& ARR);
 30      the printf ( " \ n-\ n- " );
 31 is      
32      the printf ( " -------- additional elements -------- \ n"); 
 33     append_arr(&arr,1);
 34     append_arr(&arr,2);
 35     append_arr(&arr,3);
 36     append_arr(&arr,4);
 37     append_arr(&arr,5);
 38     show_arr(&arr); 
 39     printf("\n\n");
 40     
 41     printf("--------插入元素--------\n");
 42     insert_arr(&arr,6,99);
 43     show_arr (& ARR);
 44 is      the printf ( " \ n-\ n- " );
 45      
46 is      the printf ( " -------- remove elements -------- \ n- " );
 47      IF (delet_arr ( & ARR, . 1 , & Val))
 48      {
 49          the printf ( " deleted successfully \ n-! " );
 50          the printf ( " you delete elements are:% D \ n- " , Val);
 51 is      } 
 52 is      the else 
53 is      {
 54 is          the printf ( " delete failed! \ the n- ");
 55      }
 56 is      show_arr (& ARR);
 57 is      the printf ( " \ n-\ n- " );
 58      
59      the printf ( " -------- inversion element -------- \ n- " );
 60      inversion_arr (& ARR);
 61 is      the printf ( " array contents are then inverted: \ n- " );
 62 is      show_arr (& ARR);
 63 is      the printf ( " \ n-\ n- " );
 64      
65      the printf ( " ---- ---- Sort -------- \ the n- " );
 66     sort_arr(&arr);
 67     show_arr(&arr);
 68     printf("\n\n");
 69     return 0;
 70 } 
 71 
 72 void init_arr(struct Arr * pArr,int length)
 73 {
 74     pArr->pBase = (int *)malloc(sizeof(int) * length);
 75     if(NULL == pArr->pBase)
 76     {
 77         printf(" Dynamic memory allocation failure \ n-! " );
 78          Exit (- . 1 );   // terminate the entire program 
79      }
 80      the else 
81      {
 82          pArr-> len = length;
 83          pArr-> CNT = 0 ;
 84      } 
 85      return ;
 86  }
 87  
88  BOOL is_empty ( struct Arr * Parr)
 89  {
 90      IF ( 0 == pArr-> CNT)
 91 is          return true;
 92     else
 93         return false;
 94 }
 95 
 96 bool is_full(struct Arr * pArr)
 97 {
 98     if(pArr->cnt == pArr->len)
 99         return true;
100     else
101         return false;
102 }
103 
104 void show_arr(struct Arr * pArr)
105 {
106     if( is_empty(pArr) )
107     {
108         printf("数组为空!\n");
109     }
110     else
111     {
112         for(int i=0;i<pArr->cnt;i++)
113         printf("%d ",pArr->pBase[i]);
114         printf("\n");
115     }
116 }
117 
118 bool append_arr(struct Arr * pArr,int val)
119 {
120     //满是返回false
121     if( is_full(pArr) ) 
122         return false;
123     pArr->pBase[pArr->cnt] = val;
124     pArr->cnt++;
125     return true;
126 }
127 
128 bool insert_arr(struct Arr * pArr,int pos,int val)
129 {
130     
131     if(is_empty(pArr) )
132         return false;
133     if(pos<1 || pos>pArr->len)
134         return false;
135     for(int i=pArr->cnt-1;i>=pos-1;--i)  
136     {
137         pArr->pBase[i+1] = pArr->pBase[i]; 
138     }
139     pArr->pBase[pos-1] = val;
140     (pArr->cnt)++;
141     return true;
142 } 
143 
144 bool delet_arr(struct Arr * pArr,int pos,int *pVal)
145 {
146     if( is_empty(pArr) )
147         return false;
148     if(pos<1 || pos>pArr->cnt)
149         return false;
150     *pVal = pArr->pBase[pos-1];
151     for(int i=pos;i<pArr->cnt;++i)
152     {
153         pArr->pBase[i-1] = pArr->pBase[i];
154     }
155     pArr->cnt--;
156     return true;
157 }
158 
159 void inversion_arr(struct Arr * pArr)
160 {
161     int i = 0;
162     int j = pArr->cnt-1;
163     int t;
164     while(i<j)
165     {
166         t = pArr->pBase[i];
167         pArr->pBase[i] = pArr->pBase[j];
168         pArr->pBase[j] = t;
169         ++i;
170         --j;
171     }
172     return;
173 }
174 
175 void sort_arr(struct Arr * pArr)
176 {
177     int i,j,t;
178     for(i=0;i< pArr->cnt ;++i)
179     {
180         for(j=i+1;i< pArr->cnt ;++j)
181         {
182             if(pArr->pBase[i] > pArr->pBase[j])
183             {
184                 t = pArr->pBase[i];
185                 pArr->pBase[i] = pArr->pBase[j];
186                 pArr->pBase[j] = t;
187             }
188         }
189     }
190 }

Guess you like

Origin www.cnblogs.com/aipeicai/p/12197099.html