sidrs

[TODO] FDS (KR) Ass 6 - Set Ops Using Linked Lists

Oct 16th, 2024 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Node {
  6. public:
  7.     int roll;
  8.     Node *next;
  9.  
  10.     Node () {
  11.         next = nullptr;
  12.     }
  13.  
  14.     Node (int roll) {
  15.         this->roll = roll;
  16.         next = nullptr;
  17.     }
  18. };
  19.  
  20. class Set {
  21.     Node *hU, *hV, *hB;
  22.     int *UniSet, *Vanilla, *Butterscotch;
  23.     int nU, nV, nB;
  24.     int *r1, *r2, *r3;
  25.     int n1, n2, n3;
  26.     int *arr;
  27. public:
  28.     Set () {
  29.         hU = hV = hB = nullptr;
  30.         UniSet = Vanilla = Butterscotch = nullptr;
  31.     }
  32.  
  33.     int *fillSet(int cardinality) {
  34.         arr = new int[cardinality];
  35.         for (int i = 0; i < cardinality; i++) {
  36.             cout << "Enter Roll No.: ";
  37.             cin >> arr[i];
  38.         }
  39.         return arr;
  40.     }
  41.  
  42.     void genU() {
  43.         cout << "\nUniversal Set" << endl;
  44.         cout << "Enter the total number of Students: "; cin >> nU;
  45.         //UniSet = new int[nU];
  46.         UniSet = fillSet(nU);
  47.         hU = createLL(UniSet, nU);
  48.     }
  49.  
  50.     void genVanilla() {
  51.         cout << "\nVanilla Set" << endl;
  52.         cout << "Enter the number of Students who like Vanilla: "; cin >> nV;
  53.         //Vanilla = new int[nV];
  54.         Vanilla = fillSet(nV);
  55.         hV = createLL(Vanilla, nV);
  56.     }
  57.  
  58.     void genButterscotch() {
  59.         cout << "\nButterscotch Set" << endl;
  60.         cout << "Enter the number of Students who like Butterscotch: "; cin >> nB;
  61.         //Butterscotch = new int[nB];
  62.         Butterscotch = fillSet(nB);
  63.         hB = createLL(Butterscotch, nB);
  64.     }
  65.    
  66.     Node *createLL(int *set, int size) {
  67.         if (size == 0) return nullptr;
  68.  
  69.         Node *head = new Node(set[0]);
  70.         Node *temp = head;
  71.  
  72.         for (int i = 1; i < size; i++) {
  73.             temp->next = new Node(set[i]);
  74.             temp = current->next;
  75.         }
  76.         return head;
  77.     }
  78.  
  79.     void display() {
  80.         int choice;
  81.         Node *head = nullptr;
  82.         cout << "1. Universal Set\n2. Vanilla\n3. Butterscotch\n";
  83.         cout << "Enter choice: ";
  84.         if (choice == 1) head = hU;
  85.         else if (choice == 2) head = hV;
  86.         else if (choice == 3) head = hB;
  87.         else cout << "Invalid Choice" << endl;
  88.  
  89.         Node *temp = head;
  90.         while (temp != nullptr) {
  91.             cout << temp->roll << " " << endl;
  92.             temp = temp->next;
  93.         }
  94.         cout << endl;
  95.  
  96.         // No longer needed cuz LL
  97.         // for (int i = 0; i < num; i++) cout << arr[i] << " ";
  98.     }
  99.        
  100.     // Students who like both vanilla and butterscottch
  101.     void op1() {
  102.        
  103.         Node *tempV = hV;
  104.         Node *intersectHead = nullptr;
  105.         Node *intersectTail = nullptr;
  106.  
  107.         while (tempV != nullptr) {
  108.             Node *tempB = hB;
  109.             (while tempB != nullptr) {
  110.                 if (tempV->roll == tempB->roll) {
  111.                     Node *newNode = new Node(tempV->roll);
  112.                     if(intersectHead == nullptr) intersectHead = intersectTail = newNode;
  113.                     else {
  114.                         intersectTail->next = newNode;
  115.                         intersectTail = newNode;
  116.                     }
  117.  
  118.                     break;
  119.                 }
  120.  
  121.                 tempB = tempB->next;
  122.             }
  123.  
  124.             tempA = tempA->next;
  125.         }
  126.     }
  127.    
  128.     // Stundets who like vanilla or butterscotch or not both
  129.     void op2() {
  130.    
  131.     }
  132.    
  133.     // Number of students who like neither  vanilla nor butterscotch
  134.     void op3() {
  135.    
  136.     }
  137.  
  138. };
  139.    
  140.  
Advertisement
Add Comment
Please, Sign In to add comment