| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 783 人关注过本帖
标题:[求助]两个链表合并出了点问题
只看楼主 加入收藏
Arcsacy
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-6-2
收藏
 问题点数:0 回复次数:1 
[求助]两个链表合并出了点问题
两个链表合并 出了点问题

/*系统提示说 我没模块 怎么回事*/
typedef int elemtype;
#include<stdio.h>
#define Max 100
#define error 0
#define ok 1
typedef struct
{
elemtype *elem;
int length;
}sqlist;

int Initlist_sq(sqlist *L)/*初始化链表*/
{
L->elem=(elemtype *)malloc(Max*sizeof(elemtype));
if(L->elem==0)
return error;
L->length=0;
return ok;
}

int listlength(sqlist *L)/*定义长度函数*/
{
return L->length;
}

int locate(sqlist *L,elemtype e)/*哪e与链表l中的元素比较*/
{
int i,L_len;
L_len=listlength(L);
for(i=0;i<L_len;i++)
{
if(L->elem[i]==e)
return ok;
else return error;
}
}

int listintsert(sqlist *L,int i,elemtype e)/*在链表l中第i个地方插入e*/
{
int j,L_len;
L_len=listlength(L);
for(j=L_len;i>=i;j--)
{
L->elem[j]=L->elem[j-1];
L->elem[i-1]=e;
L->length++;
}
}

int Getelem(sqlist *L,int i,elemtype *e)/*提出元素*/
{
if(i<1||i>listlength(L))
return error;
*e=L->elem[i-1];
return ok;
}

int Makeup(sqlist *La,sqlist *Lb)/*合并两链表*/
{
int La_len,Lb_len,i,e;
La_len=listlength(La);
Lb_len=listlength(Lb);
for(i=1;i<Lb_len;i++)
{
Getelem(Lb,i,&e);
if(!locate(La,e))
listinsert(La,++La_len,e);
}
}

main()
{
int i,n,m;
sqlist A,B;
Initlist(&A);
Initlist(&B);
scanf("%d,%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&A.elem[i]);
for(i=0;i<m;i++)
scanf("%d",&B.elem[i]);
makeup(&A,&B);
for(i=0;i<A.length;i++)
printf("%d",A.elem[i]);

搜索更多相关主题的帖子: 链表 点问题 elemtype int Max 
2006-06-09 11:19
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
以下是引用Arcsacy在2006-6-9 11:19:30的发言:
两个链表合并 出了点问题

/*系统提示说 我没模块 怎么回事*/
typedef int elemtype;
#include<stdio.h>/* #include <stdilb.h> 怎么没有 */
#define Max 100
#define error 0
#define ok 1
typedef struct
{
elemtype *elem;
int length;
}sqlist;

int Initlist_sq(sqlist *L)/*初始化链表*/
{
L->elem=(elemtype *)malloc(Max*sizeof(elemtype));
if(L->elem==0)
return error;
L->length=0;
return ok;
}

int listlength(sqlist *L)/*定义长度函数*/
{
return L->length;
}

int locate(sqlist *L,elemtype e)/*哪e与链表l中的元素比较*/
{
int i,L_len;
L_len=listlength(L);
for(i=0;i<L_len;i++)
{
if(L->elem[i]==e)
return ok;
else return error;
}
}

int listintsert(sqlist *L,int i,elemtype e)/*在链表l中第i个地方插入e*/
{
int j,L_len;
L_len=listlength(L);
for(j=L_len;i>=i;j--)
{
L->elem[j]=L->elem[j-1];
L->elem[i-1]=e;
L->length++;
}
}

int Getelem(sqlist *L,int i,elemtype *e)/*提出元素*/
{
if(i<1||i>listlength(L))
return error;
*e=L->elem[i-1];
return ok;
}

int Makeup(sqlist *La,sqlist *Lb)/*合并两链表*/
{
int La_len,Lb_len,i,e;
La_len=listlength(La);
Lb_len=listlength(Lb);
for(i=1;i<Lb_len;i++)
{
Getelem(Lb,i,&e);
if(!locate(La,e))
listinsert(La,++La_len,e);/* 函数写错了.*/
}
}

main()
{
int i,n,m;
sqlist A,B;
Initlist(&A);/* 不感觉这个函数你少写点什么吗? */
Initlist(&B);
scanf("%d,%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&A.elem[i]);
for(i=0;i<m;i++)
scanf("%d",&B.elem[i]);
makeup(&A,&B);/* Makeup */
for(i=0;i<A.length;i++)
printf("%d",A.elem[i]);

算法有问题.自己找吧!

[此贴子已经被作者于2006-6-10 7:54:43编辑过]


[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-06-10 07:52
快速回复:[求助]两个链表合并出了点问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014567 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved