2006年8月13日星期日

ADS Dynamic Array Using Structs完成!

上上个星期的功课……讲师要我们做的功课,只是要我们回答如下的题目,但是他说如果可以做成dynamic array的话更好。我花了一些时间研究怎样做dynamic array,过后因为懒惰,所以就放弃了。然后原本上个星期一应该把功课交上,可是因为碰巧敦马哈迪医生来给讲座,所以就取消原本的课。昨天晚上和今天心血来潮,在用心研究dynamic array,上去LYN问了问大家的意见,就做出来了。

/*
Write a program that will generate 5000 random numbers and store them into an
array. The program will then prompt the user for a number to search and will
display the number of times the searched number occurs in the array.
*/

#include
#include
#include

struct list_el {
int val;
struct list_el * next;
};

typedef struct list_el item;

int main() {
item * curr, * head;
int i;

head = NULL;

for(i=1;i<=10;i++) { curr = (item *)malloc(sizeof(item)); curr->val = i;
curr->next = head;
head = curr;
}

curr = head;

while(curr) {
printf("%d\n", curr->val);
curr = curr->next ;
}

getch();
return 0;
}


其实现在还有点乱,不过编程都是酱的啦,做多几次就自然而然地记得了。所以我一点也不担心。

没有评论: