Amazingness Oct9
Amazingness Oct9
union
{ }
char operation; }
}; }
};
int main(){
TreeNode *right;
}; TreeNode *node2 = new TreeNode();
node2->info.whichType = OPERAND;
private:
TreeNode *root; TreeNode *node3 = new TreeNode();
node3->info.whichType = OPERAND;
public: node3->info.operand = 3;
return ptr->info.operand;
case OPERATOR: TreeNode *nodeDiv = new TreeNode();
{ nodeDiv->info.operation = '/';
head = node;
myExpression expr; }
int result = expr.evaluate(nodeDiv);
// Remove a string from the set
cout << "9 / (6 + 3) is = " << result << void remove(string x) {
endl;
Set *p = head, *q = nullptr;
while (p != nullptr && p->data != x) {
delete node1;
q = p;
delete node2;
p = p->next;
delete node3;
}
delete nodeDiv;
if (p == nullptr) return; // Element not
delete nodeAdd; found
return 0;
} if (q == nullptr) {
head = head->next; // Remove the
head
#include <iostream> //sets
} else {
#include <string>
q->next = p->next; // Bypass the
using namespace std; node to remove it
struct Set { }
string data; delete p;
Set *next; }
}; bool ismember(string x) {
Set *p = head;
class setADT { while (p != nullptr) {
private: if (p->data == x) return true;
Set *head; p = p->next;
}
public: return false;
// Initialize an empty set }
setADT() : head(nullptr) {} setADT unionSet(setADT& B) {
setADT result = *this; // Start with
// Insert a string into the set (avoids the current set
duplicates) Set *p = B.head;
void insert(string x) { while (p != nullptr) {
if (ismember(x)) return; // Avoid result.insert(p->data); // Insert
inserting duplicates elements from B
Set* node = new Set; p = p->next;
node->data = x; }
node->next = head; return result;
3
}
int main() {
// Intersection of this set with another setADT set1, set2;
set (Set 1 ∩ Set 2)
set1.insert("apple");
setADT intersection(setADT& B) {
set1.insert("banana");
setADT result;
set1.insert("cherry");
Set *p = head;
while (p != nullptr) {
set2.insert("banana");
if (B.ismember(p->data)) {
set2.insert("cherry");
result.insert(p->data); // Insert
common elements set2.insert("date");
}
p = p->next; cout << "Set 1: ";
} set1.printSet();
return result;
} cout << "Set 2: ";
set2.printSet();