工作以后,一直使用C++,也做过Objective C,各种类的方法封装得很好,使用很简单,今天偶尔翻看一下 严蔚敏 的 《数据结构》,第一个程序demo就看了半天,一是由于demo的变量命名问题,全是i,m,n,p什么的;二就是对指针的使用生疏了。现在把改写的demo记录一下。。。
1: #include <stdio.h>
2: #include <stdlib.h>
3:
4: void Invert(int *pArray, int nCount) {
5: int *pHead = pArray;
6: int *pTail = pArray + nCount - 1;
7: int iTemp = 0;
8: while(pHead < pTail) {
9: iTemp = *pTail;
10: *pTail = *pHead;
11: *pHead = iTemp;
12: pHead++;
13: pTail--;
14: }
15: pHead = NULL;
16: pTail = NULL;
17: }
18:
19: int main(int argc, char *argv[]) {
20: int nCount = 0;
21: printf(" Enter nCount: ");
22: scanf("%d", &nCount);
23: int *pArray = malloc(sizeof(int) * nCount);
24: int *pCounter = pArray;
25: printf(" Input the original array: ");
26: int i = 0;
27: for(; i<nCount; i++) {
28: scanf("%d", pCounter++);
29: }
30: printf(" The original array is: ");
31: pCounter = pArray;
32: for(i=0; i<nCount; i++) {
33: printf("%d ", *(pCounter++));
34: }
35: Invert(pArray, nCount);
36: printf(" The inverted array is: ");
37: for(i=0; i<nCount; i++) {
38: printf("%d ", *(pArray++));
39: }
40: pCounter = NULL;
41: free(pArray);
42: pArray = NULL;
43: return 0;
44: }