数据结构--链表的实现(二)
来源:互联网 发布:淘宝网红裤子店铺 编辑:程序博客网 时间:2024/06/06 04:11
继续写了链表的几个操作,链表的交与并,反转,自调整查找,打印特定位置上的元素。
关于多项式表示链表的加法与乘法,写的是排序之后的形式。
多项式乘法:
/** *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;}
- 数据结构--链表的实现(二)
- 数据结构学习系列二-链表的C++实现
- 算法与数据结构【二】简单链表的实现
- 数据结构---线性表的链式表示和实现(二)
- 数据结构基础(二)队列的实现
- 编程中常用的重要的数据结构实现(二)之链表
- 探究数据结构之链表Java代码实现(二)
- 数据结构-C++实现(二):单向链表
- 数据结构(严蔚敏)之二——链表的c语言实现
- 单向循环链表的简单实现--数据结构学习(二)
- 数据结构(二)——单链表 、带头结点的单链表、循环链表 及其实现
- 数据结构 二、队列的实现
- C#实现数据结构(二)
- 学会一种数据结构二:队列的数组实现和链表实现
- 数据结构算法代码实现——线性表的顺序表示与实现(二)
- 数据结构--链表的实现(一)
- 数据结构二:线性表的顺序结构实现
- (二)数据结构之线性表的简单实现:堆栈
- hdu 2035 大数模
- Cache
- An Overview of Acoustic Modeling Techniques from ICASSP 2012
- [推荐] 基于多平台优化的音频编码器和解码器
- 第四章 职业生涯发展计划
- 数据结构--链表的实现(二)
- JAVA for each 循环
- all_parameters
- 赋值运算符重载引发的思考(引用的功能)
- 一些程序优化技术
- 效率利器1-linux快捷键
- Node.js 编程 -- module
- ubuntu 中下载openssh源码移植到 arm开发,出现you don't exist, go away问题的解决
- lighttpd+PHP安装