4.线性结构-数组【郝斌数据结构】

模块一:线性结构【把所有的结点用一根直线穿起来】

连续存储【数组】

 例题:动态数组实例

 

  1 # include <stdio.h>
  2 # include <malloc.h>//包含malloc函数
  3 # include <stdlib.h>//包含exit函数
  4 //定义了一个数据类型,该数据类型的名字叫做struct Arr,该数据类型含有三个成员,分别是pBase, len, cnt
  5 struct Arr
  6 {
  7     int * pBase;//存储的是数组的第一个元素的地址
  8     int len;//数组所能容纳的最大元素的个数
  9     int cnt;//当前数组有效元素的个数
 10 };
 11 void init_arr(struct Arr * pArr, int length);
 12 bool append_arr(struct Arr * pArr, int val);//追加
 13 bool insert_arr(struct Arr * pArr, int pos, int val);//pos的值从1开始
 14 bool delete_arr(struct Arr * pArr, int pos, int * pval);
 15 bool get();
 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(void)
 23 {
 24     struct Arr arr;//结构体变量的声明,分配结构体变量的内存空间
 25     int val;
 26     //修改主调函数中结构体变量成员的值必须要发送结构体变量的地址给被调函数
 27     init_arr(&arr, 6);
 28     show_arr(&arr);
 29 //    printf("%d\n",arr.len);
 30 //    printf("%p\n",arr.pBase);
 31     append_arr(&arr, 1);
 32     append_arr(&arr, 10);
 33     append_arr(&arr, -3);
 34     append_arr(&arr, 6);
 35     append_arr(&arr, 88);
 36     append_arr(&arr, 11);
 37     if (delete_arr(&arr, 2, &val))
 38     {
 39         printf("删除成功!\n");
 40         printf("删除的元素是:%d\n", val);
 41     }
 42     else
 43     {
 44         printf("删除失败!");
 45     }
 46 /*    if (append_arr(&arr, 6))
 47         {
 48             printf("追加成功!\n");
 49         }
 50         else
 51         {
 52             printf("追加失败!\n");
 53         }
 54 */
 55     inversion_arr(&arr);
 56     sort_arr(&arr);
 57     show_arr(&arr);
 58     printf("当前数组元素个数:%d\n", arr.cnt);
 59     return 0;
 60 }
 61 //初始化数组
 62 void init_arr(struct Arr * pArr, int length)//结构体变量可以相互赋值
 63 {
 64     pArr->pBase = (int *)malloc(sizeof(int) *length);
 65     if (NULL == pArr->pBase)
 66     {
 67         printf("动态内存分配失败!\n");
 68         exit(-1);//终止整个程序
 69     }
 70     else
 71     {
 72         pArr->len = length;
 73         pArr->cnt = 0;
 74     }
 75     return;    
 76 }
 77 //判断数组是否为空
 78 bool is_empty(struct Arr * pArr)
 79 {
 80     if (0 == pArr->cnt)
 81         return true;
 82     else
 83         return false;
 84 }
 85 //显示数组元素
 86 void show_arr(struct Arr * pArr)
 87 {
 88     if (is_empty(pArr))
 89     {
 90         printf("数组为空!\n");        
 91     }    
 92     else
 93     {
 94         for (int i=0; i<pArr->cnt; ++i)
 95             printf("%d ",pArr->pBase[i]);
 96         printf("\n");
 97     }    
 98 } 
 99 //判断数组是否为满
100 bool is_full(struct Arr * pArr)
101 {
102     if (pArr->cnt == pArr->len)
103         return true;
104     else 
105         return false;    
106 }
107 //追加数据
108 bool append_arr(struct Arr * pArr, int val)
109 {
110     //满是返回false
111     if(is_full(pArr))
112         return false;
113     //不满时追加
114     else
115     {
116         pArr->pBase[pArr->cnt] = val;
117         ++(pArr->cnt);
118         return true;
119     }
120 }
121 //插入元素,pos从1开始
122 bool insert_arr(struct Arr * pArr, int pos, int val)
123 {
124     int i;
125     if (is_full(pArr))
126         return false;
127     if (pos<1 || pos>(pArr->cnt)+1)
128         return false;
129     for (i=pArr->cnt-1; i>=pos-1; --i)
130     {
131         pArr->pBase[i+1] = pArr->pBase[i];
132     }
133     pArr->pBase[pos-1] = val;
134     (pArr->cnt)++;
135     return true;
136 }
137 //删除数组中某个位置上的值
138 bool delete_arr(struct Arr * pArr, int pos, int * pval)
139 {
140     if(is_empty(pArr))
141         return false;
142     if(pos<1 || pos>pArr->cnt )
143         return false;
144     *pval = pArr->pBase[pos-1];
145     for (int i=pos; i<=pArr->cnt; ++i)
146     {
147         pArr->pBase[i-1] = pArr->pBase[i];    
148     }
149     (pArr->cnt)--;
150     return true;
151 }
152 //数组元素倒置
153 void inversion_arr(struct Arr * pArr)
154 {
155     int i=0;
156     int j=pArr->cnt-1;
157     int t;
158     while (i<j)
159     {
160         t=pArr->pBase[i];
161         pArr->pBase[i] = pArr->pBase[j];
162         pArr->pBase[j] = t;
163         ++i;
164         --j;
165     }
166     return;
167 }
168 //数组元素冒泡排序
169 void sort_arr(struct Arr * pArr)
170 {
171     int i, j, t;
172     for (i=0; i<pArr->cnt-1; ++i)
173     {
174         for (j=0; j<pArr->cnt-1-i; ++j)
175                 {
176                     if (pArr->pBase[j] > pArr->pBase[j+1])
177                         {
178                             t = pArr->pBase[j];
179                             pArr->pBase[j] = pArr->pBase[j+1];
180                             pArr->pBase[j+1] = t;
181                         }
182                 }
183     }        
184     return;
185 }

 

离散存储【链表】

 

线性结构的两种常见应用之一 栈

线性结构的两种常见应用之一 队列

posted @ 2024-08-25 20:27  java帝国  阅读(9)  评论(0)    收藏  举报