Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- #define nodesPerPerson 7 // nodes for 1 person; used for traversing
- // Multilevel list definition
- struct Node
- {
- string data;
- Node* next;
- Node* child;
- }*prevPerson = NULL;
- // Stack definiton for DFS
- struct StackNode
- {
- struct Node* data;
- struct StackNode* next;
- }*top = NULL;
- // Helper pointers for name nodes connection
- struct Node* TEMP = NULL;
- struct Node* LAST = NULL;
- // Helper pointers for addNode()
- struct Node* lastPersonAdded;
- struct Node* firstPersonAdded = NULL;
- // Main functions for the pointer
- 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 in enterPerson()
- void traverseList(struct Node* FIRST); // Traverse the list
- void traverseListEmployed(struct Node* p); // Traversing the list, printing the employed people (new version)
- // Stack functions
- void insertStack(struct Node* n); // Insert an element in the stack
- struct Node* popStack(); // Pop element from stack
- void printStack(); // [FOR DEBUG]
- int stackNumberOfElements(); // [FOR DEBUG]
- // Variables for traversing
- int numberOfPeople = 0; // Number of people in the pointer
- int flagPrintEmployedList = 1;
- int static counterTraverse = 0;
- int static counterTraverseEmployedList = 0;
- // Variables for the input
- string names;
- string addresses[3];
- string phoneNumbers[3];
- int main()
- {
- int menuLoop = 1;
- while (menuLoop)
- {
- cout << "\nMenu: " << endl;
- cout << "1. Press 1 to add person" << endl;
- cout << "2. Press 2 to show the whole list" << endl;
- cout << "3. Press 3 to show the list of employed people" << endl;
- cout << "5. Press 0 to exit" << endl;
- int inputCommand;
- cin >> inputCommand;
- switch (inputCommand)
- {
- case 1:
- {
- enterPerson();
- // Connecting the list name[n] with name[n+1]
- if (TEMP == NULL)
- {
- TEMP = addNode();
- TEMP->next = NULL;
- LAST = TEMP;
- }
- else
- {
- TEMP = addNode();
- TEMP->next = NULL;
- LAST->next = TEMP;
- LAST = TEMP;
- }
- break;
- }
- case 2:
- {
- if (firstPersonAdded)
- {
- cout << "List of all people in the pointer: " << endl;
- traverseList(firstPersonAdded);
- }
- else
- {
- cout << "The list is empty." << endl;
- }
- counterTraverse = 0;
- break;
- }
- case 3:
- {
- if (firstPersonAdded)
- {
- cout << "List of all EMPLOYED people in the pointer: " << endl;
- traverseListEmployed(firstPersonAdded);
- }
- else
- {
- cout << "The list is empty. " << endl;
- }
- counterTraverseEmployedList = 0;
- }
- break;
- case 0:
- menuLoop = 0;
- break;
- default:
- break;
- }
- }
- }
- void enterPerson()
- {
- cin.ignore();
- cout << "Enter first name: " << endl;
- cin >> names;
- 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() // string names;
- {
- struct Node* first;
- first = new Node;
- first->data = names;
- first->next = NULL;
- prevPerson = first;
- struct Node* adress1Node = new Node;
- adress1Node->data = addresses[0];
- adress1Node->child = NULL;
- struct Node* adress2Node = new Node;
- adress2Node->data = addresses[1];
- adress1Node->next = adress2Node;
- adress2Node->child = NULL;
- struct Node* adress3Node = new Node;
- adress3Node->data = addresses[2];
- adress2Node->next = adress3Node;
- adress3Node->child = NULL;
- first->child = adress1Node;
- struct Node* firstNumberNode = new Node;
- firstNumberNode->data = phoneNumbers[0];
- firstNumberNode->next = NULL;
- firstNumberNode->child = NULL;
- struct Node* secondNumberNode = new Node;
- secondNumberNode->data = phoneNumbers[1];
- secondNumberNode->next = NULL;
- secondNumberNode->child = NULL;
- firstNumberNode->next = secondNumberNode;
- struct Node* thirdNumberNode = new Node;
- thirdNumberNode->data = phoneNumbers[2];
- thirdNumberNode->next = NULL;
- thirdNumberNode->child = NULL;
- adress1Node->child = firstNumberNode;
- adress3Node->child = thirdNumberNode;
- if (numberOfPeople > 0)
- {
- lastPersonAdded->next = first;
- lastPersonAdded = first;
- }
- else
- {
- firstPersonAdded = first;
- lastPersonAdded = first;
- }
- numberOfPeople++;
- return first;
- }
- void traverseList(struct Node* p)
- {
- int traverseFlag = 1;
- int temp = 0; // helper variable
- if (numberOfPeople > 1)
- {
- temp = 1;
- }
- if (counterTraverse == (numberOfPeople * nodesPerPerson) + temp)
- {
- counterTraverse = 0;
- traverseFlag = 0;
- }
- counterTraverse++;
- if (traverseFlag)
- {
- if (p) // Print node's value
- {
- cout << " " << p->data;
- }
- if (p->child && p->next)
- {
- insertStack(p->next);
- traverseList(p->child);
- }
- else if (p->child)
- {
- traverseList(p->child);
- }
- else if (p->next)
- {
- traverseList(p->next);
- }
- else if (top == NULL && p->child == NULL && p->next == NULL)
- {
- }
- // There are no child and next pointers but stack is not empty.
- else if (p->next == NULL && p->child == NULL && top != NULL)
- {
- p = popStack();
- traverseList(p);
- }
- }
- }
- void traverseListEmployed(struct Node* p)
- {
- struct Node* address2 = NULL;
- struct Node* address3 = NULL;
- counterTraverseEmployedList++;
- if (counterTraverseEmployedList == nodesPerPerson + 1)
- {
- counterTraverseEmployedList = 1;
- if (p->child != nullptr)
- {
- if (p->child->next)
- address2 = p->child->next;
- if (p->child->next->next)
- address3 = p->child->next->next;
- }
- // If address2 && address3 are empty printing is off
- if (address2->data.empty() == 1 && address3->data.empty() == 1)
- {
- flagPrintEmployedList = 0;
- }
- else
- {
- flagPrintEmployedList = 1;
- }
- }
- if (p) // Print node's value
- {
- if (flagPrintEmployedList)
- {
- cout << " " << p->data;
- }
- }
- if (p->child && p->next)
- {
- insertStack(p->next);
- traverseListEmployed(p->child);
- }
- else if (p->child)
- {
- traverseListEmployed(p->child);
- }
- else if (p->next)
- {
- traverseListEmployed(p->next);
- }
- else if (top == NULL && p->child == NULL && p->next == NULL)
- {
- return;
- }
- // There are no child and next pointers but stack is not empty.
- else if (p->next == NULL && p->child == NULL && top != NULL)
- {
- p = popStack();
- traverseListEmployed(p);
- }
- }
- //------------------------------------------------ STACK FUNCTIONS ---------------------------------------------------
- void insertStack(struct Node* p)
- {
- if (top == NULL)
- {
- top = new StackNode;
- top->data = p;
- top->next = NULL;
- }
- else
- {
- StackNode* t = new StackNode;
- t->data = p;
- t->next = top;
- top = t;
- }
- }
- int stackNumberOfElements()
- {
- int countStack = 0;
- StackNode* p = top;
- while (p)
- {
- p = p->next;
- countStack++;
- }
- return countStack;
- }
- struct Node* popStack()
- {
- struct StackNode* p;
- struct Node* t;
- p = top;
- t = top->data;
- top = top->next;
- delete p;
- return t;
- }
- void printStack()
- {
- struct StackNode* p = top;
- while (p != NULL)
- {
- cout << p->data->data << " ";
- p = p->next;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement