两有序链表组成一个有序链表,只能莫名为空?请大神看一看!
程序代码:
#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct _tag_linklistnode TLinkListNode; struct _tag_linklistnode { TLinkListNode* next; }; struct Value { TLinkListNode* next; int data; }; typedef struct _tag_linklist { TLinkListNode header; int length; }TLinkList; void LinkList_ShowList(TLinkList* list); TLinkList* LinkList_Create() { TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList)); if(ret != NULL) { ret->header.next = NULL; ret->length = 0; } return ret; } int LinkList_Length(TLinkList* list) { int ret = -1; if(list != NULL) { ret = list->length; } return ret; } int LinkList_Insert(TLinkList* list,TLinkListNode* node,int pos) { int ret = 1; ret = ret && (list != NULL) && (node != NULL) && (pos >= 0); if(ret) { TLinkListNode* current = (TLinkListNode*)list; for(int i = 0;(i < pos) && (current->next != NULL);i++) { current = current->next; } node->next = current->next; current->next = node; list->length++; } return ret; } TLinkListNode* LinkList_Delete(TLinkList* list,int pos) { TLinkListNode* ret = NULL; if(list != NULL && pos >= 0 && pos < list->length) { TLinkListNode* current = (TLinkListNode*)list; for(int i = 0;i < pos;i++) { current = current->next; } ret = current->next; ret->next = current->next; list->length--; } return ret; } TLinkListNode* LinkList_Get(TLinkList* list,int pos) { TLinkListNode* ret = NULL; if(list != NULL && pos >= 0 && pos < list->length) { TLinkListNode* current = (TLinkListNode*)list; for(int i = 0;i < pos;i++) { current = current->next; } ret = current->next; } return ret; } TLinkList* LinkList_Belind(TLinkList* list1,TLinkList* list2) { if(list1 == NULL && list2 == NULL) return NULL; if(list1 == NULL) return list2; if(list2 == NULL) return list1; TLinkList* list3 = LinkList_Create(); TLinkListNode* first = list1->header.next; TLinkListNode* second = list2->header.next; while(first != NULL && second != NULL) { if(((struct Value*)first)->data > ((struct Value*)second)->data) { LinkList_Insert(list3,second,LinkList_Length(list3)); second = second->next; } if(((struct Value*)first)->data <= ((struct Value*)second)->data) { LinkList_Insert(list3,first,LinkList_Length(list3)); first = first->next; //用VC调试,发现first->next值为空,为什么呢? } } while(first != NULL) { LinkList_Insert(list3,first,LinkList_Length(list3)); first = first->next; } while(second != NULL) { LinkList_Insert(list3,second,LinkList_Length(list3)); second = second->next; } return list3; } void LinkList_ShowList(TLinkList* list) { if(list == NULL) return; TLinkListNode* current = list->header.next; while(current != NULL) { printf("%d ",((struct Value*)current)->data); current = current->next; } } int main() { struct Value v1; v1.data = 1; struct Value v2; v2.data = 2; struct Value v3; v3.data = 3; struct Value v4; v4.data = 4; struct Value v5; v5.data = 5; struct Value v6; v6.data = 6; struct Value v7; v7.data = 7; struct Value v8; v8.data = 8; struct Value v9; v9.data = 9; TLinkList* List1 = LinkList_Create(); TLinkList* List2 = LinkList_Create(); LinkList_Insert(List1,(TLinkListNode*)&v1,LinkList_Length(List1)); LinkList_Insert(List2,(TLinkListNode*)&v2,LinkList_Length(List2)); LinkList_Insert(List1,(TLinkListNode*)&v3,LinkList_Length(List1)); LinkList_Insert(List2,(TLinkListNode*)&v4,LinkList_Length(List2)); LinkList_Insert(List1,(TLinkListNode*)&v5,LinkList_Length(List1)); LinkList_Insert(List2,(TLinkListNode*)&v6,LinkList_Length(List2)); LinkList_Insert(List1,(TLinkListNode*)&v7,LinkList_Length(List1)); LinkList_Insert(List2,(TLinkListNode*)&v8,LinkList_Length(List2)); LinkList_Insert(List2,(TLinkListNode*)&v9,LinkList_Length(List2)); LinkList_ShowList(List1); LinkList_ShowList(List2); printf("\nList1 Length = %d\n",LinkList_Length(List1)); printf("List2 Length = %d\n",LinkList_Length(List2)); TLinkList* newList = LinkList_Belind(List1,List2); printf("Len = %d\n",LinkList_Length(newList)); for(int i = 0;i < LinkList_Length(newList);++i) { // printf("%d\n",((struct Value*)LinkList_Get(newList,i))->data); } return 0; }