问题
I do not want to use double pointer in my code,please assume index of the first node as 1. I have a linked list 10->20->30->40->50->60->70->80->90->100->NULL In an another linked list whose head pointer is pLink,I want to copy the odd indexed nodes and get the output as 10->30->50->70->90->NULL.
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;
}
When I run this code I get output as 10->90->NULL I know I have a problem with the "else" part. SLLI*pIter=pList does not make any sense, but what can I do for eliminating this mistake?
回答1:
I guess the problem is that pList
is not changed at all in your else
statement.
Wouldnt that do the trick:
SLLI*OddNodes(SLLI*pHead)
{
int counter =1;
SLLI*pTemp=pHead;
SLLI*pList=NULL;
SLLI*pNewHead=NULL;
while(pTemp != NULL)
{
if(counter % 2 != 0)
{
if(pList==NULL)
{
pList=malloc(sizeof(SLLI));
pList->data=pTemp->data;
pList->next=NULL;
pNewHead = pList;
}
else
{
pList->next = malloc(sizeof(SLLI));
pList->next->data = pTemp->data;
pList->next->next = NULL;
pList = pList->next;
}
pTemp = pTemp->next;
counter++
}
}
return pNewHead;
}
In addition I would check if malloc
was succesful
回答2:
Here you are.
#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;
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;
}
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 program 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
回答3:
Just a DRY complement to Vlad from Moscow's answer:
/**
* Returns a list with the data from every other item starting at *original.
* Does not modify the nodes from *original: malloc()s new nodes
*/
SLLI *OddNodes(SLLI const *original)
{
int odd = 0;
SLLI oddHead = { 0, NULL },
*copy = &oddHead;
for ( ; original != NULL ; original = original->next)
if (odd ^= 1) {
if (NULL == (copy->next = malloc(sizeof *copy)))
/* alternatively, free "oddHead.next" & return NULL.
* Should be documented in head comment! */
exit(EXIT_FAILURE);
copy = copy->next;
copy->data = original->data;
copy->next = NULL;
}
return oddHead.next;
}
来源:https://stackoverflow.com/questions/60172042/how-can-i-get-the-odd-indexed-nodes-of-a-linked-list-in-an-another-linked-listi