如何在新的单链列表中返回单链列表的奇数索引节点?假定第一个节点的索引为1 [英] 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

查看:105
本文介绍了如何在新的单链列表中返回单链列表的奇数索引节点?假定第一个节点的索引为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?

输入

10->20->30->40->50->60->70->80->90->100

输出必须为

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


推荐答案

总是更改同一对象 pList-> next

       else
       {
           pList->next=pTemp;
       }

此外,原始列表未更改。因此,该函数具有未定义的行为。

And moreover the original list is not being changed. So the function has undefined behavior.

对于初学者,您应该通过引用传递原始节点的头部。否则,该函数将处理head的副本,并且副本的任何更改都不会影响原始列表。

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.

此处是一个演示程序,显示了该函数如何执行

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

函数输出为

10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> 90 -> 100 -> null
20 -> 40 -> 60 -> 80 -> 100 -> null
10 -> 30 -> 50 -> 70 -> 90 -> null

如果不打算更改原始列表,则该函数看起来会更简单,因为在这种情况下不需要通过引用将指针pHead传递给函数。

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.

这里是一个演示程序。

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

其输出为

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

这篇关于如何在新的单链列表中返回单链列表的奇数索引节点?假定第一个节点的索引为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