Advertisement
193030

Kursova added delete index

Oct 18th, 2020 (edited)
1,986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.27 KB | None | 0 0
  1. // 4. delete raboti malko
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. // Трите имена
  6. // Домашни и служебни адреси(индикатор за различаване)
  7. // телефони на съответните адреси
  8.  
  9. // -създаване на указателя, като информацията се чете
  10. // клавиатурата
  11. // -извежда информация за броя на работещите(лицето е
  12. // е работещо, ако има поне един служебен адрес
  13.  
  14. string firstName;
  15. string secondName;
  16. string lastName;
  17. string adress1;
  18. string adress2;
  19. string adress3;
  20. string phoneNumber1;
  21. string phoneNumber2;
  22. string phoneNumber3;
  23.  
  24.  
  25.  
  26. struct Node
  27. {
  28.     string data;
  29.     Node *next;
  30.     Node *child;
  31. };
  32.  
  33.  
  34.  
  35.  
  36. Node *firstChild = NULL;
  37. Node *secondChild = NULL;
  38.  
  39. void enterPerson(); // Read input from the console
  40. // third name, address1, address2, address3, Phone number 1,2,3.
  41. struct Node* addNode(); // Create a new node
  42. void traverseNodes(struct Node*); // Traverse and print the multilevel linked list
  43. void traverseNodesWithoutJobPlace(struct Node*); // Print only the people without job
  44. struct Node **deleteFromDynamicArray(int indexToDelete, struct Node**DynamicArrayInput); // delete given index from the dynamic array
  45.  
  46. int currentNode = 1;
  47. int currentNodeUnmployedList = 1;
  48. struct Node* tempFirst;
  49. int printFlag = 0;
  50. int numberOfPeople = 0;
  51.  
  52.  
  53.  
  54. int main()
  55. {
  56.     struct Node **DynamicArray;
  57.     int dynamicArraySize = 1;
  58.     DynamicArray = new struct Node*[dynamicArraySize];
  59.  
  60.     typedef struct Node* *testDynamicArray;
  61.     struct Node *Array[5];
  62.     struct Node **temp;
  63.  
  64.  
  65.     int loop = 1;
  66.     while(loop)
  67.     {
  68.         //dynamicArraySize = sizeof(DynamicArray)/sizeof(DynamicArray[0]);
  69.         cout << "Menu: " << endl;
  70.         cout << "1. Press 1 to add person" << endl;
  71.         cout << "2. Press 2 to show the list" << endl;
  72.         cout << "3. Press 3 to show unemployed people" << endl;
  73.         cout << "4. Press 4 to delete worker by index from the list" << endl;
  74.         cout << "5. Press 0 to exit" << endl;
  75.         int inputCommand;
  76.         cin >> inputCommand;
  77.         switch(inputCommand)
  78.         {
  79.         case 1:
  80.             enterPerson();
  81.             if(numberOfPeople == 0)
  82.             {
  83.                  // Saving pointer in the first address of the dynamic array
  84.                 DynamicArray = new struct Node*[numberOfPeople];
  85.                 DynamicArray[0] = addNode();
  86.                 cout << "dynamic array[0] " << DynamicArray[0]->data<< endl;
  87.  
  88.             }
  89.             else
  90.             {
  91.                 // Temporary array [numberOfPeople+1]
  92.                 temp = new struct Node*[numberOfPeople+1];
  93.  
  94.                 // Copy data to temp array
  95.                 for(int i =0; i<numberOfPeople;i++)
  96.                 {
  97.                     temp[i] = DynamicArray[i];
  98.                 }
  99.  
  100.                 // Remove dynamic array
  101.                 delete[] DynamicArray;
  102.                 // temp = nullptr;
  103.  
  104.  
  105.                 DynamicArray = temp;
  106.                 // Add Node to the dynamic array
  107.                 DynamicArray[numberOfPeople] = addNode();
  108.  
  109.             }
  110.             numberOfPeople++;
  111.             break;
  112.         case 2:
  113.             for(int i =0; i<numberOfPeople;i++)
  114.             {
  115.                 traverseNodes(DynamicArray[i]);
  116.             }
  117.  
  118.             break;
  119.         case 3:
  120.             for(int i =0; i<numberOfPeople;i++)
  121.             {
  122.                 traverseNodesWithoutJobPlace(DynamicArray[i]);
  123.             }
  124.             break;
  125.         case 4:
  126.             int index;
  127.             cout << "Enter the index " << endl;
  128.             cin >> index;
  129.             struct Node **tempArr;
  130.             tempArr = DynamicArray;
  131.             delete DynamicArray;
  132.             //DynamicArray = deleteFromDynamicArray(index, DynamicArray);
  133.             tempArr = deleteFromDynamicArray(index, tempArr);
  134.             DynamicArray = tempArr;
  135.             break;
  136.         case 0:
  137.             loop = 0;
  138.             break;
  139.         default:
  140.  
  141.             break;
  142.  
  143.         }
  144.     }
  145. }
  146.  
  147. struct Node **deleteFromDynamicArray(int indexToDelete, struct Node*DynamicArrayInput[])
  148. {
  149.     struct Node** tempForDelete;
  150.     //DynamicArrayInput = new struct Node*[numberOfPeople];
  151.     tempForDelete = new struct Node*[numberOfPeople];
  152.     cout << "Dynamic array input data " << DynamicArrayInput[0]->data << endl;
  153.     for(int i =0; i<numberOfPeople;i++)
  154.     {
  155.         tempForDelete[i] = DynamicArrayInput[i];
  156.     }
  157.     int nextElementShifting = 0;
  158.     delete[] DynamicArrayInput;
  159.     for(int i=0; i<numberOfPeople-1;i++)
  160.     {
  161.         if(i+1==indexToDelete)
  162.         {
  163.             nextElementShifting++;
  164.         }
  165.         DynamicArrayInput[i] = tempForDelete[i+nextElementShifting];
  166.     }
  167.     cout << "DynamicArrayInput data " << DynamicArrayInput[0]->data << endl;
  168.     delete[] tempForDelete;
  169.     numberOfPeople--;
  170.     return DynamicArrayInput;
  171. }
  172.  
  173.  
  174.  
  175. struct Node* addNode()
  176. {
  177.    struct Node* first;
  178.    first = new Node;
  179.    first->data = firstName;
  180.    Node* secN = new Node;
  181.    secN->data = secondName;
  182.    secN->child = NULL;
  183.    first->next =secN;
  184.  
  185.  
  186.    Node* lastN = new Node;
  187.    lastN ->data = lastName;
  188.    secN->next = lastN;
  189.    lastN->child = NULL;
  190.  
  191.    Node* adress1Node = new Node;
  192.    adress1Node->data = adress1;
  193.    adress1Node->child = NULL;
  194.  
  195.    Node* adress2Node = new Node;
  196.    adress2Node->data = adress2;
  197.    adress1Node->next = adress2Node;
  198.    adress2Node->child = NULL;
  199.  
  200.  
  201.    Node* adress3Node = new Node;
  202.    adress3Node->data = adress3;
  203.    adress2Node->next = adress3Node;
  204.    adress3Node->child = NULL;
  205.  
  206.    first->child = adress1Node;
  207.  
  208.    Node* firstNumberNode = new Node;
  209.    firstNumberNode->data = phoneNumber1;
  210.    firstNumberNode->next = NULL;
  211.    firstNumberNode->child = NULL;
  212.  
  213.    Node* secondNumberNode = new Node;
  214.    secondNumberNode->data = phoneNumber2;
  215.    secondNumberNode->next = NULL;
  216.    secondNumberNode->child = NULL;
  217.    firstNumberNode->next = secondNumberNode;
  218.  
  219.    Node* thirdNumberNode = new Node;
  220.    thirdNumberNode->data = phoneNumber3;
  221.    thirdNumberNode->next = NULL;
  222.    thirdNumberNode->child = NULL;
  223.  
  224.    adress1Node->child = firstNumberNode;
  225.    adress3Node->child = thirdNumberNode;
  226.     return first;
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233. void traverseNodesWithoutJobPlace(struct Node* p)
  234. {
  235.     if(currentNodeUnmployedList>9 && p)
  236.     {
  237.         currentNodeUnmployedList = 1;
  238.         printFlag = 0;
  239.         cout << "_______" << endl;
  240.     }
  241.     if(currentNodeUnmployedList==1 && p)
  242.     {
  243.         cout << " stava 1 " << endl;
  244.         tempFirst = p;
  245.  
  246.     }
  247.    // if(tempFirst->child->next->data.empty() && tempFirst->child->next->next->data.empty())
  248.    if(tempFirst!=NULL && currentNodeUnmployedList == 1 && tempFirst->child->next->data.empty() && tempFirst->child->next->next->data.empty())
  249.    {
  250.      printFlag = 1;
  251.    }
  252.  
  253.  
  254.     if(p==secondChild)
  255.        secondChild=NULL;
  256.     if(p==firstChild)
  257.         firstChild = NULL;
  258.     while(p)
  259.     {
  260.  
  261.       if(p->child)
  262.       {
  263.          if(secondChild == NULL && firstChild!=NULL)
  264.          {
  265.             secondChild = p->child;
  266.          }
  267.  
  268.          if(firstChild == NULL)
  269.             firstChild = p->child;
  270.  
  271.       }
  272.         if(printFlag)
  273.         {
  274.             cout << " trqbva da printira ";
  275.             cout << p->data << " ";
  276.  
  277.         }
  278.         p = p->next;
  279.         currentNodeUnmployedList++;
  280.     }
  281.     if(firstChild !=NULL || secondChild != NULL)
  282.     {
  283.  
  284.         if(firstChild)
  285.         {
  286.             traverseNodesWithoutJobPlace(firstChild);
  287.  
  288.         }
  289.         if(secondChild)
  290.         {
  291.             traverseNodesWithoutJobPlace(secondChild);
  292.  
  293.         }
  294.  
  295.     }
  296. }
  297.  
  298.  
  299.  
  300. void traverseNodes(struct Node* p)
  301. {
  302.     if(p==secondChild)
  303.        secondChild=NULL;
  304.     if(p==firstChild)
  305.         firstChild = NULL;
  306.     while(p)
  307.     {
  308.  
  309.       if(p->child)
  310.       {
  311.          if(secondChild == NULL && firstChild!=NULL)
  312.          {
  313.             secondChild = p->child;
  314.          }
  315.  
  316.          if(firstChild == NULL)
  317.             firstChild = p->child;
  318.  
  319.       }
  320.         cout << p->data << " ";
  321.         p = p->next;
  322.     }
  323.  
  324.     if(firstChild !=NULL || secondChild != NULL)
  325.     {
  326.  
  327.         if(firstChild)
  328.         {
  329.             traverseNodes(firstChild);
  330.  
  331.         }
  332.         if(secondChild)
  333.         {
  334.             //cout << " v ifa second child data " << secondChild->data << endl;
  335.             traverseNodes(secondChild);
  336.            // cout << " second child " << endl;
  337.  
  338.         }
  339.  
  340.         //enqueue(lastChild);
  341.     }
  342. }
  343.  
  344. void enterPerson()
  345. {
  346.     cin.ignore();
  347.     cout << "Enter first name: " << endl;
  348.     cin >> firstName;
  349.     cout << "Enter second name: " << endl;
  350.     cin >> secondName;
  351.     cout << "Enter last name name: " << endl;
  352.     cin >> lastName;
  353.     cout << "Enter first address: " << endl;
  354.     cin >> adress1;
  355.     cout << "Enter second address: " << endl;
  356.     cin.ignore();
  357.     getline(cin,adress2);
  358.     cout << "Enter third address: " << endl;
  359.     getline(cin,adress3);
  360.     cout << "Enter first phone number: " << endl;
  361.     cin >> phoneNumber1;
  362.     cout << "Enter second phone number: " << endl;
  363.     cin >> phoneNumber2;
  364.     cout << "Enter third phone number: " << endl;
  365.     cin >> phoneNumber3;
  366.  
  367. }
  368.  
  369.  
  370.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement