使用c语言单链表实现的一个集合,包含了交,差,并集运算

下面是set的头文件源码,set.h

//
// Created by admin on 18/3/12.
//

#ifndef _SET_H
#define _SET_H

#include <stdio.h>

typedef enum BOOL { false, true } Boolean;

// Define the element of the set
typedef struct Item {
    int data;
    struct Item *link;
} SetItem;

// Define the struct set
typedef struct SET{
    SetItem *first;
    int size;
}Set;

// Returns true if there are currently *no* set objects in existence.
Boolean validateMemUse();

// Set constructor
Set * newSet();
//Set destructor
Set * deleteSet(Set * theSet);

// Insert an item into a set, noting that sets do not contain duplicate items.
// Returns true if the item was added to the set.
Boolean insertItem(Set * const theSet, const int newItem);
// Remove a given item, if it appears in the set.
// Returns true if the item was removed from the set.
Boolean removeItem(Set * const theSet, const int givenItem);

// Returns true if the two sets contain the same items.
Boolean equals(Set const * const setA, Set const * const setB);

// Return a new set that is the union of two sets (the two sets should be
// unchanged by the operation/Volumes/Disk4/NttDeve/C:C++Prac/2073-1226/set.h).
//
// The union of two sets A and B is defined to be another set
// containing all the items in A and all the items in B.
// (Note: Any item that is in BOTH A and B appears only once
// in the union, because no set contains duplicate items.)
// Example: If A = { 1, 2, 3 } and B = { 2, 4, 5, 6 }, then
// the union of A and B is the set { 1, 2, 3, 4, 5, 6 }.
Set * unionOf(Set const * const setA, Set const * const setB);

// Return a new set that is the intersection of two sets (the two sets should be
// unchanged by the operation).
//
// The intersection of two sets A and B is defined to be another set
// containing all the items in both A and B.
// Example: If A = { 1, 2, 3, 4 } and B = { 2, 3, 5 }, then
// the intersection of A and B is the set { 2, 3 }.
Set * intersectionOf(Set const * const setA, Set const * const setB);

// Return a new set that is the difference of two sets (the two sets should be
// unchanged by the operation).
//
// The difference (A\B) of two sets A and B is defined to be
// another set containing all the items in A that are NOT in B.
// Example: If A = { 1, 2, 3 } and B = { 2, 4, 5, 6 }, then
// the difference A\B of A and B is the set { 1, 3 }.
Set * differenceOf(Set const * const setA, Set const * const setB);

// print set function
void print(Set * set);

#endif //INC_2073_1226_SET_H

下面是set.c

//
// Created by admin on 18/3/12.
//
#include "set.h"
#include <stdlib.h>

// Returns true if there are currently *no* set objects in existence.
Boolean validateMenUse() {
    return true;
}

// Set constructor
Set *newSet() {
    Set *set = (Set *) malloc(sizeof(Set *));
    set->first = (SetItem *) malloc(sizeof(SetItem *));
    set->first->data = 0;
    set->first->link = NULL;
    return set;
}

// Set destructor
Set *deleteSet(Set *theSet) {
    SetItem *p = theSet->first->link;
    while (p != NULL) {
        removeItem(theSet, p->data);
        p = theSet->first->link;
    }
    return theSet;
}

// Insert an item into a set, noting that sets do not contain duplicate items.
// Returns true if the item was added to the set.
Boolean insertItem(Set *const theSet, const int newItem) {
    // p is the scan pointer, pre is the predecessor of p
    SetItem *p = theSet->first->link, *pre = theSet->first;
    // Cyclic scan list
    while (p != NULL && p->data < newItem) {
        pre = p;
        p = p->link;
    }
    // If the collection contains this number, it returns false
    if (p != NULL && p->data == newItem)
        return false;
    // Insert the item into the set
    SetItem *q = (SetItem *) malloc(sizeof(SetItem));
    q->data = newItem;
    q->link = p;
    pre->link = q;
    theSet->size++;
    return true;

}

// Remove a given item, if it appears in the set.
// Returns true if the item was removed from the set.
Boolean removeItem(Set *const theSet, const int givenItem) {
    // p is the scan pointer, pre is the predecessor of p
    SetItem *p = theSet->first->link, *pre = theSet->first;
    // Cyclic scan list
    while (p != NULL && p->data < givenItem) {
        pre = p;
        p = p->link;
    }
    // Determine if the value is in the set
    if (p != NULL && p->data == givenItem) {
        pre->link = p->link;
        free(p);
        theSet->size--;
        return true;
    }
    return false;

}

// Returns true if the two sets contain the same items.
Boolean equals(Set const *const setA, Set const *const setB) {
    SetItem *pA = setA->first->link, *pB = setB->first->link;
    while (pA != NULL && pB != NULL) {
        if (pA->data != pB->data)
            return false;
        pA = pA->link;
        pB = pB->link;
    }
    return true;
}

