| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 626 人关注过本帖
标题:数据结构 求集合的并,交,差, 运行的结果不对,但是找不出错误,求帮助
只看楼主 加入收藏
cailu
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-6
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:4 
数据结构 求集合的并,交,差, 运行的结果不对,但是找不出错误,求帮助
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct SqList
{
    char ch;
    struct SqList * next;
} SqList, * pSqList;

void mainmenu();
pSqList creatb();
void print(pSqList);
pSqList Union (pSqList, pSqList);
pSqList Insertsection (pSqList, pSqList);
pSqList Difference (pSqList, pSqList);
void Delrepeat(pSqList);


int main(void)
{
    pSqList va_head, vb_head, vc_head;
    va_head = NULL;
    vb_head = NULL;
    vc_head = NULL;
    int a = 0;
    int flag = 0;
   
    while(a!=5)
    {
        mainmenu();
        scanf("%d",&a);
        switch(a)
        {
        case 1:
            printf("请输入链表中各结点的数据,输入#表示输入结束:\n");
            va_head = creatb();
            printf("输入的第一个链表为:");
            print(va_head);

            printf("请输入链表中各结点的数据,输入#表示输入结束:\n");
            vb_head = creatb();
            printf("输入的第二个链表为:");
            print(vb_head);
            Delrepeat(va_head);
            Delrepeat(vb_head);
            flag = 1;
            break;
        case 2:
            if(flag==1)
            {
                vc_head = Union(va_head, vb_head);
                printf("完成并运算后的集合为:");
                print(vc_head);
            }
            else
            {
                printf("请先执行1操作\n");
            }
            break;
        case 3:
            if(flag == 1)
            {
              vc_head = Insertsection(va_head,vb_head);
              printf("完成交运算后的集合为:");
              print(vc_head);
            }
            else
            {
                printf("请先执行1操作\n");
            }
            break;
        case 4:
            if(flag == 1)
            {
                 vc_head = Difference(va_head,vb_head);
                 printf("完成差运算后的集合为:");
                 print(vc_head);
            }
            break;
        case 5:
            exit(0);
            break;
        default:
            printf("输入错误!\n");
            break;
        }
    }

    return 0;
}

void mainmenu()
{
   printf("\n\n");
   printf("*******************欢迎进入链表操作系统,请选择操作***************\n");
   printf("1.输入单链表(尾部插入)\n");
   printf("2.运行集合的并运算\n");
   printf("3.运行集合的交运算\n");
   printf("4.运行集合的差运算\n");
   printf("5.结束操作退出系统********************\n");

}
pSqList creatb()
{
    pSqList head = (pSqList)malloc(sizeof(SqList));
    pSqList p = (pSqList)malloc(sizeof(SqList));
    pSqList tail;

    head->next = NULL;
    tail = head;
    p->ch = getchar();
    while (p->ch != '#')
    {
        tail->next = p;
        tail = p;
        p = (pSqList)malloc(sizeof(SqList));
        p->ch=getchar();
    }
    tail->next = NULL;
   
    return head;
}

void print(pSqList head)  
{
    pSqList p;
    p = head->next;
   
    if(head == NULL)
    {
        printf("该表为空表");
    }
    else
    {
        while(p!=NULL)
        {
            printf("%c  ", p->ch);
            p = p->next;
        }   
    }
    printf("\n");
}

void Delrepeat(pSqList head)
{
    pSqList p, q;
    p = head->next;
    while(p!=NULL)
    {
        q = p->next;
        while(q!=NULL && q->ch!=p->ch)
            q = q->next;
        if(q!=NULL)
        {
            p->next = q->next ;
            free(q);
        }
        p=p->next ;
    }
}

pSqList Union (pSqList head1, pSqList head2)
{
    pSqList head = (pSqList)malloc(sizeof(SqList));
    head->next = NULL;   
    pSqList tail, p;
    tail = head;
    pSqList p1 = head1->next;
    pSqList p2 = head2->next;

    while(p1!=NULL)
    {
        pSqList s = (pSqList)malloc(sizeof(SqList));  
        s->ch=p1->ch;
        s->next = NULL;
        tail->next = s;
        tail = s;
        p1 = p1->next;
    }

    while(p2!=NULL)
    {
        p = head->next;
        while(p!=NULL && (p2->ch != p->ch))
            p = p->next;
        if(p==NULL)
        {
            pSqList s = (pSqList)malloc(sizeof(SqList));
            s->ch = p2->ch;
            s->next = NULL;
            tail->next = s;
            tail = s;
        }
        p2 = p2->next;
    }

    tail->next = NULL;
   
    return head;
}

