鱼C论坛

 找回密码
 立即注册
查看: 3564|回复: 1

[技术交流] 研究了一个星期的单链表合并算法c语言实现devC++正常编译运行

[复制链接]
发表于 2011-8-16 18:03:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFELOW -2
#include<stdio.h>
#include<malloc.h>

typedef int Status;
typedef int ElemType;

typedef struct node
{
ElemType data;
struct node * next;
}node, *LinkList;

LinkList creat(void);
void Print(LinkList head);
void MergeList(LinkList head1,LinkList head2,LinkList head3);

int main(void)
{
LinkList La_head = NULL;
LinkList Lb_head = NULL;
LinkList Lc_head = (LinkList)malloc(sizeof(node));

La_head = creat();
Lb_head = creat();

Print(La_head);
Print(Lb_head);

MergeList(La_head,Lb_head,Lc_head);

Print(Lc_head);
system("pause");
}

LinkList creat(void)
{
int length;
int i;
int veriables;

LinkList head = (LinkList)malloc(sizeof(node));
if(NULL == head)
{
printf("No enough space!");
exit(-1);
}

LinkList tail = head;

printf("Input LinkList Length: ");
scanf("%d",&length);
printf("\n");

for(i=0;i<length;++i)
{
printf("Input number %d elem: ",i+1);
scanf("%d",&veriables);

LinkList pnew = (LinkList)malloc(sizeof(node));
if(NULL == pnew)
{
printf("No enough space!");
exit(-1);
}

pnew->data = veriables;
tail->next = pnew;
pnew->next = NULL;
tail = pnew;
}
printf("\n\n");
return (head);
}

void Print(LinkList head)
{
LinkList p = head->next;

while(p)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n\n");
}

void MergeList(LinkList head1,LinkList head2,LinkList head3)
{
LinkList La = head1->next;
LinkList Lb = head2->next;
LinkList Lc = head3;
LinkList q;

while(La && Lb)
{
if(La->data < Lb->data)
{
Lc->next = La;
Lc = La;
La = La->next;
}
else if(La->data == Lb->data)
{
Lc->next = La;
Lc = La;
La = La->next;
q = Lb;
Lb = Lb->next;
free(q);
}
else
{
Lc->next = Lb;
Lc = Lb;
Lb = Lb->next;
}
}

Lc->next = La ? La : Lb;
free(head1);
free(head2);
}
研究了一个星期希望对大家有帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-5-17 14:07:47 | 显示全部楼层
无回帖,不论坛,这才是人道。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-24 10:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表