I've been watching a bit of Hao bin data structure video every day since the holidays. It's very thorough and fun to talk about.
A few days ago is to tell the data structure to prepare, said some structure and pointers, today finally began to formally the data structure. To tell the truth, I didn't know the usefulness of the function today.
According to Hao bin the algorithm demonstration of continuous storage array, and wrote it again, found that there is a mistake, left to see the right to see what is wrong, simply posted,,, interested friends can see
Baidu help, a cow people see mistakes, thank you. Re-post the correct code
#include <stdio.h> #include <malloc.h> #include <stdlib.h>//include exit int val,i,t;
The struct ARR {int * pbase;//stores the address of the first element of the array int len;//The maximum number of elements the array can hold int cnt;////current array valid number}; void Init_arr (struct arr * parr,int length);
initialize bool Append_arr (struct arr * parr,int val); BOOL Insert_arr (struct arr * Parr,int pos,int val);
The value of POS starts with 1 bool Delete_arr (struct arr * parr,int pos,int *pval);
int get ();
BOOL Is_empty (struct ARR * pArr);
BOOL Is_full (struct ARR * pArr);
void Sort_arr (struct arr * pArr);
void Show_arr (struct arr * pArr); void Inversion_arr (struct arr * pArr);
Invert int main () {struct ARR arr;
Init_arr (&arr,6);
Show_arr (&arr);
Append_arr (&arr,1);
Append_arr (&arr,2);
Append_arr (&arr,3);
Append_arr (&arr,4);
Delete_arr (&arr,1,&val);
return 0;
} void Init_arr (struct arr * parr,int length) {parr->pbase = (int *) malloc (sizeof (int) * length); if (NULL = = parr->pbase) {printf ("Dynamic memory allocation failed.
\ n "); Exit (-1); Terminate entire program}
else {parr->len = length;
parr->cnt = 0;
} return;
} bool Is_empty (struct ARR * pArr) {if (0 = = parr->cnt) return true;
else return false;
} bool Is_full (struct ARR * pArr) {if (parr->cnt = = Parr->len) return true;
else return false;
} void Show_arr (struct arr * pArr) {if (Is_empty (PARR))//parr is originally the address {printf ("array is empty \ n");
} else {for (int i=0;i<parr->cnt;++i) printf ("%d", parr->pbase[i]);
printf ("\ n");
}} bool Append_arr (struct arr * Parr,int val) {if (Is_full (PARR)) return false;
else parr->pbase[parr->cnt] = val;
(parr->cnt) + +;
return true;
} bool Insert_arr (struct arr * Parr,int pos,int val) {int i; if (pos<1| |
Pos>parr->len) for (i=parr->cnt-1;i>=pos-1;--i) {parr->pbase[i+1] = parr->pbase[i];
} Parr->pbase[pos-1] = val;
return true;
} bool Delete_arr (struct arr * parr,int pos,int *pval) {if (Is_empty (PARR)) return false;
if (pos<1| | pos>parr->cnt) return false;
*pval = parr->pbase[pos-1];
for (I=pos; i<parr->cnt;i++) {parr->pbase[i-1] = parr->pbase[i];
} parr->cnt--;
return true;
} void Inversion_arr (struct arr * pArr) {int i = 0;
Int J = parr->cnt-1;
int t;
while (i<j) {t = parr->pbase[i];
Parr->pbase[i] = parr->pbase[j];
PARR->PBASE[J] = t;
i++;
j--;
} return;
} void Sort_arr (struct arr * pArr) {int i,j;//Bubbling Method for (i=0;i<parr->cnt;i++) {for (j=i+1;j<parr->cnt;i++)
{if (Parr->pbase[i] > Parr->pbase[j]) {t = parr->pbase[i];
Parr->pbase[i] = parr->pbase[j];
PARR->PBASE[J] = t;
}
}
}
}