C 删除双链接列表中的第一个匹配项
我有一项任务要做。。任务是删除第一个奇数,只是我写的代码,但老实说,我对它没有信心。。如果你们能帮我,我会很高兴的。 该函数删除具有奇数值的第一个节点,因此该函数搜索具有奇数值的第一个节点并将其删除C 删除双链接列表中的第一个匹配项,c,struct,linked-list,doubly-linked-list,function-definition,C,Struct,Linked List,Doubly Linked List,Function Definition,我有一项任务要做。。任务是删除第一个奇数,只是我写的代码,但老实说,我对它没有信心。。如果你们能帮我,我会很高兴的。 该函数删除具有奇数值的第一个节点,因此该函数搜索具有奇数值的第一个节点并将其删除 typedef struct Node { int data; struct Node *next; struct Node *previous; }Node; int DeleteFirstODD(Node **front) { int oddnum; Node *temp = *front;
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;
}
对于初学者来说,这个typedef声明
typedef struct Node {
int data;
Node *next;
Node *previous;
}Node;
这是不正确的。你必须写作
typedef struct Node {
int data;
struct Node *next;
struct Node *previous;
} Node;
如果函数的返回类型不是void
,则在函数中不使用类似表达式的返回语句
return;
如果删除了具有奇数值的节点,则函数可以返回整数值1
,否则返回0
函数中的代码不正确。例如,这个循环
while (ptr != NULL && ptr % 2 != 0) {
oddnum = ptr->data;
ptr = ptr->next;
}
没有道理
该函数可以通过以下方式定义
int DeleteFirstODD( Node **front )
{
while ( *front && ( *front )->data % 2 == 0 )
{
front = &( *front )->next;
}
int success = *front != NULL;
if ( success )
{
Node *current = *front;
if ( current->next )
{
current->next->previous = current->previous;
}
*front = current->next;
free( current );
}
return success;
}
这是一个演示程序
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
struct Node *previous;
} Node;
int push_front( Node **head, int data )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->next = *head;
if ( *head ) ( *head )->previous = new_node;
new_node->previous = NULL;
*head = new_node;
}
return success;
}
void display( const Node *head )
{
for ( ; head; head= head->next )
{
printf( "%d -> ", head->data );
}
puts( "null" );
}
int DeleteFirstODD( Node **front )
{
while ( *front && ( *front )->data % 2 == 0 )
{
front = &( *front )->next;
}
int success = *front != NULL;
if ( success )
{
Node *current = *front;
if ( current->next )
{
current->next->previous = current->previous;
}
*front = current->next;
free( current );
}
return success;
}
int main(void)
{
Node *head = NULL;
const int N = 10;
for ( int i = N; i != 0; i-- )
{
push_front( &head, i );
}
display( head );
while ( DeleteFirstODD( &head ) )
{
display( head );
}
return 0;
}
我已将该部分更改为以下部分:while(temp!=NULL){if(temp%2!=0)oddnum=temp->data;//将来自奇数节点的数据存储为整数oddnum else temp=temp->next;}//这可能吗?@it'sM这无关紧要。在此表达式中,临时%2!=0您正试图用指针执行算术运算符%。非常感谢您!!!!!!我对代码进行了修改,使其变得简单,因为我的老师没有给我空间来创造性地使用我的代码。。所以现在的问题是当我更改节点值时,假设节点1的值是2,那么下一个节点的值是3,不,我们需要删除中间的节点,问题是当我运行代码时,它的空黑色命令。。你能帮我一下吗?@这是一个坏主意,改变代码的问题,这样一个基本的方式,因为这将混淆读者的问题和答案。你可以用更新的代码问一个新问题。啊哈,非常感谢!!!顺便说一句,我会用你的代码作为我作业的参考,因为我的教授就是这样一个人:)
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 6 -> 8 -> 9 -> 10 -> null
2 -> 4 -> 6 -> 8 -> 10 -> null