새로운 단일 연결 목록에서 단일 연결 목록의 홀수 인덱싱 된 노드를 반환하려면 어떻게해야합니까? 첫 번째 노드의 인덱스를 1로 가정합니다.

YTE 1008

이 코드를 실행할 때 컴파일러에서 오류 메시지가 나타나지 않지만 새 목록을 반환 할 수 없습니다. MAIN 부분에 코드를 잘못 적었나요?

입력

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

Vlad / 모스크바

항상 동일한 객체를 변경합니다 pList->next.

       else
       {
           pList->next=pTemp;
       }

또한 원래 목록은 변경되지 않습니다. 따라서 함수에는 정의되지 않은 동작이 있습니다.

우선, 원본 노드의 헤드를 참조로 전달해야합니다. 그렇지 않으면 함수는 헤드의 복사본을 처리하고 복사본의 변경 사항은 원본 목록에 영향을주지 않습니다.

다음은 기능을 구현할 수있는 방법을 보여주는 데모 프로그램입니다.

#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를 참조로 함수에 전달할 필요가 없기 때문입니다.

여기에 시범 프로그램이 있습니다.

#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

참조로 포인터 작업을 이해하지 못하는 경우 함수는 다음과 같이 보일 수 있습니다.

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관