sidrs

DSAL_MN Ass 2

Jan 16th, 2025
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. class Node {
  7. public:
  8.     string label;
  9.     int count;
  10.     Node *child[10];
  11.    
  12. };
  13.  
  14. Node *root;
  15.  
  16. class Tree {
  17. private:
  18.     int chapterCount, sectionCount;
  19. public:
  20.     Tree() {
  21.         root = nullptr;
  22.     }
  23.  
  24.     void createTree();
  25.     void displayTree();
  26. };
  27.  
  28. void Tree::createTree() {
  29.     root = new Node;
  30.  
  31.     string bookName;
  32.     cout << "\nEnter name of the Book: ";
  33.     cin.ignore();
  34.     getline(cin, bookName);
  35.     root->label = bookName;
  36.  
  37.     cout << "Enter the number of chapters: "; cin >> chapterCount;
  38.     root->count = chapterCount;
  39.  
  40.     for (int i = 0; i < root->count; i++) {
  41.  
  42.         root->child[i] = new Node;
  43.        
  44.         string chapterName;
  45.         cout << "\nEnter name of the Chapter " << i + 1 << ": ";
  46.         if (i == 0) cin.ignore();
  47.         getline(cin, chapterName);
  48.         root->child[i]->label = chapterName;
  49.        
  50.         cout << "Enter the number of sections: "; cin >> sectionCount;
  51.         root->child[i]->count = sectionCount;
  52.  
  53.         for (int j = 0; j < root->child[i]->count; j++) {
  54.            
  55.             root->child[i]->child[j] = new Node;   
  56.  
  57.             string sectionName;
  58.             cout << "Enter name of the Section " << j + 1 << ": ";
  59.             if (j == 0) cin.ignore();
  60.             getline(cin, sectionName);
  61.             root->child[i]->child[j]->label = sectionName;
  62.  
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68. void Tree::displayTree() {
  69.     Node *temp = root;
  70.     cout << endl;
  71.     cout << "Displaying details for the book: " << root->label << endl;
  72.     for (int i = 0; i < root->count; i++) {
  73.         cout << "Chapter " << i + 1 << ": " << root->child[i]->label << endl;
  74.         for (int j = 0; j < root->child[i]->count; j++) {
  75.             cout << setw(4) << "" << "Section " << j + 1 << ": " << root->child[i]->child[j]->label << endl;
  76.         }
  77.     }
  78.     cout << endl;
  79. }
  80.  
  81. int main() {
  82.    
  83.     Tree t;
  84.    
  85.     while (true) {
  86.  
  87.         char ch;
  88.         cout << "----- MENU -----" << endl;
  89.         cout << "1. Create" << endl;
  90.         cout << "2. Display" << endl;
  91.         cout << "3. Exit" << endl;
  92.         cout << "Enter your choice: "; cin >> ch;
  93.        
  94.         if (ch == '1') {
  95.             t.createTree();
  96.             t.displayTree();   
  97.         } else if (ch == '2') {
  98.             t.displayTree();
  99.         } else if (ch == '3') {
  100.             cout << "\nTerminating program" << endl;
  101.             break;
  102.         } else {
  103.             cout << "Invalid Input" << endl;
  104.         }
  105.     }
  106.  
  107.     return 0;
  108. };
  109.  
  110.  
  111. /*
  112. OUPTUT
  113.  
  114. ----- MENU -----
  115. 1. Create
  116. 2. Display
  117. 3. Exit
  118. Enter your choice: 1
  119.  
  120. Enter name of the Book: Fundamentals of Data Stuctures
  121. Enter the number of chapters: 2
  122.  
  123. Enter name of the Chapter 1: Hashing
  124. Enter the number of sections: 3
  125. Enter name of the Section 1: Linear Probing
  126. Enter name of the Section 2: Quadratic Probing
  127. Enter name of the Section 3: Chaining
  128.  
  129. Enter name of the Chapter 2: Queues
  130. Enter the number of sections: 4
  131. Enter name of the Section 1: Queue
  132. Enter name of the Section 2: Circular Queue
  133. Enter name of the Section 3: Double Ended Queue
  134. Enter name of the Section 4: Priority Queue
  135.  
  136. Displaying details for the book: Fundamentals of Data Stuctures
  137. Chapter 1: Hashing
  138.     Section 1: Linear Probing
  139.     Section 2: Quadratic Probing
  140.     Section 3: Chaining
  141. Chapter 2: Queues
  142.     Section 1: Queue
  143.     Section 2: Circular Queue
  144.     Section 3: Double Ended Queue
  145.     Section 4: Priority Queue
  146.  
  147. ----- MENU -----
  148. 1. Create
  149. 2. Display
  150. 3. Exit
  151. Enter your choice: 3
  152.  
  153. Terminating program
  154.  
  155.  
  156. */
Advertisement
Add Comment
Please, Sign In to add comment