-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicecream.cpp
127 lines (104 loc) · 3.76 KB
/
icecream.cpp
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
127
#include <iostream>
#include <unordered_set>
using namespace std;
// Node structure for a linked list
struct Node {
int rollNumber; // Assuming roll number is used as a unique identifier for students
bool likesVanilla;
bool likesButterscotch;
Node* next;
};
// Function to insert a student into a linked list
void insert(Node*& head, int rollNumber, bool likesVanilla, bool likesButterscotch) {
Node* newNode = new Node();
newNode->rollNumber = rollNumber;
newNode->likesVanilla = likesVanilla;
newNode->likesButterscotch = likesButterscotch;
newNode->next = head;
head = newNode;
}
// Function to display the elements of a linked list
void display(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << "Roll Number: " << temp->rollNumber << ", Likes Vanilla: " << temp->likesVanilla
<< ", Likes Butterscotch: " << temp->likesButterscotch << endl;
temp = temp->next;
}
}
// Function to compute and display sets A, B, C, D, and E
void computeAndDisplaySets(Node* head) {
unordered_set<int> setLikesVanilla;
unordered_set<int> setLikesButterscotch;
unordered_set<int> setLikesBoth;
unordered_set<int> setLikesOnlyVanilla;
unordered_set<int> setLikesOnlyButterscotch;
unordered_set<int> setLikesNeither;
Node* temp = head;
while (temp != nullptr) {
if (temp->likesVanilla) {
setLikesVanilla.insert(temp->rollNumber);
}
if (temp->likesButterscotch) {
setLikesButterscotch.insert(temp->rollNumber);
}
if (temp->likesVanilla && temp->likesButterscotch) {
setLikesBoth.insert(temp->rollNumber);
}
if (temp->likesVanilla && !temp->likesButterscotch) {
setLikesOnlyVanilla.insert(temp->rollNumber);
}
if (!temp->likesVanilla && temp->likesButterscotch) {
setLikesOnlyButterscotch.insert(temp->rollNumber);
}
setLikesNeither.insert(temp->rollNumber);
temp = temp->next;
}
// Set A: Students who like either vanilla or butterscotch or both
cout << "Set A: ";
for (int rollNumber : setLikesVanilla) {
cout << rollNumber << " ";
}
for (int rollNumber : setLikesButterscotch) {
if (setLikesVanilla.find(rollNumber) == setLikesVanilla.end()) {
cout << rollNumber << " ";
}
}
cout << endl;
// Set B: Students who like both vanilla and butterscotch
cout << "Set B: ";
for (int rollNumber : setLikesBoth) {
cout << rollNumber << " ";
}
cout << endl;
// Set C: Students who like only vanilla not butterscotch
cout << "Set C: ";
for (int rollNumber : setLikesOnlyVanilla) {
cout << rollNumber << " ";
}
cout << endl;
// Set D: Students who like only butterscotch not vanilla
cout << "Set D: ";
for (int rollNumber : setLikesOnlyButterscotch) {
cout << rollNumber << " ";
}
cout << endl;
// Set E: Number of students who like neither vanilla nor butterscotch
cout << "Set E: " << setLikesNeither.size() << " students" << endl;
}
int main() {
// Example student data
Node* students = nullptr;
// Insert student data (roll number, likesVanilla, likesButterscotch)
insert(students, 1, true, false);
insert(students, 2, false, true);
insert(students, 3, true, true);
insert(students, 4, false, false);
// Display student data
cout << "Student Data:\n";
display(students);
// Compute and display sets A, B, C, D, and E
cout << "\nComputing and Displaying Sets A, B, C, D, and E:\n";
computeAndDisplaySets(students);
return 0;
}