gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:46:59 24 4
gpt4 key购买 nike

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

函数ListNodePtr merge(ListNodePtr xPtr, ListNodePtr yPtr)应该接收指向两个列表中每个列表的第一个节点的指针。

除了合并功能之外,其他一切似乎都有效。

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

//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");
}
}

关于c - 将两个有序链表合并为一个有序链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17844254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com