我一直在努力研究将两个有序整数列表合并为一个有序整数列表的代码。
函数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编程语言。
答案 0 :(得分:1)
我修改了你的代码,它完美无缺!
//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");
}
}
答案 1 :(得分:0)
由于您正在检查NEXT值是否为空,当您点击最后一个值时,您将在循环时中断合并。在这个while循环之外,你应该将另一个列表附加到另一个列表上。因此,如果List1用完元素而List2有3个以上,而不是遍历List2,只需make(List1的结尾) - &gt; next =(在List2中你离开的地方)。
另外,不要在合并功能中返回。你不需要。因为yoru列表已经占据了空间,所以你只是转换指针所指向的位置。一旦完成所有指针,只需从函数返回一个像往常一样的访问List1 / List2(除了你需要弄清楚哪个List包含第一个指针,所以只返回1或2)
另外,你没有将合并设置为我认为不值的值
答案 2 :(得分:0)
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