Untitled
unknown
plain_text
4 years ago
1.7 kB
2
Indexable
#include <stdio.h> #include <stdlib.h> #define no 2 struct Node { int c, x, y; struct Node* next; }; void insert(struct Node** head, int nc, int px, int py) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node -> c = nc; new_node -> x = px; new_node -> y = py; new_node -> next = (*head); (*head) = new_node; } void display(struct Node* node) { while(node != NULL) { printf("%d(x^%d)(y^%d)", node -> c, node -> x, node -> y); node = node -> next; printf(" + "); } } void add(struct Node* ans, struct Node* n1, struct Node* n2) { while(n1 != NULL) { while(n2 != NULL) { if(n1 -> x == n2 -> x && n1 -> y == n2 -> y) { insert(&ans, n1->c + n2->c, n1->x, n1->y); printf("%d", n1->c + n2->c); } n2 = n2 -> next; } n1 = n1 -> next; } } void main() { struct Node *p1 = NULL, *p2 = NULL, *sum = NULL; int n, x, y; printf("Enter the values of the linked-list, in the format <coefficent power-of-x power-of-y>:\nP1:\n"); for(int i = 0; i < no; i++) { scanf("%d %d %d", &n, &x, &y); insert(&p1, n, x, y); } printf("P2:\n"); for(int i = 0; i < no; i++) { scanf("%d %d %d", &n, &x, &y); insert(&p2, n, x, y); } display(p1); printf("\n"); display(p2); printf("\n"); add(sum, p1, p2); display(sum); }
Editor is loading...