c-将两个有序链表合并为一个有序链表



我一直在尝试编写一个代码,该代码将两个有序的整数列表合并为一个单一的有序整数列表。

函数CCD_ 1被假定接收指向两个列表中的每一个的第一节点的指针。

除了merge函数之外,其他一切似乎都正常工作。

问题似乎是它剪切了较短列表中的最后一个元素。

//12.7 merge two ordered lists into one ordered list
#include <stdio.h>
#include <stdlib.h>
struct listNode
{
    int data;
    struct listNode *nextPtr;
};
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    void insert( ListNodePtr *sPtr, int value );
    ListNodePtr merge( ListNodePtr xPtr, ListNodePtr yPtr );
    int isEmpty( ListNodePtr currentPtr );
    void printList( ListNodePtr currentPtr );
    void instructions(void);
int main(void)
{
    ListNodePtr startPtr1 = NULL;
    ListNodePtr startPtr2 = NULL;
    unsigned int choice;
    int item;
    instructions();
    printf("n?");
    scanf("%u",&choice);
    while (choice != 4)
    {
        switch (choice)
        {
        case 1:
            printf("Enter a character into list 1:n");
            scanf("n%d",&item);
            insert( &startPtr1, item );
            printList( startPtr1 );
            break;
        case 2:
            printf("Enter a character into list 2:n");
            scanf("n%d",&item);
            insert( &startPtr2, item );
            printList( startPtr2 );
            break;
        case 3:
            if (isEmpty(startPtr1) && isEmpty(startPtr2))
            {
                puts("Both lists are empty.");
            }
            else if (isEmpty(startPtr1))
            {
                puts("List 1 is empty.");
            }
            else if (isEmpty(startPtr2))
            {
                puts("List 2 is empty.");
            }
            else
            {
            printList( startPtr1 );
            printList( startPtr2 );
            puts("Merged list:");
            printList( merge( startPtr1, startPtr2 ) );
            }
            break;
        default:
            printf("Invalid choice.n");
            instructions();
            break;
        }
        printf("n?");
        scanf("%u",&choice);
    }
        puts("End of run.");
}
void instructions(void)
{
    printf("Enter your choice:n"
    "   1 to insert an number into list 1.n"
    "   2 to insert an number into list 2.n"
    "   3 to merge and order list 1 and list 2.n"
    "   4 to end.");
}
void insert( ListNodePtr *sPtr, int value )
{
    ListNodePtr newPtr;
    ListNodePtr previousPtr;
    ListNodePtr currentPtr;
    newPtr = (ListNodePtr)malloc(sizeof(ListNode));
    if (newPtr != NULL)
    {
        newPtr->data = value;
        newPtr->nextPtr = NULL;
        previousPtr = NULL;
        currentPtr = *sPtr;
        while (currentPtr != NULL && value > currentPtr->data)
        {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextPtr;
        }
        if (previousPtr == NULL)
        {
                newPtr->nextPtr = *sPtr;
                *sPtr = newPtr;
        }
        else
        {
            previousPtr->nextPtr = newPtr;
            newPtr->nextPtr = currentPtr;
        }
    }
    else
    {
            printf( "%d not inserted. No memory available.", value );
    }
}
ListNodePtr merge( ListNodePtr xPtr, ListNodePtr yPtr )
{
    ListNode merge; 
    ListNodePtr mergePtr = &merge;
    //PROBLEM: merge.nextPtr will be missing second to last element 
    //in final merged list
    while ( xPtr->nextPtr != NULL && yPtr->nextPtr != NULL)
    {
        if ( xPtr->data < yPtr->data)
        {
            mergePtr->nextPtr = xPtr;
            xPtr = xPtr->nextPtr;
            mergePtr = mergePtr->nextPtr;
        }//end if
        if ( yPtr->data < xPtr->data)
        {
            mergePtr->nextPtr = yPtr;
            yPtr = yPtr->nextPtr;
            mergePtr = mergePtr->nextPtr;
        }//end if
    }//end of while
    if ( xPtr->nextPtr == NULL )
    {
        mergePtr->nextPtr = yPtr;
    }
    if ( yPtr->nextPtr == NULL )
    {
        mergePtr->nextPtr = xPtr;
    }   
    return merge.nextPtr;
}//end of function merge

int isEmpty( ListNodePtr sPtr )
{
    return sPtr == NULL;
}
void printList( ListNodePtr currentPtr )
{
    if ( isEmpty(currentPtr) )
    {
        puts("List is empty");
    }
    else
    {
        while ( currentPtr != NULL )
        {
            printf("%d --> ", currentPtr->data);
            currentPtr = currentPtr->nextPtr;
        }
        puts("NULL");
    }
}

有人能解决这个问题吗?我已经学习C编程语言一个半月了。

我对您的代码做了一些修改,它运行得很完美!

//12.7 merge two ordered lists into one ordered list
#include <stdio.h>
#include <stdlib.h>
struct listNode
{
   ! int data;
    struct listNode *nextPtr;
};
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    void insert( ListNodePtr *sPtr, int value );
    ListNodePtr merge( ListNodePtr xPtr, ListNodePtr yPtr );
    int isEmpty( ListNodePtr currentPtr );
    void printList( ListNodePtr currentPtr );
    void instructions(void);
