SDUT2119数据结构实验之链表四:有序链表的归并
2016-07-23 09:18
399 查看
#include<bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }*head1,*head2,*tail,*p,*q; int n1,n2; struct node * built(int n) { struct node *head; head=(struct node *)malloc(sizeof(struct node)); tail=head; while(n--) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); tail->next=p; tail=p; } tail->next=NULL; return head; } void print(struct node *head) { p=head->next; while(p) { printf("%d",p->data); if(p->next) printf(" "); p=p->next; } } void merger() { struct node *p1,*p2; p1=head1->next; p2=head2->next; tail=head1; while(p1&&p2) { if(p1->data<p2->data) { tail->next=p1; tail=p1; p1=p1->next; } else { tail->next=p2; tail=p2; p2=p2->next; } } if(p1) tail->next=p1; else tail->next=p2; } int main() { scanf("%d%d",&n1,&n2); head1=built(n1); head2=built(n2); //print(head1); //print(head2); //printf("\n"); merger(); print(head1); }
相关文章推荐
- 复习(数据结构):队列:c语言
- 优先队列与堆
- SDUT3341数据结构实验之二叉树二:遍历二叉树
- Redis与Java - 数据结构
- Redis与Java - 数据结构
- Redis与Java - 数据结构
- Core_1_数据结构和函数
- 【排序】稳定排序和不稳定排序
- 数据结构中的各种排序方法小结(JS实现)
- 数据结构 线性链表栈
- SDUT3343数据结构实验之二叉树四:还原二叉树
- SDUT2137数据结构实验之求二叉树后序遍历和层次遍历
- 数据结构-----------B树
- 数据结构与算法 1 :基本概念,线性表顺序结构,线性表链式结构,单向循环链表
- SDUT2136数据结构实验之二叉树的建立与遍历
- 单链表
- 树状数组 ( 基础篇 )——敌兵布阵 ( HDU 1166 )
- SDUT2118数据结构实验之链表三:链表的逆置
- AVL树的插入与删除操作
- 数据结构-串操作应用之词索引表