数组顺序表

数组顺序表

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<malloc.h>
  4  
  5 struct Arr   
  6 {
  7     int *pBase;   //存储数组第一个元素的地址
  8     int len;   //数组所能容纳的最大元素个数 
  9     int cnt;   //当前数组有效元素 
 10 };
 11 
 12 void init_arr(struct Arr * pArr,int length);
 13 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 void inversion_arr(struct Arr * pArr);
 21 
 22  int main()
 23  {
 24     struct Arr arr;
 25     int val;
 26     
 27     printf("--------顺序表初始化--------\n"); 
 28     init_arr(&arr,6);
 29     show_arr(&arr);
 30     printf("\n\n");
 31     
 32     printf("--------追加元素--------\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     printf("\n\n");
 45     
 46     printf("--------删除元素--------\n");
 47     if( delet_arr(&arr,1,&val) )
 48     {
 49         printf("删除成功!\n");
 50         printf("您删除的元素是:%d\n",val);
 51     } 
 52     else
 53     {
 54         printf("删除失败!\n");
 55     }
 56     show_arr(&arr);
 57     printf("\n\n");
 58     
 59     printf("--------倒置元素--------\n");
 60     inversion_arr(&arr);
 61     printf("倒置之后的数组内容是:\n");
 62     show_arr(&arr);
 63     printf("\n\n");
 64     
 65     printf("--------排序--------\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("动态内存分配失败!\n");
 78         exit(-1);  //终止整个程序 
 79     }
 80     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         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 }

猜你喜欢

转载自www.cnblogs.com/aipeicai/p/12197099.html