注册 登录
编程论坛 C语言论坛

单链表求集合并集!!帮忙看看写的函数哪里出错了!!跑不起!!

程火山 发布于 2017-05-23 01:18, 2042 次点击
#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编辑过]

8 回复
#2
程火山2017-05-23 01:22
0 erroer  0 warnings  就是不晓得到底哪里错了!!求助!!
#3
Emotiona2017-05-23 01:23
这个就不帮你看了, 自己琢磨下,睡觉
#4
程火山2017-05-23 01:25
回复 3楼 Emotiona
看看呗!!
#5
程火山2017-05-23 01:25
回复 3楼 Emotiona
就是调不对!啊啊啊啊啊啊
#6
Emotiona2017-05-23 01:29
你研究好了另外一个这个也不难。我给你把另外一个更正了
#7
程火山2017-05-23 01:40
回复 6楼 Emotiona
恩!我看看!再调调!谢谢指正!!!
#8
程火山2017-05-23 01:57
回复 6楼 Emotiona
单个函数都是对的!!就是合在一起就。。。也不晓得是为啥?!
#9
Emotiona2017-05-25 21:16
为什么又是一个相似度很高的帖子。
求链表并集简单思路:
1.创建两个链表,分别在创建的时候去重,也可以在链表合并后去重。
2.去重可以用指针排序去重, 可以用指针遍历去重, 也可以装到数组中排序去重。
这里使用的指针遍历:
程序代码:
#include<stdio.h>
#include<stdlib.h>

struct LNode
{
    int data;
    struct LNode *next;
};
typedef struct LNode LinkList;//表的头指针类型         //定义的结构体别名为指针类型

LinkList *CreateListF(int a[], int n);       //把结构体指针定义到创建函数中
LinkList *Union(LinkList *La, LinkList *Lb);

LinkList *CreateListF(int *a, int n)//创建一个单链表
{
    LinkList *L, *s, *s1;
    int i;

    L = NULL;
    for(i=0; i<n; i++)
    {

        s=(LinkList *)malloc(sizeof(LinkList));//创建新结点
        if(L == NULL)
        L = s;
        else
        s1->next = s;
        s1 = s;
        s->data = a[i];
    }
    s1->next = NULL;
    s = NULL;
    return L;
}

LinkList *Union(LinkList *La, LinkList *Lb)         //集合去重,遍历整个表
{
    LinkList *p, *p1, *p2, *head, *temp;
    head = p = p2 = La;

    while(La != NULL)
    {
      p1 = La;
      La = La->next;
    }
    p1->next = Lb;
    while(p != NULL)
    {
        while(p2 != NULL)
        {

            if(p2->data == p->data && p != p2)
            {
                temp->next = p2->next;
                free(p2);
                p2 = temp->next;
            }
            else
            {
                temp = p2;
                p2 = p2->next;
            }
        }
        p = p->next;
        p2 = p;
    }
    return head;
}

void print(LinkList *p)
{
    while(p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

int main(void)
{

    int a[60], b[30];
    int i, e;
    LinkList *headA = NULL;
    LinkList *headB = NULL;

    printf("请输入A:");
    for(i=0; i < 10 ; i++)
    scanf("%d", &a[i]);
    headA = CreateListF(a, 10);
    print(headA);

    printf("请输入B:");
    for(i=0; i<5; i++)
    scanf("%d", &b[i]);
    headB = CreateListF(b, 5);
    print(headB);

    headA = Union(headA, headB);
    printf("集合A和集合B并集: \n");
    print(headA);
    return 0;
}

1