pSqList Insertsection (pSqList head1, pSqList head2)
{
    pSqList head = (pSqList)malloc(sizeof(SqList));
    head->next = NULL;   
    pSqList tail;
    tail = head;
    pSqList p1 = head1->next;
    pSqList p2;

    while(p1!=NULL)
    {
        p2 = head2->next;
        while(p2!=NULL && (p1->ch != p2->ch))
            p2 = p2->next;
        if(p2 != NULL)
        {
            pSqList s = (pSqList)malloc(sizeof(SqList));
            s->ch = p1->ch;
            s->next = NULL;
            tail->next = s;
            tail = s;
        }
         p1 = p1->next;
    }
    tail->next = NULL;

    return head;
}

pSqList Difference (pSqList head1, pSqList head2)
{
    pSqList head = (pSqList)malloc(sizeof(SqList));
    head->next = NULL;   
    pSqList p, q, tail;
    tail = head;
    pSqList p1 = head1->next;
    pSqList p2;

    while(p1!=NULL)
    {
        pSqList s = (pSqList)malloc(sizeof(SqList));  
        s->ch=p1->ch;
        tail->next = s;
        tail = s;
        p1 = p1->next;
    }
    tail->next = NULL;
    p = head->next;
    q = head;

    while(q->next!=NULL)
    {
        p2 = head2->next;
        while(p2!=NULL && (p->ch != p2->ch))
            p2 = p2->next;
        if(p2!=NULL)
        {
            q->next = p->next;
            free(p);
            p = q->next;
        }
        else
        {
            q = p;
            p = p->next ;
        }
    }

    return head;
}
这个数据结构的问题,是求集合的并,交,差.   找不出其中的错误,求帮助
搜索更多相关主题的帖子: include void 
2012-11-06 15:44
cailu
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-6
收藏
得分:0 
或者有谁可以告诉我,怎么去学吗?应该买什么书??谢谢了
2012-11-06 21:37
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
建议你一个功能一个功能地调试   保证了单个功能的运行正常   然后再调试整体的关联问题
2012-11-07 08:48
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
nn*******************欢迎进入链表操作系统,请选择操作***************
1.输入单链表(尾部插入)
2.运行集合的并运算
3.运行集合的交运算
4.运行集合的差运算
5.结束操作退出系统********************
1
请输入链表中各结点的数据,输入#表示输入结束:
1234567#
输入的第一个链表为:
  1  2  3  4  5  6  7
请输入链表中各结点的数据,输入#表示输入结束:
2345678#
输入的第二个链表为:
  2  3  4  5  6  7  8
nn*******************欢迎进入链表操作系统,请选择操作***************
1.输入单链表(尾部插入)
2.运行集合的并运算
3.运行集合的交运算
4.运行集合的差运算
5.结束操作退出系统********************
2
完成并运算后的集合为:
  1  2  3  4  5  6  7  8
nn*******************欢迎进入链表操作系统,请选择操作***************
1.输入单链表(尾部插入)
2.运行集合的并运算
3.运行集合的交运算
4.运行集合的差运算
5.结束操作退出系统********************
3
完成交运算后的集合为:
  2  3  4  5  6  7
nn*******************欢迎进入链表操作系统,请选择操作***************
1.输入单链表(尾部插入)
2.运行集合的并运算
3.运行集合的交运算
4.运行集合的差运算
5.结束操作退出系统********************
4
完成差运算后的集合为:1
nn*******************欢迎进入链表操作系统,请选择操作***************
1.输入单链表(尾部插入)
2.运行集合的并运算
3.运行集合的交运算
4.运行集合的差运算
5.结束操作退出系统********************
5
Press any key to continue
2012-11-07 09:25
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
void Delrepeat(pSqList head)

 {
     pSqList p, q;
     p = head->next;
     while(p!=NULL)
     {
         q = p->next;
         while(q!=NULL && q->ch!=p->ch)
             q = q->next;
         if(q!=NULL)
         {
             p->next = q->next ;
             free(q);
         }
         p=p->next ;
     }

 }
//上面函数是什么意思   如果是除掉重复的字符  那这个函数是错误的
2012-11-09 09:06
快速回复:数据结构 求集合的并,交,差, 运行的结果不对,但是找不出错误,求帮 ...
数据加载中...
 
   



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

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