// Return a new set that is the union of two sets (the two sets should be
// unchanged by the operation).
//
// The union of two sets A and B is defined to be another set
// containing all the items in A and all the items in B.
// (Note: Any item that is in BOTH A and B appears only once
// in the union, because no set contains duplicate items.)
// Example: If A = { 1, 2, 3 } and B = { 2, 4, 5, 6 }, then
// the union of A and B is the set { 1, 2, 3, 4, 5, 6 }.
Set *unionOf(Set const *const setA, Set const *const setB) {
    Set *setC = newSet();
    SetItem *pA = setA->first->link, *pB = setB->first->link, *p;
    while (pA != NULL && pB != NULL) {
        if (pA->data <= pB->data) {
            insertItem(setC, pA->data);
            pA = pA->link;
        } else {
            insertItem(setC, pB->data);
            pB = pB->link;
        }
    }
    p = (pA != NULL) ? pA : pB;
    while (p != NULL) {
        insertItem(setC, p->data);
        p = p->link;
    }
    return setC;
}

// Return a new set that is the intersection of two sets (the two sets should be
// unchanged by the operation).
//
// The intersection of two sets A and B is defined to be another set
// containing all the items in both A and B.
// Example: If A = { 1, 2, 3, 4 } and B = { 2, 3, 5 }, then
// the intersection of A and B is the set { 2, 3 }.
Set *intersectionOf(Set const *const setA, Set const *const setB) {
    Set *setC = newSet();
    SetItem *pA = setA->first->link, *pB = setB->first->link;
    while (pA != NULL && pB != NULL){
        if (pA->data == pB->data) {
            insertItem(setC,pA->data);
            pA = pA->link;
            pB = pB->link;
        } else if (pA->data < pB->data) {
            pA = pA->link;
        } else{
            pB = pB->link;
        }
    }

    return setC;
}

// Return a new set that is the difference of two sets (the two sets should be
// unchanged by the operation).
//
// The difference (A\B) of two sets A and B is defined to be
// another set containing all the items in A that are NOT in B.
// Example: If A = { 1, 2, 3 } and B = { 2, 4, 5, 6 }, then
// the difference A\B of A and B is the set { 1, 3 }.
Set *differenceOf(Set const *const setA, Set const *const setB) {
    Set *setC = newSet();
    SetItem *pA = setA->first->link, *pB = setB->first->link;
    while (pA != NULL) {
        while (pB != NULL && pB->data != pA->data)
            pB = pB->link;
        if (pB == NULL) {
            insertItem(setC, pA->data);
        }
        pA = pA->link;
        pB = setB->first->link;
    }
    return setC;
}

// print set function
void print(Set *set) {
    if (set->size == 0) {
        printf("the set is empty!\n");
        return;
    }
    SetItem *p = set->first->link;
    while (p != NULL) {
        printf("%d" , p->data);
        printf("\n");
        p = p->link;
    }

下面是测试主函数main.c

#include <stdio.h>
#include <assert.h>
#include "set.h"

int main() {
    Boolean rtn;
    Set *set1 = newSet();
    rtn = insertItem(set1, 1);
    if (rtn == true)
        printf("insert 1 to set1 ok\n");
    rtn = insertItem(set1, 2);
    if (rtn == true)
        printf("insert 2 to set1 ok\n");
    rtn = insertItem(set1, 3);
    if (rtn == true)
        printf("insert 3 to set1 ok\n");
    printf("print set1----------->:\n");
    print(set1);

    Set *set2 = newSet();
    rtn = insertItem(set2, 2);
    if (rtn == true)
        printf("insert 2 to set2 ok\n");
    rtn = insertItem(set2, 4);
    if (rtn == true)
        printf("insert 4 to set2 ok\n");
    rtn = insertItem(set2, 5);
    if (rtn == true)
        printf("insert 5 to set2 ok\n");
    rtn = insertItem(set2, 6);
    if (rtn == true)
        printf("insert 6 to set2 ok\n");
    printf("print set2----------->:\n");
    print(set2);

    rtn = removeItem(set1, 1);
    if (rtn == true)
        printf("remove 1 from set1 ok\n");
    printf("print set1----------->:\n");
    print(set1);

    rtn = removeItem(set2, 5);
    if (rtn == true)
        printf("remove 5 from set2 ok\n");
    printf("print set2----------->:\n");
    print(set2);

    Set *set3 = intersectionOf(set1, set2);
    printf("the result of intersectionOf(set1, set2) is----------->:\n");
    print(set3);

    Set *set4 = unionOf(set1, set2);
    printf("the result of unionOf(set1, set2) is----------->:\n");
    print(set4);

    Set *set5 = differenceOf(set1, set2);
    printf("the result of differenceOf(set1, set2) is----------->:\n");
    print(set5);

    rtn = equals(set1, set2);
    if (rtn == false){
        printf("the result of equals(set1, set2) is----------->:\n");
        printf("set1 not equal to set2\n");
    }

    deleteSet(set1);
    printf("the result of deleteSet(set1) is----------->:\n");
    print(set1);

    deleteSet(set2);
    deleteSet(set3);
    deleteSet(set4);
    deleteSet(set5);
    return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_24736703/article/details/79537915
文章标签: 集合 c 单链表
个人分类: 数据结构
上一篇数据结构课后题目源码
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