193030

Kursova added delete and formated

Oct 19th, 2020
825
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. // Домашни и служебни адреси(индикатор за различаване)
  6. // телефони на съответните адреси
  7.  
  8. // -създаване на указателя, като информацията се чете
  9. // клавиатурата
  10. // -извежда информация за броя на работещите(лицето е
  11. // е работещо, ако има поне един служебен адрес
  12.  
  13.  
  14. string names[3];
  15. string addresses[3];
  16. string phoneNumbers[3];
  17.  
  18. struct Node
  19. {
  20.     string data;
  21.     Node *next;
  22.     Node *child;
  23. };
  24.  
  25.  
  26. /*
  27. * COULDN'T IMPLEMENT QUEUE OF DOUBLE POINTERS
  28. * Traversing by breadth
  29. * Node *firstChild && Node *secondChild are emulating queue; Helping nodes for traversing
  30. * for this multilevel linked list, where you have maximum two children on the queue
  31. *
  32. */
  33. Node *firstChild = NULL;
  34. Node *secondChild = NULL;
  35.  
  36. void enterPerson(); // Read input from the console
  37. // third name, address1, address2, address3, Phone number 1,2,3.
  38.  
  39. struct Node* addNode(); // Create a new node from the values entered in enterPerson()
  40. void traverseNodes(struct Node*); // Traverse by breadth and print the multilevel linked list
  41. void traverseNodesWithoutJobPlace(struct Node*); // Traverse by breadth and print only the people without job
  42. struct Node **deleteFromDynamicArray(int indexToDelete, struct Node**DynamicArrayInput); // Delete given index from the dynamic array
  43.  
  44. int currentNode = 1;
  45. int currentNodeUnmployedList = 1;
  46. struct Node* tempFirst;
  47. int printFlagUnempoyedList = 0; // flag for traversing the list for unemployed people
  48. int numberOfPeople = 0;
  49.  
  50. int main()
  51. {
  52.     struct Node **DynamicArray;
  53.     DynamicArray = new struct Node*[1]; // initialize the dynamic array with one element
  54.  
  55.     typedef struct Node* *testDynamicArray;
  56.     struct Node **temp;
  57.  
  58.     int loop = 1;
  59.     while(loop)
  60.     {
  61.         cout << "Menu: " << endl;
  62.         cout << "1. Press 1 to add person" << endl;
  63.         cout << "2. Press 2 to show the list" << endl;
  64.         cout << "3. Press 3 to show unemployed people" << endl;
  65.         cout << "4. Press 4 to delete worker by index from the list" << endl;
  66.         cout << "5. Press 0 to exit" << endl;
  67.         int inputCommand;
  68.         cin >> inputCommand;
  69.         switch(inputCommand)
  70.         {
  71.         case 1:
  72.             enterPerson();
  73.  
  74.             // There are no people entered yet
  75.             if(numberOfPeople == 0)
  76.             {
  77.                  // Saving pointer in the first address of the dynamic array
  78.                 DynamicArray = new struct Node*[numberOfPeople];
  79.                 DynamicArray[0] = addNode();
  80.             }
  81.             else
  82.             {
  83.                 // Temporary array [numberOfPeople+1]
  84.                 temp = new struct Node*[numberOfPeople+1];
  85.  
  86.                 // Copy data to temp array
  87.                 for(int i =0; i<numberOfPeople;i++)
  88.                 {
  89.                     temp[i] = DynamicArray[i];
  90.                 }
  91.  
  92.                 // Remove dynamic array
  93.                 delete[] DynamicArray;
  94.                 // temp = nullptr;
  95.  
  96.  
  97.                 DynamicArray = temp;
  98.                 // Add node to the dynamic array
  99.                 DynamicArray[numberOfPeople] = addNode();
  100.  
  101.             }
  102.             numberOfPeople++;
  103.             break;
  104.  
  105.         // Print all people in the array
  106.         case 2:
  107.             if(numberOfPeople>=1)
  108.             {
  109.                for(int i =0; i<numberOfPeople;i++)
  110.                 {
  111.                     traverseNodes(DynamicArray[i]);
  112.                 }
  113.             }
  114.             else
  115.                 cout << "There are no people in the list " << endl;
  116.  
  117.             break;
  118.  
  119.         // Print people without work addresses(address1 | address 2)
  120.         case 3:
  121.             for(int i =0; i<numberOfPeople;i++)
  122.             {
  123.                 traverseNodesWithoutJobPlace(DynamicArray[i]);
  124.             }
  125.             if(printFlagUnempoyedList == 0)
  126.                cout << "All people in the list have jobs" << endl;
  127.             break;
  128.  
  129.         // Delete chosen employee from the array
  130.         case 4:
  131.             // Reading the index that need to deleted from the dynamic array.
  132.             int index;
  133.             cout << "Enter the index of the employee you want to remove from the list " << endl;
  134.             cin >> index;
  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.             break;
  142.         case 0:
  143.             loop = 0;
  144.             break;
  145.         default:
  146.  
  147.             break;
  148.  
  149.         }
  150.     }
  151. }
  152.  
  153.  
  154. void enterPerson()
  155. {
  156.     cin.ignore();
  157.     cout << "Enter first name: " << endl;
  158.     cin >> names[0];
  159.     cout << "Enter second name: " << endl;
  160.     cin >> names[1];
  161.     cout << "Enter last name name: " << endl;
  162.     cin >> names[2];
  163.     cout << "Enter first address: " << endl;
  164.     cin >> addresses[0];
  165.     cout << "Enter second address: " << endl;
  166.     cin.ignore();
  167.     getline(cin,addresses[1]);
  168.     cout << "Enter third address: " << endl;
  169.     getline(cin,addresses[2]);
  170.     cout << "Enter first phone number: " << endl;
  171.     cin >> phoneNumbers[0];
  172.     cout << "Enter second phone number: " << endl;
  173.     cin >> phoneNumbers[1];
  174.     cout << "Enter third phone number: " << endl;
  175.     cin >> phoneNumbers[2];
  176.  
  177. }
  178.  
  179.  
  180. struct Node* addNode()
  181. {
  182.    struct Node* first;
  183.    first = new Node;
  184.    first->data = names[0];
  185.    Node* secN = new Node;
  186.    secN->data = names[1];
  187.    secN->child = NULL;
  188.    first->next =secN;
  189.  
  190.  
  191.    Node* lastN = new Node;
  192.    lastN ->data = names[2];
  193.    secN->next = lastN;
  194.    lastN->child = NULL;
  195.  
  196.    Node* adress1Node = new Node;
  197.    adress1Node->data = addresses[0];
  198.    adress1Node->child = NULL;
  199.  
  200.    Node* adress2Node = new Node;
  201.    adress2Node->data = addresses[1];
  202.    adress1Node->next = adress2Node;
  203.    adress2Node->child = NULL;
  204.  
  205.  
  206.    Node* adress3Node = new Node;
  207.    adress3Node->data = addresses[2];
  208.    adress2Node->next = adress3Node;
  209.    adress3Node->child = NULL;
  210.  
  211.    first->child = adress1Node;
  212.  
  213.    Node* firstNumberNode = new Node;
  214.    firstNumberNode->data = phoneNumbers[0];
  215.    firstNumberNode->next = NULL;
  216.    firstNumberNode->child = NULL;
  217.  
  218.    Node* secondNumberNode = new Node;
  219.    secondNumberNode->data = phoneNumbers[1];
  220.    secondNumberNode->next = NULL;
  221.    secondNumberNode->child = NULL;
  222.    firstNumberNode->next = secondNumberNode;
  223.  
  224.    Node* thirdNumberNode = new Node;
  225.    thirdNumberNode->data = phoneNumbers[2];
  226.    thirdNumberNode->next = NULL;
  227.    thirdNumberNode->child = NULL;
  228.  
  229.    adress1Node->child = firstNumberNode;
  230.    adress3Node->child = thirdNumberNode;
  231.     return first;
  232. }
  233.  
  234.  
  235. void traverseNodes(struct Node* p)
  236. {
  237.     if(p==secondChild)
  238.        secondChild=NULL;
  239.     if(p==firstChild)
  240.         firstChild = NULL;
  241.     while(p)
  242.     {
  243.  
  244.       if(p->child)
  245.       {
  246.          if(secondChild == NULL && firstChild!=NULL)
  247.          {
  248.             secondChild = p->child;
  249.          }
  250.  
  251.          if(firstChild == NULL)
  252.             firstChild = p->child;
  253.  
  254.       }
  255.         cout << p->data << " ";
  256.         p = p->next;
  257.     }
  258.  
  259.     // Traversing the next child
  260.     if(firstChild !=NULL || secondChild != NULL)
  261.     {
  262.  
  263.         if(firstChild)
  264.         {
  265.             traverseNodes(firstChild);
  266.  
  267.         }
  268.         if(secondChild)
  269.         {
  270.             traverseNodes(secondChild);
  271.  
  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.     // Traversing the next child
  326.     if(firstChild !=NULL || secondChild != NULL)
  327.     {
  328.  
  329.         if(firstChild)
  330.         {
  331.             traverseNodesWithoutJobPlace(firstChild);
  332.  
  333.         }
  334.         if(secondChild)
  335.         {
  336.             traverseNodesWithoutJobPlace(secondChild);
  337.  
  338.         }
  339.  
  340.     }
  341. }
  342.  
  343. struct Node **deleteFromDynamicArray(int indexToDelete, struct Node*DynamicArrayInput[])
  344. {
  345.     struct Node** tempForDelete;
  346.     tempForDelete = new struct Node*[numberOfPeople];
  347.     cout << "Dynamic array input data " << DynamicArrayInput[0]->data << endl;
  348.     for(int i =0; i<numberOfPeople;i++) // Sav
  349.     {
  350.         tempForDelete[i] = DynamicArrayInput[i];
  351.     }
  352.     int nextElementShifting = 0;
  353.     delete[] DynamicArrayInput;
  354.  
  355.     for(int i=0; i<numberOfPeople-1;i++) // Saving the data from the temp array(input) to the output array, without the deleted index
  356.     {
  357.         if(i+1==indexToDelete)
  358.         {
  359.             nextElementShifting++;
  360.         }
  361.         DynamicArrayInput[i] = tempForDelete[i+nextElementShifting];
  362.     }
  363.  
  364.     delete[] tempForDelete;
  365.     numberOfPeople--;
  366.     return DynamicArrayInput;
  367. }
  368.  
  369.  
  370.  
  371.  
RAW Paste Data