简体   繁体   English

如何在新的单向链表中返回单向链表的奇数索引节点?假设第一个节点的索引为 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

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.

相关问题 单链表,如何返回指向新节点的指针 - Singly linked list, how to return a pointer to a new node 如何使用给定的递归在单链表中插入节点:1. 指向头节点的指针 2. 插入新节点的索引 - How can I insert a node in a singly linked list using recursion given: 1. a pointer to the head node 2. the index to insert the new node 填充单链列表节点 - Filling a singly linked list nodes 如何在第 n 个位置后的单向链表中插入新节点? - How to insert new node in a singly linked list after nth position? 如何将节点添加到最后(单链表) - how to add node to the end(singly linked list) 如何删除单链列表中的任何节点 - How to remove any node in a singly linked list 如果同一索引中的下一个节点不是 NULL,我如何删除单链表链接 hash 表的第一个节点? - How do i delete the first node of singly linked list chaining hash table if the next node in the same index is not NULL? 带有cstring的单链表,如何找到复杂时间为o(1)的单链表中所有节点的长度之和? - Singly linked list with cstring, how to find sum of length of all nodes in singly linked list with complexity time is o(1)? 如何使用这个单链表? - How to work with this singly linked list? 给定一个节点如何在单链表中找到前一个节点 - given a node how can I find previous node in a singly linked list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM