【发布时间】:2023-03-25 23:00:02
【问题描述】:
我有一个任务要做.. 任务是删除第一个奇数,只是我编写了代码,但老实说,我对它的信心为零.. 如果你们能帮助我,我会很高兴。 该函数删除第一个奇数值的节点,因此该函数搜索第一个奇数值的节点并将其删除。
typedef struct Node {
int data;
struct Node *next;
struct Node *previous;
}Node;
int DeleteFirstODD(Node **front) {
int oddnum;
Node *temp = *front;
if (*front == NULL) //Checking if the list is empty
return;
while (temp != NULL && temp->data % 2 == 0)
temp = temp->next;
if (temp == NULL)
return -1;
else if (temp == *front) { //if odd num founded @ the begining of the doubly
linked list!
oddnum = (*front)->data;
*front = (*front)->next;
(*front)->previous = NULL;
free(temp);
}
else if (temp->next == NULL) { //if odd num founded @ the end
oddnum = temp->data;
temp->previous->next = NULL;
free(temp);
}
else { // if the odd somewhere in the middle
temp->previous->next = NULL;
temp->next->previous = NULL;
free(temp);
}
return oddnum;
}
【问题讨论】:
标签: c struct linked-list doubly-linked-list function-definition