#include<stdio.h> #include<stdlib.h> typedef struct node { float coef; int expn; struct node *next; }node,*linklist; linklist L1,L2,head; int num; void create_linklist(linklist *S) { int i=0; linklist p; float data; head=NULL; printf("\nplease input nodes' coef (group by expn(from 0 ~%d)):\n",num-1); while(i<num) { p=(node *)malloc(num*sizeof(node)); if(!p) { printf("overflow!"); exit(0); } p->next=head; head=p; scanf("%f",&data); head->coef=data; head->expn=i; i++; } *S=head; printf("\nhaving created a linklist:\n"); i=0; p=*S; printf("%d",num); while(p!=NULL) { printf(" %f",p->coef); p=p->next; } } void computer_linklist() { linklist p1,p2; float data; printf("\nhow many expns of the expression do you want to set up?:"); scanf("%d",&num); printf("\nstart to create linklist L1:"); create_linklist(&L1); printf("\nstart to create linklise L2:"); create_linklist(&L2); p1=L1; p2=L2; printf("\nthe L1+L2's result is:"); printf("%d",num); while(p2!=NULL&&p1!=NULL) { data=p2->coef+p1->coef; p1=p1->next; p2=p2->next; printf(" %f",data); } printf("\nthe L1-L2's result is:"); p1=L1; p2=L2; printf("%d",num); while(p2!=NULL&&p1!=NULL) { data=p2->coef-p1->coef; p1=p1->next; p2=p2->next; printf(" %f",data); } getch(); } main() { clrscr(); computer_linklist(); }

评论