说成难以置信,简直难以置信.因为我这种能力的人,居然还敢说难以置信.真的难以置信.
这个东西,写了大约6小时?我本想更快的,但结构却跟我想得相差甚多.
原本以为自己的指针使用得炉火纯青了,但这么说话确实显得没什么程度.我对内存不熟悉,怎么能深刻地理解指针呢?不过我还是不担心,因为我的书就快看到内存部分了,呵呵.
我想精简操作,并没有像写ADT那样写出那么多分工那么明确的函数,而是强调快速,高效.无论是对机器,还是对我.
我的思路,比较聪明吧.因为我目前还没学很多东西,呵呵.这算是对我自己的嘲笑吗?主要的思路就是几个关键字概括吧:链表, 哈希表.
卡壳的主要地方,是在为链表分配空间的时候.我第一次感受到了内存的巨大压力,我试图一次分配一大块连续的空间,这样就能减少过程调用了.可事实上,老是没有足够的栈空间归我调用.于是,我更改了,放弃了这个提高效率的想法.再一个,对于表的大小.我的思路是,创建一个表的大小为相乘后最大指数+1,但开始我并没有这么做.后来,发现了.但我确实发现,我确实浪费了很多空间.不过,没办法了,当初就是这么设计的,中途更改将花费大量的工时,于是我向我的完美低头了,为了结果,我妥协了.当然,我最终是实现了的.其次,就是一些看似不起眼的细节问题.
最部分代码上,我察觉到我对链表的理解不错,至少没白学.其次,最主要的我想说的是,我盲目地想要优化他的性能.这带来的损失是惨痛的.最近在看优化程序性能的的书,于是我给用上了.对于这个问题,首先,我对程序的优化能力还不强.其次,我不应该在构造程序的过程中尝试优化.这样带来的效果就是,程序难写,分工不明确.优化,应该在写完程序之后才进行.这么做,优化操作简单,而且不破坏模块性.的确是很聪明的做法.而我的做法,实在是太愚蠢了.再一个,通过一个其他的问题,我知道,我不该依赖F7来帮助我检测错误.这样只会害了我,F7,应该是确认无误之后才去做的事.养成好的习惯,有助于我成长.因为我坚信,我,要么成功,要么走火入魔.因为,我不会放弃这条路.这是我人生仅存的理想了.
好吧,贴出我的代码.
/*5-7-10-24-00.29 -- 第五章第七题*/
#include
#include
#include
#define USED 1
#define LEISURE 0
typedef int Item ;
typedef struct node
{
Item coefficent ;/*系数*/
Item power ;/*指数*/
struct node * next ;
} Node ;/*结点,可用作多项式链表结点和表的元素*/
typedef struct linkedlist
{
Node * noumenon ;/*指向结点的指针*/
int sum_of_power ;/*指数和*/
} * LinkedList ;
typedef struct cell
{
Item coefficent ;
Item power ;
int condition ;
} Cell ;
typedef struct hashtable
{
int size ;/*表的大小*/
Cell * cell ;/*指向元素的指针*/
} * HashTable ;
int main (void) ;
int hash (const int key, const int size) ;
int get_random_coefficent (const int max) ;
int get_random_power (const int max) ;
LinkedList create_list (const int lenth, const int c_max, const int p_max) ;
HashTable create_table (const int size) ;
void print_multinomial (Node * const multinomial) ;
void print_table (const HashTable * const ph) ;
void Release_list (const LinkedList * const pl) ;
void Release_table (const HashTable * const ph) ;
int main (void)
{
LinkedList m1, m2 ;
Node * scan_m1, * scan_m2 ;
Cell * temp ;
HashTable h ;
int lenth_1, lenth_2, c_max_1, c_max_2, p_max_1, p_max_2, table_size, key ;
lenth_1 = 7 ;/*多项式1项数*/
c_max_1 = 50 ;/*多项式1最高系数*/
p_max_1 = 12 ;/*多项式1最高指数*/
lenth_2 = 9 ;/*多项式2项数*/
c_max_2 = 12 ;/*多项式2最高系数*/
p_max_2 = 7 ;/*多项式2最高指数*/
m1 = create_list (lenth_1, c_max_1, p_max_1) ;
m2 = create_list (lenth_2, c_max_2, p_max_2) ;
table_size = p_max_1 + m2 -> sum_of_power + 1 ;
h = create_table (table_size) ;
for (scan_m1 = m1 -> noumenon; scan_m1 != NULL; scan_m1 = scan_m1 -> next)
{
for (scan_m2 = m2 -> noumenon; scan_m2 != NULL; scan_m2 = scan_m2 -> next)
{
scan_m1 -> coefficent *= scan_m2 -> coefficent ;
scan_m1 -> power += scan_m2 -> power ;
key = hash (scan_m1 -> power, table_size) ;
temp = &h -> cell[key] ;
if (LEISURE == temp -> condition)
{
temp -> coefficent = scan_m1 -> coefficent ;
temp -> power = scan_m1 -> power ;
temp -> condition = USED ;
}
else
temp -> coefficent += scan_m1 -> coefficent ;
}
}
print_table (&h) ;
Release_list (&m1) ;
Release_list (&m2) ;
Release_table (&h) ;
return 0 ;
}
int hash (const int key, const int size)
{
return key % size ;
}
int get_random_coefficent (const int max)
{
return rand () % max + 1 ;
}
int get_random_power (const int max)
{
return rand () % max + 1 ;
}
LinkedList create_list (const int lenth, const int c_max, const int p_max)
{
LinkedList list ;
Node * new_node, * parent = NULL ;
int count, sum_of_power = 0 ;
list = (struct linkedlist *) malloc (sizeof (struct linkedlist)) ;
for (count = 0; count < lenth; count++)
{
new_node = (Node *) malloc (sizeof (Node)) ;
if (NULL == new_node)
puts ("Out of space[1]") ;
if (parent != NULL)
parent -> next = new_node ;
else
list -> noumenon = new_node ;
new_node -> coefficent = get_random_coefficent (c_max) ;
new_node -> power = get_random_coefficent (p_max) ;
sum_of_power += new_node -> power ;
parent = new_node ;
}
parent -> next = NULL ;
list -> sum_of_power = sum_of_power ;
return list ;
}
HashTable create_table (const int size)
{
HashTable h ;
int count ;
h = (struct hashtable *) malloc (sizeof (struct hashtable)) ;
if (NULL == h)
puts ("Out of space[2]") ;
h -> cell = (Cell *) malloc (sizeof (Cell) * size) ;
if (NULL == h -> cell)
puts ("Out of space[3]") ;
h -> size = size ;
for (count = 0; count < size; count++)
h -> cell[count].condition = LEISURE ;
return h ;
}
void print_multinomial (Node * const multinomial)
{
Node * scan = multinomial ;
while (scan)
{
printf ("[系数:%-30d 指数:%-10d]/n", scan -> coefficent, scan -> power) ;
scan = scan -> next ;
}
putchar ('/n') ;
}
void print_table (const HashTable * const ph)
{
int count, size = (*ph) -> size ;
Cell temp ;
for (count = 0; count < size; count++)
{
temp = (*ph) -> cell[count] ;
if (USED == temp.condition)
printf ("[系数:%-30d指数:%-10d]/n", temp.coefficent, temp.power) ;
}
}
void Release_list (const LinkedList * const pl)
{
Node * scan = (*pl) -> noumenon, * temp ;
while (scan)
{
temp = scan ;
scan = scan -> next ;
free (temp) ;
}
free (*pl) ;
}
void Release_table (const HashTable * const ph)
{
free ((*ph) -> cell) ;
free (*ph) ;
}