193030

Kursova SAA vsichko raboti 20.10

Oct 20th, 2020 (edited)
794
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string names[3];
  6. string addresses[3];
  7. string phoneNumbers[3];
  8.  
  9. struct Node
  10. {
  11.     string data;
  12.     Node *next;
  13.     Node *child;
  14. };
  15.  
  16.  
  17. /*
  18. * COULDN'T IMPLEMENT QUEUE OF DOUBLE POINTERS
  19. * Traversing by breadth
  20. * Node *firstChild && Node *secondChild are emulating queue; Helping nodes for traversing
  21. * for this multilevel linked list, where you have maximum two children in the queue
  22. *
  23. */
  24. Node *firstChild = NULL;
  25. Node *secondChild = NULL;
  26.  
  27. void enterPerson(); // Read input from the console
  28. // third name, address1, address2, address3, Phone number 1,2,3.
  29.  
  30. struct Node* addNode(); // Create a new node from the values entered in enterPerson()
  31. void traverseNodes(struct Node*); // Traverse by breadth and print the multilevel linked list
  32. void traverseNodesWithoutJobPlace(struct Node*); // Traverse by breadth and print only the people without job
  33. struct Node **deleteFromDynamicArray(int indexToDelete, struct Node**DynamicArrayInput); // Delete given index from the dynamic array
  34.  
  35. int currentNode = 1;
  36. int currentNodeUnmployedList = 1;
  37. struct Node* tempFirst;
  38. int printFlagUnempoyedList = 0; // flag for traversing the list for unemployed people
  39. int numberOfPeople = 0;
  40.  
  41. int main()
  42. {
  43.     struct Node **DynamicArray;
  44.     DynamicArray = new struct Node*[1]; // initialize the dynamic array with one element
  45.  
  46.     typedef struct Node* *testDynamicArray;
  47.     struct Node **temp;
  48.  
  49.     int loop = 1;
  50.     while(loop)
  51.     {
  52.         cout << "Menu: " << endl;
  53.         cout << "1. Press 1 to add person" << endl;
  54.         cout << "2. Press 2 to show the list" << endl;
  55.         cout << "3. Press 3 to show unemployed people" << endl;
  56.         cout << "4. Press 4 to delete worker by index from the list" << endl;
  57.         cout << "5. Press 0 to exit" << endl;
  58.         int inputCommand;
  59.         cin >> inputCommand;
  60.         switch(inputCommand)
  61.         {
  62.         case 1:
  63.             enterPerson();
  64.  
  65.             // There are no people entered yet
  66.             if(numberOfPeople == 0)
  67.             {
  68.                  // Saving pointer in the first address of the dynamic array
  69.                 DynamicArray = new struct Node*[numberOfPeople];
  70.                 DynamicArray[0] = addNode();
  71.             }
  72.             else
  73.             {
  74.                 // Temporary array [numberOfPeople+1]
  75.                 temp = new struct Node*[numberOfPeople+1];
  76.  
  77.                 // Copy data to temp array
  78.                 for(int i =0; i<numberOfPeople;i++)
  79.                 {
  80.                     temp[i] = DynamicArray[i];
  81.                 }
  82.  
  83.                 // Remove dynamic array
  84.                 delete[] DynamicArray;
  85.                 // temp = nullptr;
  86.  
  87.  
  88.                 DynamicArray = temp;
  89.                 // Add node to the dynamic array
  90.                 DynamicArray[numberOfPeople] = addNode();
  91.  
  92.             }
  93.             numberOfPeople++;
  94.             break;
  95.  
  96.         // Print all people in the array
  97.         case 2:
  98.             if(numberOfPeople>=1)
  99.             {
  100.                 cout <<"Information for all of the people in the list" << endl;
  101.                for(int i =0; i<numberOfPeople;i++)
  102.                 {
  103.                     traverseNodes(DynamicArray[i]);
  104.                 }
  105.             }
  106.             else
  107.                 cout << "There are no people in the list " << endl;
  108.  
  109.             break;
  110.  
  111.         // Print people without work addresses(address1 | address 2)
  112.         case 3:
  113.             cout << "Information for all of the unemployed people" << endl;
  114.             for(int i =0; i<numberOfPeople;i++)
  115.             {
  116.                 traverseNodesWithoutJobPlace(DynamicArray[i]);
  117.             }
  118.             if(printFlagUnempoyedList == 0)
  119.                cout << "All people in the list have jobs" << endl;
  120.             break;
  121.  
  122.         // Delete chosen employee from the array
  123.         case 4:
  124.             // Reading the index that need to deleted from the dynamic array.
  125.             if(numberOfPeople>=1)
  126.             {
  127.                 int index;
  128.                 cout << "Enter the index of the employee you want to remove from the list " << endl;
  129.                 cin >> index;
  130.                 while(index > numberOfPeople || index ==0) // input check for non existing indexes and 0 as input
  131.                 {
  132.                     cout << "Please enter a valid index. The list includes only " << numberOfPeople <<" people. 0 is not an option" << endl;
  133.                     cin>>index;
  134.                 }
  135.  
  136.                 struct Node **tempArr;
  137.                 tempArr = DynamicArray;
  138.                 delete DynamicArray;
  139.                 tempArr = deleteFromDynamicArray(index, tempArr); // Returns the array with deleted element pointed by index
  140.                 DynamicArray = tempArr;
  141.             }
  142.             else
  143.                 cout << "There are no people in the list and deleting is not possible" << endl;
  144.  
  145.             break;
  146.         case 0:
  147.             loop = 0;
  148.             break;
  149.         default:
  150.  
  151.             break;
  152.  
  153.         }
  154.     }
  155. }
  156.  
  157.  
  158. void enterPerson()
  159. {
  160.     cin.ignore();
  161.     cout << "Enter first name: " << endl;
  162.     cin >> names[0];
  163.     cout << "Enter second name: " << endl;
  164.     cin >> names[1];
  165.     cout << "Enter last name name: " << endl;
  166.     cin >> names[2];
  167.     cout << "Enter first address: " << endl;
  168.     cin >> addresses[0];
  169.     cout << "Enter second address: " << endl;
  170.     cin.ignore();
  171.     getline(cin,addresses[1]);
  172.     cout << "Enter third address: " << endl;
  173.     getline(cin,addresses[2]);
  174.     cout << "Enter first phone number: " << endl;
  175.     cin >> phoneNumbers[0];
  176.     cout << "Enter second phone number: " << endl;
  177.     cin >> phoneNumbers[1];
  178.     cout << "Enter third phone number: " << endl;
  179.     cin >> phoneNumbers[2];
  180.  
  181. }
  182.  
  183.  
  184. struct Node* addNode()
  185. {
  186.    struct Node* first;
  187.    first = new Node;
  188.    first->data = names[0];
  189.    Node* secN = new Node;
  190.    secN->data = names[1];
  191.    secN->child = NULL;
  192.    first->next =secN;
  193.  
  194.  
  195.    Node* lastN = new Node;
  196.    lastN ->data = names[2];
  197.    secN->next = lastN;
  198.    lastN->child = NULL;
  199.  
  200.    Node* adress1Node = new Node;
  201.    adress1Node->data = addresses[0];
  202.    adress1Node->child = NULL;
  203.  
  204.    Node* adress2Node = new Node;
  205.    adress2Node->data = addresses[1];
  206.    adress1Node->next = adress2Node;
  207.    adress2Node->child = NULL;
  208.  
  209.  
  210.    Node* adress3Node = new Node;
  211.    adress3Node->data = addresses[2];
  212.    adress2Node->next = adress3Node;
  213.    adress3Node->child = NULL;
  214.  
  215.    first->child = adress1Node;
  216.  
  217.    Node* firstNumberNode = new Node;
  218.    firstNumberNode->data = phoneNumbers[0];
  219.    firstNumberNode->next = NULL;
  220.    firstNumberNode->child = NULL;
  221.  
  222.    Node* secondNumberNode = new Node;
  223.    secondNumberNode->data = phoneNumbers[1];
  224.    secondNumberNode->next = NULL;
  225.    secondNumberNode->child = NULL;
  226.    firstNumberNode->next = secondNumberNode;
  227.  
  228.    Node* thirdNumberNode = new Node;
  229.    thirdNumberNode->data = phoneNumbers[2];
  230.    thirdNumberNode->next = NULL;
  231.    thirdNumberNode->child = NULL;
  232.  
  233.    adress1Node->child = firstNumberNode;
  234.    adress3Node->child = thirdNumberNode;
  235.     return first;
  236. }
  237.  
  238.  
  239. void traverseNodes(struct Node* p)
  240. {
  241.     if(p==secondChild)
  242.        secondChild=NULL;
  243.     if(p==firstChild)
  244.         firstChild = NULL;
  245.     while(p)
  246.     {
  247.       if(p->child)
  248.       {
  249.          if(secondChild == NULL && firstChild!=NULL)
  250.          {
  251.             secondChild = p->child;
  252.          }
  253.  
  254.          if(firstChild == NULL)
  255.             firstChild = p->child;
  256.       }
  257.         cout << p->data << " ";
  258.         p = p->next;
  259.     }
  260.  
  261.     // Traversing the next child
  262.     if(firstChild !=NULL || secondChild != NULL)
  263.     {
  264.         if(firstChild)
  265.         {
  266.             traverseNodes(firstChild);
  267.  
  268.         }
  269.         if(secondChild)
  270.         {
  271.             traverseNodes(secondChild);
  272.  
  273.         }
  274.     }
  275. }
  276.  
  277. void traverseNodesWithoutJobPlace(struct Node* p)
  278. {
  279.     if(currentNodeUnmployedList>9 && p) // 9 is the number of nodes in the multilevel linked list, 3x addresses, 3x names, 3x phone numbers
  280.     {
  281.         currentNodeUnmployedList = 1;
  282.         printFlagUnempoyedList = 0;
  283.     }
  284.     // Check for start of a new first node.
  285.     if(currentNodeUnmployedList==1 && p)
  286.     {
  287.         tempFirst = p;
  288.  
  289.     }
  290.     // Check the new first node. If address2 && address3 are empty the person is unemployed.
  291.    if(tempFirst!=NULL && currentNodeUnmployedList == 1
  292.        && tempFirst->child->next->data.empty()
  293.       && tempFirst->child->next->next->data.empty())
  294.    {
  295.      printFlagUnempoyedList = 1;
  296.    }
  297.  
  298.  
  299.     if(p==secondChild)
  300.        secondChild=NULL;
  301.     if(p==firstChild)
  302.         firstChild = NULL;
  303.     while(p)
  304.     {
  305.  
  306.       if(p->child)
  307.       {
  308.          if(secondChild == NULL && firstChild!=NULL)
  309.          {
  310.             secondChild = p->child;
  311.          }
  312.  
  313.          if(firstChild == NULL)
  314.             firstChild = p->child;
  315.  
  316.       }
  317.         if(printFlagUnempoyedList) //  address2 && address3 are empty so the person is unemployed and data will be printed
  318.         {
  319.             cout << p->data << " ";
  320.  
  321.         }
  322.         p = p->next;
  323.         currentNodeUnmployedList++;
  324.     }
  325.    
  326.     // Traversing the next child
  327.     if(firstChild !=NULL || secondChild != NULL)
  328.     {
  329.         if(firstChild)
  330.         {
  331.             traverseNodesWithoutJobPlace(firstChild);
  332.  
  333.         }
  334.         if(secondChild)
  335.         {
  336.             traverseNodesWithoutJobPlace(secondChild);
  337.         }
  338.  
  339.     }
  340. }
  341.  
  342. struct Node **deleteFromDynamicArray(int indexToDelete, struct Node*DynamicArrayInput[])
  343. {
  344.     struct Node** tempForDelete;
  345.     tempForDelete = new struct Node*[numberOfPeople];
  346.     cout << "Dynamic array input data " << DynamicArrayInput[0]->data << endl;
  347.     for(int i =0; i<numberOfPeople;i++) // Sav
  348.     {
  349.         tempForDelete[i] = DynamicArrayInput[i];
  350.     }
  351.     int nextElementShifting = 0;
  352.     delete[] DynamicArrayInput;
  353.  
  354.     for(int i=0; i<numberOfPeople-1;i++) // Saving the data from the temp array(input) to the output array, without the deleted index
  355.     {
  356.         if(i+1==indexToDelete)
  357.         {
  358.             nextElementShifting++;
  359.         }
  360.         DynamicArrayInput[i] = tempForDelete[i+nextElementShifting];
  361.     }
  362.  
  363.     delete[] tempForDelete;
  364.     numberOfPeople--;
  365.     return DynamicArrayInput;
  366. }
  367.  
  368.  
  369.  
  370.  
RAW Paste Data