include <stdio.h>
include <stdlib.h>
struct node {
int val;
struct node* next;
};
struct node* head = NULL;
void insertAfterK(int value, int k) {
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->val = value;
newnode->next = NULL;
struct node* current = head;
// If the linked list is empty, set the new node as head and return
if (current == NULL) {
head = newnode;
return;
}
// Iterate to the k-th node (assuming k is 1-based)
for (int i = 1; i < k; i++) {
if (current->next == NULL) { // If k is greater than the number of nodes, insert at the end
break;
}
current = current->next;
}
// Set the next of new node to next of current
newnode->next = current->next;
// Set the next of current to new node
current->next = newnode;
}
void insert(int data) {
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->val = data;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
struct node* temp = head;
while (temp->next != NULL) { // Traverse until the end
temp = temp->next;
}
temp->next = newnode;
}
}
void display() {
struct node* temp = head;
while (temp != NULL) {
printf("%d ", temp->val);
temp = temp->next;
}
}
int main() {
int size, val, k;
scanf(“%d %d %d”, &size, &val, &k);
for (int i = 0; i < size; i++) {
int data;
scanf("%d", &data);
insert(data);
}
insertAfterK(val, k);
display();
return 0;
}
I got this many times I dont know why also I asked from chatgpt to about this error issue
sol.c:113:5: error: redefinition of ‘main’
** 113 | int main() {**
** | ^~**~**
sol.c:85:5: note: previous definition of ‘main’ with type ‘int()’
** 85 | int main() {**
** | ^
sol.c: In function ‘main’:
sol.c:126:9: error: too many arguments to function ‘insertAfterK’
** 126 | insertAfterK(list, a, i);**
** | ^~~****
sol.c:34:6: note: declared here
** 34 | void insertAfterK(int value, int k) {**
** | ^
sol.c:128:5: error: too many arguments to function ‘insertAfterK’
** 128 | insertAfterK(list, x, k);**
** | ^~~~~~~~~~~~**
sol.c:34:6: note: declared here
** 34 | void insertAfterK(int value, int k) {**
** | ^~~~~~~~~~~~t**