cl1p.net - The internet clipboard
Login/Sign Up
cl1p.net/xxxx
cl1p.net/xxxx
Login/Sign Up
This cl1p will be deleted in in 14 days.
Copy
#include
#include
struct Node { int coeff; int pow; struct Node *next; }; // Function to create a new node struct Node* createNode(int c, int p) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->coeff = c; newNode->pow = p; newNode->next = NULL; return newNode; } // Insert term in descending order of power void insertTerm(struct Node** poly, int c, int p) { if (c == 0) return; // ignore zero coefficients struct Node* newNode = createNode(c, p); if (*poly == NULL || (*poly)->pow < p) { newNode->next = *poly; *poly = newNode; } else { struct Node* temp = *poly; while (temp->next != NULL && temp->next->pow > p) { temp = temp->next; } if (temp->next != NULL && temp->next->pow == p) { temp->next->coeff += c; // combine like terms free(newNode); } else { newNode->next = temp->next; temp->next = newNode; } } } // Display polynomial void display(struct Node* poly) { if (poly == NULL) { printf("0\n"); return; } while (poly != NULL) { printf("%dx^%d", poly->coeff, poly->pow); poly = poly->next; if (poly != NULL) printf(" + "); } printf("\n"); } // Polynomial addition -> result in poly3 void addPoly(struct Node* p1, struct Node* p2, struct Node** poly3) { while (p1 != NULL && p2 != NULL) { if (p1->pow > p2->pow) { insertTerm(poly3, p1->coeff, p1->pow); p1 = p1->next; } else if (p1->pow < p2->pow) { insertTerm(poly3, p2->coeff, p2->pow); p2 = p2->next; } else { insertTerm(poly3, p1->coeff + p2->coeff, p1->pow); p1 = p1->next; p2 = p2->next; } } while (p1 != NULL) { insertTerm(poly3, p1->coeff, p1->pow); p1 = p1->next; } while (p2 != NULL) { insertTerm(poly3, p2->coeff, p2->pow); p2 = p2->next; } } // Polynomial multiplication -> result in poly3 void multiplyPoly(struct Node* p1, struct Node* p2, struct Node** poly3) { struct Node* t1 = p1; struct Node* t2; while (t1 != NULL) { t2 = p2; while (t2 != NULL) { insertTerm(poly3, t1->coeff * t2->coeff, t1->pow + t2->pow); t2 = t2->next; } t1 = t1->next; } } int main() { struct Node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL; int n, coeff, pow, i; printf("Enter number of terms in first polynomial: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Enter coefficient and power: "); scanf("%d%d", &coeff, &pow); insertTerm(&poly1, coeff, pow); } printf("Enter number of terms in second polynomial: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Enter coefficient and power: "); scanf("%d%d", &coeff, &pow); insertTerm(&poly2, coeff, pow); } printf("\nFirst Polynomial: "); display(poly1); printf("Second Polynomial: "); display(poly2); // Addition poly3 = NULL; addPoly(poly1, poly2, &poly3); printf("\nSum of Polynomials: "); display(poly3); // Multiplication poly3 = NULL; // reset result list multiplyPoly(poly1, poly2, &poly3); printf("Product of Polynomials: "); display(poly3); return 0; } web #include
#include
#include
struct Node { char url[100]; struct Node *prev; struct Node *next; }; struct Node *current = NULL; // Visit a new page void visitPage(char *url) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->url, url); newNode->next = NULL; newNode->prev = current; // Remove forward history if it exists if (current != NULL) { struct Node *temp = current->next; while (temp != NULL) { struct Node *del = temp; temp = temp->next; free(del); } current->next = NULL; } if (current != NULL) current->next = newNode; current = newNode; printf("Visited: %s\n", url); } // Go back to the previous page void goBack() { if (current == NULL || current->prev == NULL) { printf("No previous page.\n"); return; } current = current->prev; printf("Back to: %s\n", current->url); } // Go forward to the next page void goForward() { if (current == NULL || current->next == NULL) { printf("No forward page.\n"); return; } current = current->next; printf("Forward to: %s\n", current->url); } // Display browsing history from start void displayHistory() { struct Node *temp = current; // Move to first page while (temp && temp->prev != NULL) temp = temp->prev; printf("\nBrowsing History:\n"); while (temp != NULL) { if (temp == current) printf("-> %s (current)\n", temp->url); else printf(" %s\n", temp->url); temp = temp->next; } } int main() { int choice; char url[100]; do { printf("\nMenu:\n"); printf("1. Visit new page\n"); printf("2. Back\n"); printf("3. Forward\n"); printf("4. Display History\n"); printf("5. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); getchar(); // to consume newline switch (choice) { case 1: printf("Enter URL: "); fgets(url, sizeof(url), stdin); url[strcspn(url, "\n")] = '\0'; // remove newline visitPage(url); break; case 2: goBack(); break; case 3: goForward(); break; case 4: displayHistory(); break; case 5: printf("Exiting...\n"); break; default: printf("Invalid choice!\n"); } } while (choice != 5); return 0; }