作业二之合并链表
2010-03-21 16:01
148 查看
#include<stdio.h>//链表没有长度的限制
#include<malloc.h>
typedef struct node
{
int data;
struct node* next;
}Node,*Lnode;//定义结点
void insert(Lnode head) //插入数据
{
int i;
Lnode q; //定义全局变量q,用q来辅助构造链表
for(i=0;i<3;i++)
{
Lnode p=(Lnode)malloc(sizeof(Node));//创建新结点
printf("please input the num: ");
scanf("%d",&(p->data));
if(head->next==NULL)
{
head->next=p;
p->next=NULL;
q=head->next;
}
else
{
q->next=p;
p->next=NULL;
q=p;
}
}
}
void print(Lnode head)
{
int i;
Lnode p=head->next;//指向第一个结点
for(i=0;i<6;i++)
{
printf("%d ",p->data);
p=p->next;
}
}
void unite(Lnode *head1,Lnode *head2,Lnode *lc)//如果有2个'*',就是&head,一个就直接是head
{
Lnode p1,p2,p3,p4,p5;
int i,j;
p1=*head1;//指向头结点
p2=*head2;
p3=p1->next;//指向第一个结点
p4=p2->next;
p5=*lc=*head1;
for(;p3&&p4;)
{
if(p3->data<=p4->data) {p5->next=p3;p5=p3;p3=p3->next;}
else {p5->next=p4;p5=p4;p4=p4->next;}
}
p5->next=p3?p3:p4;
}
void main()//合并链表千万不要怕设置参数,可以多设几个参数来进行合并
{
Lnode head1,head2,head3;
int i,j;
head1=(Lnode)malloc(sizeof(Node));
head1->next=NULL;//创建空链表
head2=(Lnode)malloc(sizeof(Node));
head2->next=NULL;
head3=(Lnode)malloc(sizeof(Node));
head3->next=NULL;
printf("init head1 /n");
insert(head1);
printf("init head2 /n");
insert(head2);
unite(&head1,&head2,&head3);
print(head3);
}
#include<malloc.h>
typedef struct node
{
int data;
struct node* next;
}Node,*Lnode;//定义结点
void insert(Lnode head) //插入数据
{
int i;
Lnode q; //定义全局变量q,用q来辅助构造链表
for(i=0;i<3;i++)
{
Lnode p=(Lnode)malloc(sizeof(Node));//创建新结点
printf("please input the num: ");
scanf("%d",&(p->data));
if(head->next==NULL)
{
head->next=p;
p->next=NULL;
q=head->next;
}
else
{
q->next=p;
p->next=NULL;
q=p;
}
}
}
void print(Lnode head)
{
int i;
Lnode p=head->next;//指向第一个结点
for(i=0;i<6;i++)
{
printf("%d ",p->data);
p=p->next;
}
}
void unite(Lnode *head1,Lnode *head2,Lnode *lc)//如果有2个'*',就是&head,一个就直接是head
{
Lnode p1,p2,p3,p4,p5;
int i,j;
p1=*head1;//指向头结点
p2=*head2;
p3=p1->next;//指向第一个结点
p4=p2->next;
p5=*lc=*head1;
for(;p3&&p4;)
{
if(p3->data<=p4->data) {p5->next=p3;p5=p3;p3=p3->next;}
else {p5->next=p4;p5=p4;p4=p4->next;}
}
p5->next=p3?p3:p4;
}
void main()//合并链表千万不要怕设置参数,可以多设几个参数来进行合并
{
Lnode head1,head2,head3;
int i,j;
head1=(Lnode)malloc(sizeof(Node));
head1->next=NULL;//创建空链表
head2=(Lnode)malloc(sizeof(Node));
head2->next=NULL;
head3=(Lnode)malloc(sizeof(Node));
head3->next=NULL;
printf("init head1 /n");
insert(head1);
printf("init head2 /n");
insert(head2);
unite(&head1,&head2,&head3);
print(head3);
}
相关文章推荐
- 【数据结构作业二】写出单链表结点的结构体类型定义及查找、插入、删除算法,并以单链表作存储结构,实现有序表的合并
- LeetCode 21. 合并两个有序链表
- 16 合并两个排序的链表
- 将两递增链表合并,合并后的链表为递减的顺序
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 剑指offer-面试题17.合并两个排序的链表
- 合并两个排序的链表
- 数据结构:头插法、尾插法创建单链表,求链表长度、查找、插入、删除、合并
- 剑指offer:合并两个排序的链表
- 合并两个已有序(升序)链表的操作!
- 合并两个有序链表
- 剑指offer--合并两个排序的链表
- merge two sort list-leetcode 有序链表合并的二级指针简洁非递归解法
- 合并链表 【微软面试100题 第二十四题】
- 作业1.452:删除链表中的元素
- C语言程序设计教程(第三版)课后习题11.8 链表合并及操作
- 《剑指offer》牛客网java题解-合并两个排序的链表
- 合并两个排序的链表