将两个有序链表合并为一个有序链表
2012-10-02 21:55
483 查看
struct link_node{ int data ; struct link_node *next ; } ; struct link_node *create_linklist(int arr[],int len) { assert(arr!=NULL) ; struct link_node *p_head = (struct link_node*)malloc( sizeof(struct link_node)) ; struct link_node *p_tmp = p_head ; for(int i=0 ; i<len ; i++){ p_tmp->next = (struct link_node*)malloc( sizeof(struct link_node)) ; p_tmp->next->data = arr[i] ; p_tmp = p_tmp->next ; } p_tmp->next = NULL ; return p_head ; } void print_linklist(struct link_node *p_head){ assert(p_head!=NULL) ; struct link_node *p_tmp = p_head->next ; while(p_tmp!=NULL){ cout<<p_tmp->data<<"," ; p_tmp = p_tmp->next ; } cout<<endl ; } int judge_reverse(struct link_node *p_head){ assert(p_head!=NULL && p_head->next!=NULL && p_head->next->next!=NULL) ; return p_head->next->data>p_head->next->next->data ; } struct link_node * bubble_sort(struct link_node *p_head){ assert(p_head!=NULL) ; struct link_node *p_tmp = p_head ; struct link_node *p_tail = NULL ; struct link_node *p_index = p_head ; /*note: termination condition*/ while(p_tail!=p_head->next&&p_tail!=p_head->next->next){ p_index = p_head ; while(p_index->next != p_tail && p_index->next->next != p_tail){ if(p_index->next->data > p_index->next->next->data){ p_tmp = p_index->next ; p_index->next = p_tmp->next ; p_tmp->next = p_index->next->next ; p_index->next->next = p_tmp ; } p_index = p_index->next ; } p_tail = p_index->next ; } return p_head ; } struct link_node *merge_linklist(struct link_node *p_head1, struct link_node *p_head2){ assert(p_head1!=NULL && p_head2!=NULL) ; struct link_node *p_tmp = NULL ; struct link_node *p_list1 = NULL ; struct link_node *p_list2 = NULL ; p_list1 = p_head1->next->data<p_head2->next->data?p_head1:p_head2 ; p_list2 = p_head1->next->data<p_head2->next->data?p_head2:p_head1 ; while(p_list1->next!=NULL && p_list2->next!=NULL){ if(p_list1->next->data>p_list2->next->data){ /*remove the smaller node from list2*/ p_tmp = p_list2->next ; p_list2->next = p_tmp->next ; /*insert the node into list1*/ p_tmp->next = p_list1->next ; p_list1->next = p_tmp ; } p_list1=p_list1->next ; } if(p_list1->next==NULL && p_list2->next!=NULL){ p_list1->next = p_list2->next ; } return p_head1->next==NULL?p_head2:p_head1 ; } int count_linklist(struct link_node *const p_head){ assert(p_head!=NULL) ; int count = 0 ; struct link_node *p_tmp = p_head->next ; while(p_tmp!=NULL){ count++ ; p_tmp = p_tmp->next ; } return count ; } int main() { int arr1[]={20,12,31,34,42,24,46} ; int arr2[]={2,4,3,78,1,51,9,432,5,7,8,11,32,20,12,24,31,21} ; struct link_node *p_head1 = create_linklist(arr1,sizeof(arr1)/sizeof(arr1[0])) ; p_head1 = bubble_sort(p_head1) ; print_linklist(p_head1) ; struct link_node *p_head2 = create_linklist(arr2,sizeof(arr2)/sizeof(arr2[0])) ; p_head2= bubble_sort(p_head2) ; print_linklist(p_head2) ; struct link_node *p_tmp = merge_linklist(p_head1,p_head2) ; print_linklist(p_tmp) ; cout<<"sizeof(arr1)="<<sizeof(arr1)/4<<endl ; cout<<"sizeof(arr2)="<<sizeof(arr2)/4<<endl ; cout<<"sizeof(merge)="<<count_linklist(p_tmp)<<endl ; }
相关文章推荐
- 每天一个小算法(2)----合并两个有序链表
- 两个链表有序 合并成一个链表
- 两个递增有序链表合并为一个链表
- 链表操作(将两个链表合并成一个链表依然有序,非递归方法和迭代方法)
- l链表操作(合并两个有序链表为一个新链表)
- 9、单链表的合并,两个有序单链表,合并为一个有序链表。
- 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。使用非递归方法以及递归方法。
- 每天一道LeetCode-----合并两个/多个有序链表为一个新链表
- 将两个非递减的有序链表合并为一个非递增的有序链表
- 将两个有序链表合并为一个新链表
- 将两个递增有序的单链表合并成一个递减有序的单链表,利用原结点空间
- c语言数据结构中两个有序链表合并为一个新链表
- 将两个有序链表合并成一个有序链表
- 将两个有序链表合并成一个有序链表
- 数据结构之将两个递增的有序链表合并为一个递增的有序链表
- 将两个递增有序的单链表合并成一个递减有序的单链表,利用原结点空间
- 两个有序链表合并为一个有序链表
- 写程序。设ha和hb分别是两个带头结点的非递减有序单链表的头指针,试设计算法,将这两个有序链表合并成一个非递增有序的单链表。要求使用原链表空间,
- 将两个有序链表合并成一个有序链表
- 将两个有序链表合并成一个新的有序链表