标签:状态 int ret 记录 根据 链表 while log 指针
2 7 3 5 12 1 6 0 -1 -1
14 6 15 4 12 0
代码如下
#include<stdio.h> #include<malloc.h> typedef struct polynode //链表结构 { int c; int e; struct polynode *next; } poly; poly *creatpoly() //用三个指针创建链表 { //一个记录链表头指针,一个用来开辟新空间,一个用来连接链表 int c; int e; poly *p,*q,*h; q = NULL,h = NULL; while(e != -1) { scanf("%d%d",&c,&e); h = (poly*)malloc(sizeof(poly)); h->c = c; h->e = e; h->next = NULL; if (q == NULL) q = h; else p->next = h; p = h; } return q; } poly *qiudao(poly *q) //求导:用两个指针 { //一个用来 记录求导后链表的首地址,一个从头移动到尾 poly *s; s = q; while(q) { q->c = (q->c)*(q->e); q->e = q->e - 1; q = q->next; } return s; } void printpoly(poly *s) //输出链表 { int i = 0; //这儿 i 的作用就是判断有没有输入,根据题目意思,如果直接输入-1,-1就要输出0 ,与第62行的判断相对应 if (s->e == -1) { printf("0");i++; } else { while (s->next != NULL) { if (s->c != 0) { printf("%d %d ",s->c,s->e); i++; } s = s->next; } } if (i == 0) printf("0"); printf("\n"); } int main() //主函数 { poly *q,*s; q = creatpoly(); s = qiudao(q); printpoly(s); return 0; }
标签:状态 int ret 记录 根据 链表 while log 指针
原文地址:http://www.cnblogs.com/cunyusup/p/7642873.html