数据结构:连续存储数组的算法

  1 # include<stdio.h>
  2 # include<malloc.h>
  3 # include<stdlib.h>
  4 
  5 struct pArr
  6 {
  7 
  8     int *pBase;
  9     int len;
 10     int cnt;
 11 };
 12 void init(struct pArr *arr,int length);
 13 void show(struct pArr *arr);
 14 bool is_empty(struct pArr *arr);
 15 bool is_full(struct pArr *arr);
 16 bool append(struct pArr *arr,int val);
 17 bool insert(struct pArr *arr,int pos,int val);
 18 bool delet(struct pArr *arr,int pos,int *val);
 19 void daozhi(struct pArr *arr);
 20 void sort(struct pArr *arr);
 21 int main(void)
 22 {
 23     int val;
 24     struct pArr ac;
 25 
 26     init(&ac,6);
 27     append(&ac,45);
 28     append(&ac,11);
 29     append(&ac,22);
 30 
 31     append(&ac,651);
 32     append(&ac,231);
 33 
 34     printf("追加的数据为:\n");
 35     show(&ac);
 36     printf("插入后的数据结果为:\n");
 37     insert(&ac,2,999);
 38     show(&ac);
 39     delet(&ac,4,&val);
 40     printf("删除后的数据结果为:\n");
 41     show(&ac);
 42     printf("删除的数据为为:\n");
 43 
 44     printf("%d\n",val);
 45     daozhi(&ac);
 46         
 47     printf("倒置后的数据结果为:\n");
 48     show(&ac);
 49     sort(&ac);
 50     printf("排序后的数据结果为:\n");
 51     show(&ac);
 52     return 0;
 53 }
 54 
 55 void init(struct pArr *arr,int length)
 56 {
 57     arr ->pBase = (int *)malloc(sizeof(int) * length);
 58     if(NULL == arr->pBase)
 59     {    printf("动态内存分配失败\n");
 60     exit(-1);
 61     }
 62     else
 63     {
 64         arr->len = length;
 65         arr->cnt = 0;
 66     }
 67     return;
 68 }
 69 bool is_empty(struct pArr *arr)
 70 {
 71     if(0 == arr->cnt)
 72         return true;
 73     else
 74         return false;
 75     
 76 }
 77 void show(struct pArr *arr)
 78 {
 79     if(is_empty(arr))
 80         printf("为空!\n");
 81     else
 82     {
 83         for(int i=0;i<arr->cnt;i++)
 84              printf("%d ",arr->pBase[i]);
 85         printf("\n");
 86     }
 87 }
 88 bool is_full(struct pArr *arr)
 89 {
 90     if( arr ->cnt == arr ->len)
 91         return true;
 92     else
 93         return false;
 94 }
 95 bool append(struct pArr *arr,int val)
 96 {
 97     if( is_full(arr) )
 98         return false;
 99     else
100         arr->pBase[arr->cnt] = val;
101     (arr->cnt)++;
102     return true;
103 }
104 bool insert(struct pArr *arr,int pos,int val)
105 {
106     int i;
107     if(is_full(arr))
108         return false;
109     if(pos <1 ||pos>arr->cnt+1)
110         return false;
111     for(i=arr->cnt;i>=pos-1;i--)
112     {
113         arr->pBase[i+1] = arr->pBase[i];
114     }
115     arr->pBase[pos-1] = val;
116     arr->cnt++;
117     return true;
118 }
119 bool delet(struct pArr *arr,int pos,int *val)
120 {
121     int i;
122     if(is_empty(arr))
123         return false;
124     if(pos<1 || pos>arr->cnt)
125         return false;
126     *val= arr->pBase[pos-1];
127     for(i=pos-1;i<=arr->cnt-1;i++)
128     {
129         arr->pBase[i] = arr->pBase[i+1];
130     }
131     arr->cnt--;
132     return true;
133    
134 }
135 void daozhi(struct pArr *arr)
136 {
137     int i = 0;
138     int j = arr->cnt-1;
139     
140     int t;
141 while(i < j)
142 {
143 
144     t = arr->pBase[i];
145     arr->pBase[i] = arr ->pBase[j];
146     arr->pBase[j] = t;
147         i++;
148         j--;
149 }
150 
151 
152 }
153 void sort(struct pArr *arr)
154 {
155     int i,j;
156     int max;
157 
158     for(i=0;i<arr->cnt-1;i++)
159     {
160         for(j=i+1;j<arr->cnt;j++)
161         {    if(arr->pBase[i]>arr->pBase[j])
162             {
163                 
164                     max = arr->pBase[i];
165                     arr->pBase[i] = arr ->pBase[j];
166                     arr->pBase[j] =max;
167             }
168         }
169     }
170 }
171 /*
172 在VC++6.0中运行结果是:
173 ==========================================
174 追加的数据为:
175 45 11 22 651 231
176 插入后的数据结果为:
177 45 999 11 22 651 231
178 删除后的数据结果为:
179 45 999 11 651 231
180 删除的数据为为:
181 22
182 倒置后的数据结果为:
183 231 651 11 999 45
184 排序后的数据结果为:
185 11 45 231 651 999
186 Press any key to continue
187 =========================================
188 */

posted on 2012-11-06 08:14  Your Song  阅读(269)  评论(0编辑  收藏  举报

导航