单链表在集合中的应用(交、并、差)

#include <iostream>
using namespace std;

#define MAXSIZE 20
#define TRUE 1
#define FALSE 0
typedef bool Status;          /* Status是函数的类型,其值是函f数结果状态代码*/
typedef char ElemType;        /* ElemType类型根据实际情况而定,这里假设为int */

typedef struct Node {
    ElemType data;
    struct Node* next;
}LinkList;

void DisplayList(LinkList* List)
{
    Node* p = List;
    while (p->next)
    {
        p = p->next;
        cout << p->data << ' ';
    }
    cout << endl;
}

void InitList(LinkList*& List)
{
    List = new LinkList;
    List->next = NULL;
}

void CreateListAtHead(LinkList*& List, ElemType arr[], int len)
{
    Node* temp;
    List = new LinkList;
    List->next = NULL; // 作为链表结束标志
    for (int i = 0; i < len; i++)
    {
        temp = new Node;
        temp->data = arr[i]; // 把数据放进去
        temp->next = List->next;
        List->next = temp;
    }
}

void CreateListAtTail(LinkList*& List, ElemType arr[], int len)
{
    Node* temp, * p; // temp用来存放临时的地址, p用来指向最后的那个节点
    List = new LinkList;
    List->next = NULL; // 作为链表结束标志
    p = List;
    for (int i = 0; i < len; i++)
    {
        temp = new Node;
        temp->data = arr[i];
        p->next = temp;
        p = temp;
    }
    p->next = NULL;
}

Status ListInsert(LinkList*& List, int position, ElemType e)
{
    if (position <= 0)
        return FALSE;
    LinkList* p = List;
    LinkList* temp;
    int count = 0;
    while (p->next && count < position - 1)
    {
        p = p->next;
        count++;
    }
    if (count != position - 1)
        return FALSE;
    temp = new Node;
    temp->data = e;
    temp->next = p->next;
    p->next = temp;
    return TRUE;
}

Status ListDelete(LinkList*& List, int position, ElemType& e)
{
    if (position <= 0)
        return FALSE;
    LinkList* p = List;
    LinkList* temp;
    int count = 0;
    while (p->next && count < position - 1)
    {
        p = p->next;
        count++;
    }
    if (count != position - 1)
        return FALSE;
    temp = p->next;
    e = temp->data;
    p->next = p->next->next;
    delete temp;
    return TRUE;
}

void ClearList(LinkList*& List)
{
    LinkList* p = List;
    if (p->next == NULL)
        return;
    else
        p = p->next;

    LinkList* c;
    while (p->next)
    {
        c = p;
        p = p->next;
        delete c;
    }
}

Status ListEmpty(LinkList* List)
{
    if (List->next == NULL)
        return TRUE;
    else
        return FALSE;
}

int ListLength(LinkList* List)
{
    LinkList* p = List;
    int count = 0;
    while (p->next)
    {
        p = p->next;
        count++;
    }
    return count;
}

Status GetElem(LinkList* List, int position, ElemType& e)
{
    LinkList* p = List;
    int count = 0;
    if (position <= 0)
        return FALSE;
    while (p->next && count < position)
    {
        p = p->next;
        count++;
    }
    e = p->data;
    return TRUE;
}

int LocateElem(LinkList* List, ElemType e)
{
    Node* p = List;
    int count = 0;
    while (p->next)
    {
        p = p->next;
        count++;
        if (p->data == e)
            return count;
    }
    return 0;
}

void DestroyList(LinkList*& List)
{
    ClearList(List);
    delete List;
}

/*去两个集合的合集, LA和LB位要去合集的对象, LC为结果*/
void UnionList(LinkList *LA, LinkList *LB, LinkList *&LC)
{ 
    int lena, lenb, i;
    ElemType e;
    lena = ListLength(LA);
    lenb = ListLength(LB);
    for(i = 1; i <= lena; i++) // 把LA放到LC上
    {
        GetElem(LA, i ,e);
        ListInsert(LC, i ,e);
    }
    for(i = 1; i <= lenb; i++)
    {
        GetElem(LB, i, e);
        if(!LocateElem(LA, e))
            ListInsert(LC, ++lena, e);
    }
}

void CommonList(LinkList *LA, LinkList *LB, LinkList *&LC)
{
    ElemType e;
    int lena = ListLength(LA);
    for(int i = 1; i <= lena; i++)
    {
        GetElem(LA, i, e);
        if(LocateElem(LB, e))
            ListInsert(LC, ListLength(LC) + 1, e);
    }
}

void DifferenceSetList(LinkList *LA, LinkList *LB, LinkList *&LC)
{
    ElemType e;
    int lena = ListLength(LA);
    for(int i = 1; i <= lena; i++)
    {
        GetElem(LA, i, e);
        if(!LocateElem(LB, e))
            ListInsert(LC, ListLength(LC) + 1, e);
    }
}

int main()
{
    LinkList *listA, *listB, *listC, *listD, *listE, *listF;
    ElemType list_a[4] = {'a', 'b', 'c', 'd'};
    ElemType list_b[5] = {'b', 'c', 'e', 'f', 'g'};
    CreateListAtTail(listA, list_a, 5);
    cout << "线性表A的元素是:";
    DisplayList(listA);
    CreateListAtTail(listB, list_b, 6);
    cout << "线性表B的元素是:";
    DisplayList(listB);
    InitList(listC);
    InitList(listD);
    InitList(listE);
    InitList(listF);

    cout << "A ∪ B: ";
    UnionList(listA, listB, listC);
    DisplayList(listC); 

    cout << "A ∩ B: ";
    CommonList(listA, listB, listD);
    DisplayList(listD); 

    cout << "A - B: ";
    DifferenceSetList(listA, listB, listE);
    DisplayList(listE);

    cout << "B - A: ";
    DifferenceSetList(listB, listA, listF);
    DisplayList(listF);

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值