int main(void)
{
    ListNodePtr startPtr1 = NULL;
    ListNodePtr startPtr2 = NULL;
    unsigned int choice;
    int item;
    instructions();
    printf("n?");
    scanf("%u",&choice);
    while (choice != 4)
    {
        switch (choice)
        {
        case 1:
            printf("Enter a character into list 1:n");
            scanf("n%d",&item);
            insert( &startPtr1, item );
            printList( startPtr1 );
            break;
        case 2:
            printf("Enter a character into list 2:n");
            scanf("n%d",&item);
            insert( &startPtr2, item );
            printList( startPtr2 );
            break;
        case 3:
            if (isEmpty(startPtr1) && isEmpty(startPtr2))
            {
                puts("Both lists are empty.");
            }
            else if (isEmpty(startPtr1))
            {
                puts("List 1 is empty.");
            }
            else if (isEmpty(startPtr2))
            {
                puts("List 2 is empty.");
            }
            else
            {
            printList( startPtr1 );
            printList( startPtr2 );
            puts("Merged list:");
            printList( merge( startPtr1, startPtr2 ) );
            }
            break;
        default:
            printf("Invalid choice.n");
            instructions();
            break;
        }
        printf("n?");
        scanf("%u",&choice);
    }
        puts("End of run.");
}
void instructions(void)
{
    printf("Enter your choice:n"
    "   1 to insert an number into list 1.n"
    "   2 to insert an number into list 2.n"
    "   3 to merge and order list 1 and list 2.n"
    "   4 to end.");
}
void insert( ListNodePtr *sPtr, int value )
{
    ListNodePtr newPtr;
    ListNodePtr previousPtr;
    ListNodePtr currentPtr;
    newPtr = (ListNodePtr)malloc(sizeof(ListNode));
    if (newPtr != NULL)
    {
        newPtr->data = value;
        newPtr->nextPtr = NULL;
        previousPtr = NULL;
        currentPtr = *sPtr;
        while (currentPtr != NULL && value > currentPtr->data)
        {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextPtr;
        }
        if (previousPtr == NULL)
        {
                newPtr->nextPtr = *sPtr;
                *sPtr = newPtr;
        }
        else
        {
            previousPtr->nextPtr = newPtr;
            newPtr->nextPtr = currentPtr;
        }
    }
    else
    {
            printf( "%d not inserted. No memory available.", value );
    }
}
ListNodePtr merge( ListNodePtr xPtr, ListNodePtr yPtr )
{
    ListNode merge; 
    ListNodePtr begin, mergePtr = &merge;          //here
    begin = mergePtr;                              //here
    //PROBLEM: merge.nextPtr will be missing second to last element 
    //in final merged list
    while ( xPtr != NULL && yPtr != NULL)      //here 2   now I deal with the last of short link!
    {
        if ( xPtr->data < yPtr->data)
        {
            mergePtr->nextPtr = xPtr;
            xPtr = xPtr->nextPtr;
            mergePtr = mergePtr->nextPtr;
        }//end if
        else// ( yPtr->data < xPtr->data)      //here for yPtr->data == xPtr->data
        {
            mergePtr->nextPtr = yPtr;
            yPtr = yPtr->nextPtr;
            mergePtr = mergePtr->nextPtr;
        }//end if
    }//end of while
    if ( xPtr != NULL )                          //here 2
    {
        mergePtr->nextPtr = xPtr;
    }
    if ( yPtr != NULL )                           //here 2
    {
        mergePtr->nextPtr = yPtr;
    }   
    return begin->nextPtr;                         //here
}//end of function merge

int isEmpty( ListNodePtr sPtr )
{
    return sPtr == NULL;
}
void printList( ListNodePtr currentPtr )
{
    if ( isEmpty(currentPtr) )
    {
        puts("List is empty");
    }
    else
    {
        while ( currentPtr != NULL )
        {
            printf("%d --> ", currentPtr->data);
            currentPtr = currentPtr->nextPtr;
        }
        puts("NULL");
    }
}

由于您正在检查NEXT值是否为null,因此当您达到最后一个值时,您将退出mergewhile循环。在while循环之外,您应该将另一个列表附加到另一个上。因此,如果清单1的元素用完了,而清单2又有3个元素,那么不必遍历清单2,只需使(清单1的末尾)->next=(在清单2中停止的地方)。

此外,不要在merge函数中返回。你不需要。由于yoru列表已经占据了空间,你只是在切换它们的指针指向的位置。一旦所有指针都完成了,只需从函数返回一个访问List1/List2,就像你通常会做的那样(除了你需要弄清楚哪个List包含第一个指针,所以只需返回1或2)

此外,您永远不会将merge设置为我认为不存在的值

ListNodePtr merge( ListNodePtr xPtr, ListNodePtr yPtr )
{
    ListNode merge; 
    ListNodePtr mergePtr = &merge;
    while ( xPtr != NULL && yPtr != NULL)
    {
        if ( xPtr->data < yPtr->data)
        {
            mergePtr->nextPtr = xPtr;
            xPtr = xPtr->nextPtr;
            mergePtr = mergePtr->nextPtr;
        } else if ( yPtr->data < xPtr->data)//need else because 1 loop 2 ecxecute merge
        {
            mergePtr->nextPtr = yPtr;
            yPtr = yPtr->nextPtr;
            mergePtr = mergePtr->nextPtr;
        }//end if
        //nothing case of (yPtr->data == xPtr->data)
    }//end of while
    if ( xPtr == NULL )
    {
        mergePtr->nextPtr = yPtr;
    }
    if ( yPtr == NULL )
    {
        mergePtr->nextPtr = xPtr;
    }   
    return merge.nextPtr;
}//end of function merge

相关内容

  • 没有找到相关文章

最新更新