简体   繁体   中英

How can I get the odd indexed nodes of a linked list in an another linked list?I do not want to use double pointer

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?

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

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM