一元多项式的加法
include <stdio.h>
include <stdlib.h>
typedef struct Term {
int coef;
int exp;
struct Term *next;
} Term;
Term* readPoly() {
int n;
scanf("%d", &n);
Term *head = NULL;
Term *tail = NULL;
for (int i = 0; i < n; i++) {
int c, e;
scanf("%d %d", &c, &e);
Term t = (Term)malloc(sizeof(Term));
t->coef = c;
t->exp = e;
t->next = NULL;
if (head == NULL) {
head = t;
tail = t;
} else {
tail->next = t;
tail = t;
}
}
return head;
}
Term* addPoly(Term* p1, Term* p2) {
Term *head = NULL;
Term *tail = NULL;
Term *t1 = p1;
Term *t2 = p2;
while (t1 || t2) {
Term t = (Term)malloc(sizeof(Term));
if (t1 && t2) {
if (t1->exp > t2->exp) {
t->coef = t1->coef;
t->exp = t1->exp;
t1 = t1->next;
} else if (t1->exp < t2->exp) {
t->coef = t2->coef;
t->exp = t2->exp;
t2 = t2->next;
} else {
t->coef = t1->coef + t2->coef;
t->exp = t1->exp;
t1 = t1->next;
t2 = t2->next;
}
} else if (t1) {
t->coef = t1->coef;
t->exp = t1->exp;
t1 = t1->next;
} else {
t->coef = t2->coef;
t->exp = t2->exp;
t2 = t2->next;
}
if (t->coef != 0) {
t->next = NULL;
if (head == NULL) {
head = t;
tail = t;
} else {
tail->next = t;
tail = t;
}
} else {
free(t);
}
}
return head;
}
void printPoly(Term* p) {
if (p == NULL) {
printf("0 0");
return;
}
int first = 1;
Term *t = p;
while (t) {
if (!first) {
printf(" ");
}
printf("%d %d", t->coef, t->exp);
first = 0;
t = t->next;
}
}
void freePoly(Term* p) {
Term *t = p;
while (t) {
Term *temp = t;
t = t->next;
free(temp);
}
}
int main() {
Term* p1 = readPoly();
Term* p2 = readPoly();
Term* sum = addPoly(p1, p2);
printPoly(sum);
freePoly(sum);
freePoly(p1);
freePoly(p2);
return 0;
}
Central List
include <stdio.h>
include <stdlib.h>
include <string.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertEnd(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
head = newNode;
return;
}
Node temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* curr = head;
Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
int isPalindrome(Node* head) {
if (head == NULL || head->next == NULL) {
return 1;
}
Node* slow = head;
Node* fast = head;
while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
Node* secondHalf = reverseList(slow->next);
slow->next = NULL;
Node* firstHalf = head;
Node* temp = secondHalf;
while (temp != NULL) {
if (firstHalf->data != temp->data) {
return 0;
}
firstHalf = firstHalf->next;
temp = temp->next;
}
return 1;
}
int main() {
char input[1024];
fgets(input, sizeof(input), stdin);
Node* head = NULL;
char* token = strtok(input, ",");
while (token != NULL) {
if (strlen(token) > 0 && token[0] != '\n') {
int num = atoi(token);
insertEnd(&head, num);
}
token = strtok(NULL, ",");
}
if (isPalindrome(head)) {
printf("Yes\n");
} else {
printf("No\n");
}
// Free the list
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
return 0;
}
Who is the Last?
include <stdio.h>
include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_circle(int n) {
if (n == 0) return NULL;
struct node *head = (struct node *)malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
struct node *tail = head;
for (int i = 2; i <= n; i++) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = i;
new_node->next = NULL;
tail->next = new_node;
tail = new_node;
}
tail->next = head;
return head;
}
int main() {
int N, M;
scanf("%d,%d", &N, &M);
struct node *head = create_circle(N);
struct node *current = head;
int num = N;
int *order = (int *)malloc(N * sizeof(int));
int idx = 0;
while (num > 1) {
struct node *prev = current;
struct node *temp = current->next;
int count = 1;
while (count < M) {
prev = temp;
temp = temp->next;
count++;
}
order[idx++] = temp->data;
prev->next = temp->next;
free(temp);
current = prev->next;
num--;
}
if (num == 1) {
order[idx++] = current->data;
free(current);
}
for (int i = 0; i < N; i++) {
printf("%d", order[i]);
if (i < N - 1) printf(",");
}
free(order);
return 0;
}