继续写了链表的几个操作,链表的交与并,反转,自调整查找,打印特定位置上的元素。
关于多项式表示链表的加法与乘法,写的是排序之后的形式。
多项式乘法:
/** *FILENAME: polynomial.c *AUTHOR: XIANG CHANG SHENG *CREATE ON:2012-12-22 */ #include <stdio.h> #include <stdlib.h> #include <time.h> /** *To implement two polynomial multiple */ struct Polynomial { int coeff; int power; struct Polynomial *next; }; struct Polynomial * multiple(struct Polynomial *poly_1, struct Polynomial *poly_2) { int coeff, power; struct Polynomial *result, *cnt_item_result, *cnt_row_result, *temp, *previous_result = NULL; struct Polynomial *cnt_poly_1, *cnt_poly_2; result = (struct Polynomial *)malloc(sizeof(struct Polynomial)); result->next = NULL; for (cnt_poly_1 = poly_1->next; cnt_poly_1 != NULL; cnt_poly_1 = cnt_poly_1->next) { cnt_row_result = result; for (cnt_poly_2 = poly_2->next; cnt_poly_2 != NULL; cnt_poly_2 = cnt_poly_2->next) { power = cnt_poly_1->power + cnt_poly_2->power; coeff = cnt_poly_1->coeff * cnt_poly_2->coeff; temp = (struct Polynomial *) malloc(sizeof(struct Polynomial)); temp->power = power; temp->coeff = coeff; cnt_item_result = cnt_row_result; while (cnt_item_result->next != NULL && cnt_item_result->next->power > temp->power) { cnt_item_result = cnt_item_result->next; } temp->next = cnt_item_result->next; cnt_item_result->next = temp; cnt_row_result = temp; } } return result; } int main() { freopen("input.txt","r",stdin); struct Polynomial *poly_1 = (struct Polynomial *)malloc(sizeof(struct Polynomial)); struct Polynomial *poly_2 = (struct Polynomial *)malloc(sizeof(struct Polynomial)); struct Polynomial *temp, *cnt_coeff, *result; clock_t start, end; int coeff, power; int first, n ,m, i; cnt_coeff = poly_1; while (scanf("%d%d",&n,&m) != EOF) { while (scanf("%d%d",&coeff,&power) == 2 && (coeff || power)) { temp = (struct Polynomial *)malloc(sizeof(struct Polynomial)); temp->coeff = coeff; temp->power = power; temp->next = NULL; cnt_coeff->next = temp; cnt_coeff = temp; } cnt_coeff = poly_2; while (scanf("%d%d",&coeff,&power) == 2 && (coeff || power)) { temp = (struct Polynomial *)malloc(sizeof(struct Polynomial)); temp->coeff = coeff; temp->power = power; temp->next = NULL; cnt_coeff->next = temp; cnt_coeff = temp; } start = clock(); result = multiple(poly_1, poly_2); end = clock(); printf("%.3f\n",(double)(end - start) / CLOCKS_PER_SEC); } for (;result != NULL; result = result->next) { if (first == 1) { printf("%dX^%d ",result->coeff, result->power); first = 2; } else { printf("+ %dX^%d",result->coeff,result->power); } } printf("\n"); }
struct Link * selfAdjustFind(struct Link *link_list, int key) { struct Link *link_node; struct Link *temp_node; link_node = findPrevious(link_list , key); if (link_node->next != NULL) { temp_node = link_node->next; link_node->next = temp_node->next; temp_node->next = link_list->next; link_list->next = temp_node; } } void headInsert(struct Link *link_list, int key) { struct Link *link_node = link_list; struct Link *insert_node; insert_node = (struct Link *)malloc(sizeof(struct Link)); insert_node->key = key; insert_node->next = link_node->next; link_node->next = insert_node; } struct Link * unionLinkList(struct Link *link_list_1, struct Link *link_list_2) { struct Link *result = createLink(); struct Link *link_node_1 = link_list_1->next; struct Link *link_node_2 = link_list_2->next; struct Link *result_node = result; while (link_node_1 != NULL || link_node_2 != NULL) { if (link_node_2 == NULL || (link_node_1 != NULL && link_node_1->key < link_node_2->key)) { result_node->next = (struct Link *)malloc(sizeof(struct Link)); result_node = result_node->next; result_node->key = link_node_1->key; result_node->next = NULL; link_node_1 = link_node_1->next; } else { result_node->next = (struct Link *)malloc(sizeof(struct Link)); result_node = result_node->next; result_node->key = link_node_2->key; result_node->next = NULL; link_node_2 = link_node_2->next; } } return result; } struct Link * intersectionLinkList(struct Link *link_list_1, struct Link *link_list_2) { struct Link *result= createLink(); struct Link *link_node_1 = link_list_1->next; struct Link *link_node_2 = link_list_2->next; struct Link *result_node = result; while (link_node_1 != NULL && link_node_2 != NULL) { if (link_node_1->key > link_node_2->key) { link_node_2 = link_node_2->next; } else if (link_node_1->key < link_node_2->key) { link_node_1 = link_node_1->next; } else { result_node->next = (struct Link *)malloc(sizeof(struct Link)); result_node = result_node->next; result_node->key = link_node_1->key; result_node->next = NULL; link_node_1 = link_node_1->next; link_node_2 = link_node_2->next; } } return result; } void printLots(struct Link *link_list, struct Link *order) { struct Link *link_node = link_list->next; struct Link *order_node = order->next; int order_number = 1; while (link_node != NULL) { if ((order_node != NULL) && (order_number == order_node->key)) { printf("%d\n", link_node->key); order_node = order_node->next; } order_number++; link_node = link_node->next; } } void reverseLinkList(struct Link *link_list) { struct Link *next_node; struct Link *cnt_node; struct Link *previous_node = link_list->next; if (previous_node == NULL) { return ; } cnt_node = previous_node->next; previous_node->next = NULL; while (cnt_node != NULL) { next_node = cnt_node->next; cnt_node->next = previous_node; previous_node = cnt_node; cnt_node = next_node; } link_list->next = previous_node; }