C Programs
C Programs
// Add the product to the result, combining like terms // Function to pop a character from the stack
int found = 0; char pop() {
for (int l = 0; l < k; l++) { if (top == -1) {
if (result[l].exponent == expo) { printf("Stack Underflow\n");
result[l].coefficient += coeff; return -1; // Return an invalid character when stack is empty
found = 1; } else {
break; } } char ch = stack[top];
// If no like term found, add the new term top--;
if (!found) { return ch;
result[k].coefficient = coeff; }
result[k].exponent = expo; }
k++;}}}
// Print the result polynomial int main() {
printf("Result of polynomial multiplication:\n"); char str[MAX];
for (int i = 0; i < k; i++) {
printf("%dx^%d", result[i].coefficient, result[i].exponent); // Read the string from the user
if (i != k - 1) { printf("Enter a string: ");
printf(" + "); gets(str); // Using gets to allow reading space-separated
} strings
}
printf("\n"); // Push each character of the string onto the stack
} for (int i = 0; i < strlen(str); i++) {
push(str[i]);
void main() { }
int n1, n2;
// Pop characters from the stack to get the reversed string
printf("Enter the number of terms in the first polynomial: "); printf("Reversed string: ");
scanf("%d", &n1); while (top != -1) {
struct Term poly1[n1]; printf("%c", pop());
printf("Enter the terms of the first polynomial (coefficient }
exponent): \n"); printf("\n");
for (int i = 0; i < n1; i++) {
scanf("%d %d", &poly1[i].coefficient, &poly1[i].exponent); return 0;
} }
printf("Enter the number of terms in the second
polynomial:");
scanf("%d", &n2);
struct Term poly2[n2];
printf("Enter the terms of the second polynomial (coefficient
exponent): \n");
for (int i = 0; i < n2; i++) {
scanf("%d %d", &poly2[i].coefficient, &poly2[i].exponent)}
void main() {
int choice, x;
printf("Enter the size of the priority queue: ");
scanf("%d", &n);
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a number: ");
Merge two stacks }
#include <stdio.h> }
int stack1[30], stack2[30], stack3[60], top1 = -1, top2 = -1, top3 = -1, void main() {
n1, n2; int x, ch;
// Display stack2
void displayStack2() {
if (top2 == -1) {
printf("Copied stack is empty\n");
} else {
printf("Copied stack:\n");
for (int i = top2; i >= 0; i--) {
printf("%d\t", stack2[i]);
}
printf("\n");
}
}
void main() {
int x;