改进版(有头指针)的链表操作:集合的运算

#include <iostream>
using namespace std;

struct Lnode{
    int data;
    Lnode *next;
};

//存头结点的地址
struct LinkList{
    Lnode *head;
};

//创建链表的头结点,初始化
void Create(LinkList &L)
{
    L.head = new Lnode;
    L.head -> data = 0;
    L.head -> next = NULL;
}


void show(LinkList &L)
{
    Lnode *p = L.head -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
    return ;
}
//添加元素x,并且使得链表从小到大有序
void InsertList(LinkList &L, int x)
{
    //p第一个数据节点,pr头结点
    Lnode *p = L.head -> next, *pr = L.head;
    while(p && x >= p -> data)
    {
        if(x == p -> data)    return ;//重复元素不插入
        pr = p;
        p = p -> next;
    }//循环结束表明表尾或者x找到了第一个小于data的位置(需要插入的位置)
    //两种情况都需要插入,且代码相同
    Lnode *s = new Lnode;
    s -> data = x;
    s -> next = p;
    pr -> next = s;
    return ;
}
//删除元素x
void DeleteList(LinkList &L, int x)
{
    Lnode *p = L.head -> next;
    Lnode *pr = L.head;
    while(p)
    {
        if(p -> data == x)
        {
            pr -> next = p -> next;
            delete p;
            return ;
        }
        pr = p;
        p = p -> next;
    }
    return ;
}
//两个有序集合的并集
void union_1(LinkList &ha, LinkList &hb)
{
    LinkList ha_temp = ha, hb_temp = hb;
    Lnode *p1 = ha_temp.head, *p2 = hb_temp.head;
    while(p2)
    {
        InsertList(ha_temp, p2 -> data);
        p2 = p2 -> next;
    }
    return ;
}
//两个有序集合的交集
void union_2(LinkList &ha, LinkList &hb)
{
    Lnode *p1 = ha.head, *p2 = hb.head;
    while(p1 && p2)
    {
        if(p1 -> data == p2 -> data)
        {
            p1 = p1 -> next;
            p2 = p2 -> next;
        }
        else if(p1 -> data < p2 -> data)
        {
            Lnode *temp = p1;
            p1 = p1 -> next;
            DeleteList(ha, temp -> data);
        }
        else
        {
            p2 = p2 -> next;
        }
    }
    while(p1)
    {
        Lnode *temp = p1;
        p1 = p1 -> next;
        DeleteList(ha, temp -> data);
    }
}

int main()
{
    //注意集合是没有重复元素的
    LinkList ha;
    Create(ha);
    cout << "创建集合ha,请输入要插入的元素(以-1000作为结束信号):" << endl;
    int temp = 0;
    while(cin >> temp && temp != -1000)
        InsertList(ha, temp);
    cout << "有序集合ha如下" << endl;
    show(ha);
    cout << endl;

    LinkList hb;
    Create(hb);
    cout << "创建集合hb,请输入要插入的元素(以-1000作为结束信号):" << endl;
    temp = 0;
    while(cin >> temp && temp != -1000)
        InsertList(hb, temp);
    cout << "有序集合hb如下" << endl;
    show(hb);
    cout << endl;

    union_1(ha, hb);
    cout << "ha与hb两个集合的并集如下" << endl;
    show(ha);

    cout << endl << "==================================================================" << endl << endl;
    //========================================================================

    LinkList hc;
    Create(hc);
    cout << "创建集合hc,请输入要插入的元素(以-1000作为结束信号):" << endl;
    temp = 0;
    while(cin >> temp && temp != -1000)
        InsertList(hc, temp);
    cout << "有序集合hc如下" << endl;
    show(hc);
    cout << endl;

    LinkList hd;
    Create(hd);
    cout << "创建集合hd,请输入要插入的元素(以-1000作为结束信号):" << endl;
    temp = 0;
    while(cin >> temp && temp != -1000)
        InsertList(hd, temp);
    cout << "有序集合hd如下" << endl;
    show(hd);
    cout << endl;

    union_2(hc, hd);
    cout << "hc与hd两个集合的交集如下" << endl;
    show(hc);

    return 0;
}

运行结果:

猜你喜欢

转载自www.cnblogs.com/Chaosliang/p/11600968.html
今日推荐