| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2054 人关注过本帖
标题:单链表求集合并集!!帮忙看看写的函数哪里出错了!!跑不起!!
取消只看楼主 加入收藏
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
结帖率:25%
收藏
已结贴  问题点数:20 回复次数:5 
单链表求集合并集!!帮忙看看写的函数哪里出错了!!跑不起!!
#include<stdio.h>  
#include<stdlib.h>  
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

//Status 为函数的类型,其值是函数结果状态代码,如OK等
typedef int Status;

// ElemType为数据元素类型,根据实际情况而定,这里假设为int
typedef int ElemType;  

//单链表存储结构节点定义  y
typedef struct LNode{  
    ElemType data;  
    struct LNode *next;  
}LNode;

struct LNode *Union(struct LNode *La,struct LNode *Lb) ;
struct LNode *creatlink(int n);
void print(struct LNode *h);
int GetElem(struct LNode *L,int i);
struct LNode *ListInsert(struct LNode *L,int i,ElemType e);
int ListLength(struct LNode *h);
typedef struct LNode *LinkList;


int  main()
{  
    //完成union操作
    int n;
    struct LNode *La, *Lb;     
    printf("\n创建线性表4个节点的La:\n");   
    La=creatlink(4);//初始化并创建线性表La   
    print(La);   
    printf("\n创建线性表5个节点的Lb:\n");   
    Lb=creatlink(5);//初始化并创建线性表Lc   
    print(Lb);
    n=ListLength(La);   
    printf("\nUnion以后得到的La:\n");   
    La=Union(La,Lb);

    print(La);   
}  
struct LNode *creatlink(int n)
{
    struct LNode *head;
    struct LNode *p;
    struct LNode *q;
    int i;

    q=(struct LNode *)malloc(sizeof(struct LNode ));
    head=NULL;
    printf("Please input the data : ");
    for(i = 0; i < n; i++)
    {
        p=(struct LNode *)malloc(sizeof(struct LNode));
        scanf("%d", &p->data);
        if(head==NULL)
            head=p;
        else
            q->next=p;
        q=p;
    }
    q->next=NULL;
    return head;
}

void print(struct LNode *h)
{
    while(h!=NULL)
    {
        printf("%d —> ",h->data);
        h = h -> next;
    }
    puts("NULL");
}
struct LNode *Union(struct LNode *La,struct LNode *Lb)
{
    int i,e;
    struct LNode *h,*p=La;
    // 将所有在表Lb(代表B集合)中但不在La(代表A集合)中的数据元素插入到La中
   int La_len=ListLength(La); // 求表La的长度
   int Lb_len=ListLength(Lb);
   for(i=1;i<=Lb_len;i++)
   {
     e=GetElem(Lb,i); // 取表Lb中第i个数据元素赋给变量e

         while(p)
        {
            if(p->data == e)
                break;
            else
                p=p->next ;
        }                            // 表La中不存在和e相同的元素,则将e插入La中
     if(p==NULL)
         h=ListInsert(La,++La_len,e);
 }
   return h;
 }

struct LNode *ListInsert(struct LNode *L,int i,ElemType e)
{
    int m=0;
    struct LNode *p,*q;
    p=(struct LNode * )malloc(sizeof(struct LNode ));
    p->data = e;
    q=L;
    while( m < i )
    {
        q=q->next;
        m++;
    }
    p->next=q->next ;
    q->next = p;
    return L;

}  

int GetElem(struct LNode *L,int i)  // 2
 {
    int e;
                                    /* L为单链表的头指针。当第i个元素存在时,其值赋给e并返回OK,否则返回ERROR */
   int j=1;                            /* j为计数器 */
    struct LNode * p=L;                /* p指向第一个结点 */
   while(p&&j<i)                /* 顺指针向后查找, 直到p指向第i个元素 或p为空 */
   {
     p=p->next;
     j++;
   }                            /* 第i个元素不存在 */
   e=p->data;
   return e;/* 取第i个元素 */
 
 }

int ListLength(struct LNode *h)  // 1
{
    int n=0;
    while(h!=NULL)
    {
        n++;
        h = h -> next;
    }
    return n;
}




[此贴子已经被作者于2017-5-23 20:13编辑过]

搜索更多相关主题的帖子: int 元素 struct next NULL 
2017-05-23 01:18
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
0 erroer  0 warnings  就是不晓得到底哪里错了!!求助!!
2017-05-23 01:22
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
回复 3楼 Emotiona
看看呗!!
2017-05-23 01:25
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
回复 3楼 Emotiona
就是调不对!啊啊啊啊啊啊
2017-05-23 01:25
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
回复 6楼 Emotiona
恩!我看看!再调调!谢谢指正!!!
2017-05-23 01:40
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
回复 6楼 Emotiona
单个函数都是对的!!就是合在一起就。。。也不晓得是为啥?!
2017-05-23 01:57
快速回复:单链表求集合并集!!帮忙看看写的函数哪里出错了!!跑不起!!
数据加载中...
 
   



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

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