[英]How can I return the odd indexed nodes of a singly linked list in a new singly linked list ?Assume index of the first node as 1
When I run this code I am not getting an error message from the compiler but I can not return the new list.当我运行此代码时,我没有从编译器收到错误消息,但我无法返回新列表。 Am I writing down the code wrong in the MAIN part?
我在 MAIN 部分写错了代码吗?
Input输入
10->20->30->40->50->60->70->80->90->100
Output must be输出必须是
10->30->50->70->90
#include <stdio.h>
#include <stdlib.h>
typedef struct SinglyLinkedListItem
{
int data;
struct SinglyLinkedListItem*next;
}SLLI;
SLLI*OddNodes(SLLI*pHead)
{
int counter =1;
SLLI*pTemp=pHead;
SLLI*pList=NULL;
while(pTemp != NULL)
{
if(counter % 2 != 0)
{
if(pList==NULL)
{
pList=malloc(sizeof(SLLI));
pList->data=pTemp->data;
pList->next=NULL;
}
else
{
SLLI*pIter=pList;
SLLI*pNew=malloc(sizeof(SLLI));
pNew->data=pTemp->data;
pNew->next=NULL;
pIter->next=pNew;
pIter=pIter->next;
}
}
pTemp=pTemp->next;
counter ++;
}
return pList;
}
You are always changing the same object pList->next
.您总是在更改同一个对象
pList->next
。
else
{
pList->next=pTemp;
}
And moreover the original list is not being changed.此外,原始列表不会更改。 So the function has undefined behavior.
因此该函数具有未定义的行为。
For starters you should pass the head of the original node by reference.对于初学者,您应该通过引用传递原始节点的头部。 Otherwise the function will deal with a copy of head and any changes of the copy will not influence on the original list.
否则该函数将处理 head 的副本,副本的任何更改都不会影响原始列表。
Here is a demonstrative program that shows how the function can be implemented.这是一个演示程序,展示了如何实现该功能。
#include <stdio.h>
#include <stdlib.h>
typedef struct SinglyLinkedListItem
{
int data;
struct SinglyLinkedListItem *next;
} SLLI;
SLLI * OddNodes( SLLI **pHead )
{
int odd = 0;
SLLI *pList = NULL;
SLLI **pCurrent = &pList;
while ( *pHead != NULL )
{
if ( odd ^= 1 )
{
*pCurrent = *pHead;
*pHead = ( *pHead )->next;
( *pCurrent )->next = NULL;
pCurrent = &( *pCurrent )->next;
}
else
{
pHead = &( *pHead )->next;
}
}
return pList;
}
int insert( SLLI **pHead, int data )
{
SLLI *pCurrent = malloc( sizeof( SLLI ) );
int success = pCurrent != NULL;
if ( success )
{
pCurrent->data = data;
pCurrent->next = *pHead;
*pHead = pCurrent;
}
return success;
}
void out( SLLI *pHead )
{
for ( ; pHead != NULL; pHead = pHead->next )
{
printf( "%d -> ", pHead->data );
}
puts( "null" );
}
int main(void)
{
const int N = 10;
SLLI *pHead = NULL;
for ( int i = N; i != 0; --i )
{
insert( &pHead, 10 * i );
}
out( pHead );
SLLI *pSecondHead = OddNodes( &pHead );
out( pHead );
out( pSecondHead );
return 0;
}
The function output is函数输出是
10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> 90 -> 100 -> null
20 -> 40 -> 60 -> 80 -> 100 -> null
10 -> 30 -> 50 -> 70 -> 90 -> null
If you are not going to change the original list then the function can look simpler because in this case there is no need to pass the pointer pHead to the function by reference.如果您不打算更改原始列表,那么该函数看起来会更简单,因为在这种情况下,无需通过引用将指针 pHead 传递给该函数。
Here is a demonstrative program.这是一个演示程序。
#include <stdio.h>
#include <stdlib.h>
typedef struct SinglyLinkedListItem
{
int data;
struct SinglyLinkedListItem *next;
} SLLI;
SLLI * OddNodes( SLLI *pHead )
{
int odd = 0;
SLLI *pList = NULL;
SLLI **pCurrent = &pList;
for ( ; pHead != NULL; pHead = pHead->next )
{
if ( odd ^= 1 )
{
*pCurrent = malloc( sizeof( SLLI ) );
( *pCurrent )->data = pHead->data;
( *pCurrent )->next = NULL;
pCurrent = &( *pCurrent )->next;
}
}
return pList;
}
int insert( SLLI **pHead, int data )
{
SLLI *pCurrent = malloc( sizeof( SLLI ) );
int success = pCurrent != NULL;
if ( success )
{
pCurrent->data = data;
pCurrent->next = *pHead;
*pHead = pCurrent;
}
return success;
}
void out( SLLI *pHead )
{
for ( ; pHead != NULL; pHead = pHead->next )
{
printf( "%d -> ", pHead->data );
}
puts( "null" );
}
int main(void)
{
const int N = 10;
SLLI *pHead = NULL;
for ( int i = N; i != 0; --i )
{
insert( &pHead, 10 * i );
}
out( pHead );
SLLI *pSecondHead = OddNodes( pHead );
out( pHead );
out( pSecondHead );
return 0;
}
Its output is它的输出是
10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> 90 -> 100 -> null
10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> 90 -> 100 -> null
10 -> 30 -> 50 -> 70 -> 90 -> null
If you do not yest understand the work with pointers by reference then the function can look the following way如果您不了解通过引用使用指针的工作,那么该函数可以如下所示
SLLI * OddNodes( SLLI *pHead )
{
int odd = 0;
SLLI *pList = NULL;
for ( SLLI *pCurrent = pList; pHead != NULL; pHead = pHead->next )
{
if ( odd ^= 1 )
{
if ( pCurrent == NULL )
{
pList = malloc( sizeof( SLLI ) );
pList->data = pHead->data;
pList->next = NULL;
pCurrent = pList;
}
else
{
pCurrent->next = malloc( sizeof( SLLI ) );
pCurrent->next->data = pHead->data;
pCurrent->next->next = NULL;
pCurrent = pCurrent->next;
}
}
}
return pList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.