Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // Трите имена
- // Домашни и служебни адреси(индикатор за различаване)
- // телефони на съответните адреси
- // -създаване на указателя, като информацията се чете
- // клавиатурата
- // -извежда информация за броя на работещите(лицето е
- // е работещо, ако има поне един служебен адрес
- string firstName;
- string secondName;
- string lastName;
- string adress1;
- string adress2;
- string adress3;
- string phoneNumber1;
- string phoneNumber2;
- string phoneNumber3;
- struct Node
- {
- string data;
- Node *next;
- Node *child;
- };
- 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
- void traverseNodes(struct Node*); // Traverse and print the multilevel linked list
- void traverseNodesWithoutJobPlace(struct Node*); // Print only the people without job
- int currentNode = 1;
- int currentNodeUnmployedList = 1;
- struct Node* tempFirst;
- int printFlag = 0;
- int main()
- {
- typedef struct Node* *testDynamicArray;
- struct Node *Array[5];
- struct Node **DynamicArray;
- struct Node **temp;
- int dynamicArraySize = 1;
- DynamicArray = new struct Node*[dynamicArraySize];
- int numberOfPeople = 0;
- int loop = 1;
- while(loop)
- {
- //dynamicArraySize = sizeof(DynamicArray)/sizeof(DynamicArray[0]);
- 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 0 to exit" << endl;
- int inputCommand;
- cin >> inputCommand;
- switch(inputCommand)
- {
- case 1:
- enterPerson();
- if(numberOfPeople == 0)
- {
- // Saving pointer in the first address of the dynamic array
- DynamicArray = new struct Node*[numberOfPeople];
- DynamicArray[0] = addNode();
- cout << "dynamic array[0] " << DynamicArray[0]->data<< endl;
- }
- 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;
- case 2:
- for(int i =0; i<numberOfPeople;i++)
- {
- traverseNodes(DynamicArray[i]);
- }
- break;
- case 3:
- for(int i =0; i<numberOfPeople;i++)
- {
- traverseNodesWithoutJobPlace(DynamicArray[i]);
- }
- break;
- case 0:
- loop = 0;
- break;
- default:
- break;
- }
- }
- }
- struct Node* addNode()
- {
- struct Node* first;
- first = new Node;
- first->data = firstName;
- Node* secN = new Node;
- secN->data = secondName;
- secN->child = NULL;
- first->next =secN;
- Node* lastN = new Node;
- lastN ->data = lastName;
- secN->next = lastN;
- lastN->child = NULL;
- Node* adress1Node = new Node;
- adress1Node->data = adress1;
- adress1Node->child = NULL;
- Node* adress2Node = new Node;
- adress2Node->data = adress2;
- adress1Node->next = adress2Node;
- adress2Node->child = NULL;
- Node* adress3Node = new Node;
- adress3Node->data = adress3;
- adress2Node->next = adress3Node;
- adress3Node->child = NULL;
- first->child = adress1Node;
- Node* firstNumberNode = new Node;
- firstNumberNode->data = phoneNumber1;
- firstNumberNode->next = NULL;
- firstNumberNode->child = NULL;
- Node* secondNumberNode = new Node;
- secondNumberNode->data = phoneNumber2;
- secondNumberNode->next = NULL;
- secondNumberNode->child = NULL;
- firstNumberNode->next = secondNumberNode;
- Node* thirdNumberNode = new Node;
- thirdNumberNode->data = phoneNumber3;
- thirdNumberNode->next = NULL;
- thirdNumberNode->child = NULL;
- adress1Node->child = firstNumberNode;
- adress3Node->child = thirdNumberNode;
- return first;
- }
- void traverseNodesWithoutJobPlace(struct Node* p)
- {
- if(currentNodeUnmployedList>9 && p)
- {
- currentNodeUnmployedList = 1;
- printFlag = 0;
- cout << "_______" << endl;
- }
- if(currentNodeUnmployedList==1 && p)
- {
- cout << " stava 1 " << endl;
- tempFirst = p;
- }
- // if(tempFirst->child->next->data.empty() && tempFirst->child->next->next->data.empty())
- if(tempFirst!=NULL && currentNodeUnmployedList == 1 && tempFirst->child->next->data.empty() && tempFirst->child->next->next->data.empty())
- {
- printFlag = 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(printFlag)
- {
- cout << " trqbva da printira ";
- cout << p->data << " ";
- }
- p = p->next;
- currentNodeUnmployedList++;
- }
- if(firstChild !=NULL || secondChild != NULL)
- {
- if(firstChild)
- {
- traverseNodesWithoutJobPlace(firstChild);
- }
- if(secondChild)
- {
- traverseNodesWithoutJobPlace(secondChild);
- }
- }
- }
- 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;
- }
- if(firstChild !=NULL || secondChild != NULL)
- {
- if(firstChild)
- {
- traverseNodes(firstChild);
- }
- if(secondChild)
- {
- //cout << " v ifa second child data " << secondChild->data << endl;
- traverseNodes(secondChild);
- // cout << " second child " << endl;
- }
- //enqueue(lastChild);
- }
- }
- void enterPerson()
- {
- cin.ignore();
- cout << "Enter first name: " << endl;
- cin >> firstName;
- cout << "Enter second name: " << endl;
- cin >> secondName;
- cout << "Enter last name name: " << endl;
- cin >> lastName;
- cout << "Enter first address: " << endl;
- cin >> adress1;
- cout << "Enter second address: " << endl;
- cin.ignore();
- getline(cin,adress2);
- cout << "Enter third address: " << endl;
- getline(cin,adress3);
- cout << "Enter first phone number: " << endl;
- cin >> phoneNumber1;
- cout << "Enter second phone number: " << endl;
- cin >> phoneNumber2;
- cout << "Enter third phone number: " << endl;
- cin >> phoneNumber3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement