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

//
// 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;
} 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

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

// Returns true if there are currently *no* set objects in existence.
return true;
}

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

// Set destructor
Set *deleteSet(Set *theSet) {
while (p != NULL) {
removeItem(theSet, p->data);
}
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;
}
// 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;
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;
}
// Determine if the value is in the set
if (p != NULL && p->data == givenItem) {
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) {
while (pA != NULL && pB != NULL) {
if (pA->data != pB->data)
return false;
}
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();
while (pA != NULL && pB != NULL) {
if (pA->data <= pB->data) {
insertItem(setC, pA->data);
} else {
insertItem(setC, pB->data);
}
}
p = (pA != NULL) ? pA : pB;
while (p != NULL) {
insertItem(setC, p->data);
}
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();
while (pA != NULL && pB != NULL){
if (pA->data == pB->data) {
insertItem(setC,pA->data);
} else if (pA->data < pB->data) {
} else{
}
}

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();
while (pA != NULL) {
while (pB != NULL && pB->data != pA->data)
if (pB == NULL) {
insertItem(setC, pA->data);
}
}
return setC;
}

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


#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;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120