1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
using namespace std;
template<class T>
class OLL
{
private:struct NODE
{
T info;
NODE *next;
};
NODE *list;
public: OLL()
{
list = NULL;
}
void insert (T x)
{
NODE *p = list, *q = list, *r;
//create new node
r = new (NODE); r -> info = x;
r -> next = NULL;
//find the insertion place
while (p != NULL && p -> info < x)
{
q = p; p = p -> next;
}
if (p == list) // x is the first info
{
list = r; r -> next = p;
}
else if (p == NULL) // x is the last info
{
q -> next = r;
}
else //x is neither first nor last info
{
r -> next = p; q -> next = r;
}
}
void display ()
{
NODE *p = list;
while (p != NULL)
{
cout<<p->info<<" --> ";
p = p -> next;
}
cout << "NULL\n";
}
void display (OLL <T> Tset)
{
while (Tset.list != NULL)
{
cout<<Tset.list -> info<<" --> ";
Tset.list = Tset.list -> next;
}
cout<<"NULL\n";
}
void FindAUB(OLL setA, OLL setB, OLL &U) //modify this function to make FindAIB "A Intersect B"
{
NODE *a = setA.list;
NODE *b = setB.list;
while (a != NULL && b != NULL)
{
if (a->info < b->info)
{
U.insert(a->info);
a = a->next;
}
else if (b->info < a->info)
{
U.insert(b->info);
b = b->next;
}
else
{
U.insert(a->info);
a = a->next;
b = b->next;
}
}
while (a != NULL)
{
U.insert(a->info);
a = a->next;
}
while (b != NULL)
{
U.insert(b->info);
b = b->next;
}
}
};
int main()
{
//create set of integers
OLL <int> setA, setB, setAUB;
//insert set A
int A[4] = {3, 9, 6, 8};
int B[7] = {17, 19, 2, 6, 4, 1, 3};
//set A
for (int i=0; i<4; ++i)
{
setA.insert(A[i]);
}
setA.display();
//set B
for (int i=0; i<7; ++i)
{
setB.insert(B[i]);
}
setB.display();
//set AUB
setA.FindAUB(setA, setB, setAUB);
setAUB.display();
//Terminate Program
system("pause");
return 0;
}
|