Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- string names[3];
- string addresses[3];
- string phoneNumbers[3];
- struct Node
- {
- string data;
- Node *next;
- Node *child;
- };
- /*
- * COULDN'T IMPLEMENT QUEUE OF DOUBLE POINTERS
- * Traversing by breadth
- * Node *firstChild && Node *secondChild are emulating queue; Helping nodes for traversing
- * for this multilevel linked list, where you have maximum two children in the queue
- *
- */
- Node *firstChild = NULL;
- Node *secondChild = NULL;
- void enterPerson(); // Read input from the console
- // third name, address1, address2, address3, Phone number 1,2,3.
- struct Node* addNode(); // Create a new node from the values entered in enterPerson()
- void traverseNodes(struct Node*); // Traverse by breadth and print the multilevel linked list
- void traverseNodesWithoutJobPlace(struct Node*); // Traverse by breadth and print only the people without job
- struct Node **deleteFromDynamicArray(int indexToDelete, struct Node**DynamicArrayInput); // Delete given index from the dynamic array
- int currentNode = 1;
- int currentNodeUnmployedList = 1;
- struct Node* tempFirst;
- int printFlagUnempoyedList = 0; // flag for traversing the list for unemployed people
- int numberOfPeople = 0;
- int main()
- {
- struct Node **DynamicArray;
- DynamicArray = new struct Node*[1]; // initialize the dynamic array with one element
- typedef struct Node* *testDynamicArray;
- struct Node **temp;
- int loop = 1;
- while(loop)
- {
- cout << "Menu: " << endl;
- cout << "1. Press 1 to add person" << endl;
- cout << "2. Press 2 to show the list" << endl;
- cout << "3. Press 3 to show unemployed people" << endl;
- cout << "4. Press 4 to delete worker by index from the list" << endl;
- cout << "5. Press 0 to exit" << endl;
- int inputCommand;
- cin >> inputCommand;
- switch(inputCommand)
- {
- case 1:
- enterPerson();
- // There are no people entered yet
- if(numberOfPeople == 0)
- {
- // Saving pointer in the first address of the dynamic array
- DynamicArray = new struct Node*[numberOfPeople];
- DynamicArray[0] = addNode();
- }
- else
- {
- // Temporary array [numberOfPeople+1]
- temp = new struct Node*[numberOfPeople+1];
- // Copy data to temp array
- for(int i =0; i<numberOfPeople;i++)
- {
- temp[i] = DynamicArray[i];
- }
- // Remove dynamic array
- delete[] DynamicArray;
- // temp = nullptr;
- DynamicArray = temp;
- // Add node to the dynamic array
- DynamicArray[numberOfPeople] = addNode();
- }
- numberOfPeople++;
- break;
- // Print all people in the array
- case 2:
- if(numberOfPeople>=1)
- {
- cout <<"Information for all of the people in the list" << endl;
- for(int i =0; i<numberOfPeople;i++)
- {
- traverseNodes(DynamicArray[i]);
- }
- }
- else
- cout << "There are no people in the list " << endl;
- break;
- // Print people without work addresses(address1 | address 2)
- case 3:
- cout << "Information for all of the unemployed people" << endl;
- for(int i =0; i<numberOfPeople;i++)
- {
- traverseNodesWithoutJobPlace(DynamicArray[i]);
- }
- if(printFlagUnempoyedList == 0)
- cout << "All people in the list have jobs" << endl;
- break;
- // Delete chosen employee from the array
- case 4:
- // Reading the index that need to deleted from the dynamic array.
- if(numberOfPeople>=1)
- {
- int index;
- cout << "Enter the index of the employee you want to remove from the list " << endl;
- cin >> index;
- while(index > numberOfPeople || index ==0) // input check for non existing indexes and 0 as input
- {
- cout << "Please enter a valid index. The list includes only " << numberOfPeople <<" people. 0 is not an option" << endl;
- cin>>index;
- }
- struct Node **tempArr;
- tempArr = DynamicArray;
- delete DynamicArray;
- tempArr = deleteFromDynamicArray(index, tempArr); // Returns the array with deleted element pointed by index
- DynamicArray = tempArr;
- }
- else
- cout << "There are no people in the list and deleting is not possible" << endl;
- break;
- case 0:
- loop = 0;
- break;
- default:
- break;
- }
- }
- }
- void enterPerson()
- {
- cin.ignore();
- cout << "Enter first name: " << endl;
- cin >> names[0];
- cout << "Enter second name: " << endl;
- cin >> names[1];
- cout << "Enter last name name: " << endl;
- cin >> names[2];
- cout << "Enter first address: " << endl;
- cin >> addresses[0];
- cout << "Enter second address: " << endl;
- cin.ignore();
- getline(cin,addresses[1]);
- cout << "Enter third address: " << endl;
- getline(cin,addresses[2]);
- cout << "Enter first phone number: " << endl;
- cin >> phoneNumbers[0];
- cout << "Enter second phone number: " << endl;
- cin >> phoneNumbers[1];
- cout << "Enter third phone number: " << endl;
- cin >> phoneNumbers[2];
- }
- struct Node* addNode()
- {
- struct Node* first;
- first = new Node;
- first->data = names[0];
- Node* secN = new Node;
- secN->data = names[1];
- secN->child = NULL;
- first->next =secN;
- Node* lastN = new Node;
- lastN ->data = names[2];
- secN->next = lastN;
- lastN->child = NULL;
- Node* adress1Node = new Node;
- adress1Node->data = addresses[0];
- adress1Node->child = NULL;
- Node* adress2Node = new Node;
- adress2Node->data = addresses[1];
- adress1Node->next = adress2Node;
- adress2Node->child = NULL;
- Node* adress3Node = new Node;
- adress3Node->data = addresses[2];
- adress2Node->next = adress3Node;
- adress3Node->child = NULL;
- first->child = adress1Node;
- Node* firstNumberNode = new Node;
- firstNumberNode->data = phoneNumbers[0];
- firstNumberNode->next = NULL;
- firstNumberNode->child = NULL;
- Node* secondNumberNode = new Node;
- secondNumberNode->data = phoneNumbers[1];
- secondNumberNode->next = NULL;
- secondNumberNode->child = NULL;
- firstNumberNode->next = secondNumberNode;
- Node* thirdNumberNode = new Node;
- thirdNumberNode->data = phoneNumbers[2];
- thirdNumberNode->next = NULL;
- thirdNumberNode->child = NULL;
- adress1Node->child = firstNumberNode;
- adress3Node->child = thirdNumberNode;
- return first;
- }
- void traverseNodes(struct Node* p)
- {
- if(p==secondChild)
- secondChild=NULL;
- if(p==firstChild)
- firstChild = NULL;
- while(p)
- {
- if(p->child)
- {
- if(secondChild == NULL && firstChild!=NULL)
- {
- secondChild = p->child;
- }
- if(firstChild == NULL)
- firstChild = p->child;
- }
- cout << p->data << " ";
- p = p->next;
- }
- // Traversing the next child
- if(firstChild !=NULL || secondChild != NULL)
- {
- if(firstChild)
- {
- traverseNodes(firstChild);
- }
- if(secondChild)
- {
- traverseNodes(secondChild);
- }
- }
- }
- void traverseNodesWithoutJobPlace(struct Node* p)
- {
- if(currentNodeUnmployedList>9 && p) // 9 is the number of nodes in the multilevel linked list, 3x addresses, 3x names, 3x phone numbers
- {
- currentNodeUnmployedList = 1;
- printFlagUnempoyedList = 0;
- }
- // Check for start of a new first node.
- if(currentNodeUnmployedList==1 && p)
- {
- tempFirst = p;
- }
- // Check the new first node. If address2 && address3 are empty the person is unemployed.
- if(tempFirst!=NULL && currentNodeUnmployedList == 1
- && tempFirst->child->next->data.empty()
- && tempFirst->child->next->next->data.empty())
- {
- printFlagUnempoyedList = 1;
- }
- if(p==secondChild)
- secondChild=NULL;
- if(p==firstChild)
- firstChild = NULL;
- while(p)
- {
- if(p->child)
- {
- if(secondChild == NULL && firstChild!=NULL)
- {
- secondChild = p->child;
- }
- if(firstChild == NULL)
- firstChild = p->child;
- }
- if(printFlagUnempoyedList) // address2 && address3 are empty so the person is unemployed and data will be printed
- {
- cout << p->data << " ";
- }
- p = p->next;
- currentNodeUnmployedList++;
- }
- // Traversing the next child
- if(firstChild !=NULL || secondChild != NULL)
- {
- if(firstChild)
- {
- traverseNodesWithoutJobPlace(firstChild);
- }
- if(secondChild)
- {
- traverseNodesWithoutJobPlace(secondChild);
- }
- }
- }
- struct Node **deleteFromDynamicArray(int indexToDelete, struct Node*DynamicArrayInput[])
- {
- struct Node** tempForDelete;
- tempForDelete = new struct Node*[numberOfPeople];
- cout << "Dynamic array input data " << DynamicArrayInput[0]->data << endl;
- for(int i =0; i<numberOfPeople;i++) // Sav
- {
- tempForDelete[i] = DynamicArrayInput[i];
- }
- int nextElementShifting = 0;
- delete[] DynamicArrayInput;
- for(int i=0; i<numberOfPeople-1;i++) // Saving the data from the temp array(input) to the output array, without the deleted index
- {
- if(i+1==indexToDelete)
- {
- nextElementShifting++;
- }
- DynamicArrayInput[i] = tempForDelete[i+nextElementShifting];
- }
- delete[] tempForDelete;
- numberOfPeople--;
- return DynamicArrayInput;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement