Advertisement
193030

Kursova SAA predadena

Dec 16th, 2020
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #define nodesPerPerson 7 // nodes for 1 person; used for traversing
  6.  
  7.  
  8. // Multilevel list definition
  9. struct Node
  10. {
  11.     string data;
  12.     Node* next;
  13.     Node* child;
  14. }*prevPerson = NULL;
  15.  
  16. // Stack definiton for DFS
  17. struct StackNode
  18. {
  19.     struct Node* data;
  20.     struct StackNode* next;
  21. }*top = NULL;
  22.  
  23. // Helper pointers for name nodes connection
  24. struct Node* TEMP = NULL;
  25. struct Node* LAST = NULL;
  26.  
  27. // Helper pointers for addNode()
  28. struct Node* lastPersonAdded;
  29. struct Node* firstPersonAdded = NULL;
  30.  
  31. // Main functions for the pointer
  32. void enterPerson();     // Read input from the console; third name, address1, address2, address3, Phone number 1,2,3.
  33. struct Node* addNode(); // Create a new node from the values in enterPerson()
  34. void traverseList(struct Node* FIRST);  // Traverse the list
  35. void traverseListEmployed(struct Node* p);  // Traversing the list, printing the employed people (new version)
  36.  
  37. // Stack functions
  38. void insertStack(struct Node* n);       // Insert an element in the stack
  39. struct Node* popStack();                // Pop element from stack
  40. void printStack();                      // [FOR DEBUG]
  41. int stackNumberOfElements();            // [FOR DEBUG]
  42.  
  43. // Variables for traversing
  44. int numberOfPeople = 0;      // Number of people in the pointer
  45. int flagPrintEmployedList = 1;
  46. int static counterTraverse = 0;
  47. int static counterTraverseEmployedList = 0;
  48.  
  49.  
  50. // Variables for the input
  51. string names;
  52. string addresses[3];
  53. string phoneNumbers[3];
  54.  
  55.  
  56. int main()
  57. {
  58.     int menuLoop = 1;
  59.     while (menuLoop)
  60.     {
  61.         cout << "\nMenu: " << endl;
  62.         cout << "1. Press 1 to add person" << endl;
  63.         cout << "2. Press 2 to show the whole list" << endl;
  64.         cout << "3. Press 3 to show the list of employed people" << endl;
  65.         cout << "5. Press 0 to exit" << endl;
  66.         int inputCommand;
  67.         cin >> inputCommand;
  68.         switch (inputCommand)
  69.         {
  70.         case 1:
  71.         {
  72.             enterPerson();
  73.  
  74.             // Connecting the list name[n] with name[n+1]
  75.             if (TEMP == NULL)
  76.             {
  77.                 TEMP = addNode();
  78.                 TEMP->next = NULL;
  79.                 LAST = TEMP;
  80.             }
  81.             else
  82.             {
  83.                 TEMP = addNode();
  84.                 TEMP->next = NULL;
  85.                 LAST->next = TEMP;
  86.                 LAST = TEMP;
  87.             }
  88.             break;
  89.         }
  90.         case 2:
  91.         {
  92.             if (firstPersonAdded)
  93.             {
  94.                 cout << "List of all people in the pointer: " << endl;
  95.                 traverseList(firstPersonAdded);
  96.             }
  97.             else
  98.             {
  99.                 cout << "The list is empty." << endl;
  100.             }
  101.             counterTraverse = 0;
  102.             break;
  103.         }
  104.         case 3:
  105.         {
  106.             if (firstPersonAdded)
  107.             {
  108.                 cout << "List of all EMPLOYED people in the pointer: " << endl;
  109.                 traverseListEmployed(firstPersonAdded);
  110.             }
  111.             else
  112.             {
  113.                 cout << "The list is empty. " << endl;
  114.             }
  115.             counterTraverseEmployedList = 0;
  116.         }
  117.         break;
  118.         case 0:
  119.             menuLoop = 0;
  120.             break;
  121.         default:
  122.  
  123.             break;
  124.         }
  125.     }
  126. }
  127.  
  128. void enterPerson()
  129. {
  130.     cin.ignore();
  131.     cout << "Enter first name: " << endl;
  132.     cin >> names;
  133.     cout << "Enter first address: " << endl;
  134.     cin >> addresses[0];
  135.     cout << "Enter second address: " << endl;
  136.     cin.ignore();
  137.     getline(cin, addresses[1]);
  138.     cout << "Enter third address: " << endl;
  139.     getline(cin, addresses[2]);
  140.     cout << "Enter first phone number: " << endl;
  141.     cin >> phoneNumbers[0];
  142.     cout << "Enter second phone number: " << endl;
  143.     cin >> phoneNumbers[1];
  144.     cout << "Enter third phone number: " << endl;
  145.     cin >> phoneNumbers[2];
  146.  
  147. }
  148.  
  149. struct Node* addNode() // string names;
  150. {
  151.     struct Node* first;
  152.     first = new Node;
  153.     first->data = names;
  154.     first->next = NULL;
  155.     prevPerson = first;
  156.  
  157.     struct Node* adress1Node = new Node;
  158.     adress1Node->data = addresses[0];
  159.     adress1Node->child = NULL;
  160.  
  161.     struct Node* adress2Node = new Node;
  162.     adress2Node->data = addresses[1];
  163.     adress1Node->next = adress2Node;
  164.     adress2Node->child = NULL;
  165.  
  166.     struct Node* adress3Node = new Node;
  167.     adress3Node->data = addresses[2];
  168.     adress2Node->next = adress3Node;
  169.     adress3Node->child = NULL;
  170.  
  171.     first->child = adress1Node;
  172.  
  173.     struct Node* firstNumberNode = new Node;
  174.     firstNumberNode->data = phoneNumbers[0];
  175.     firstNumberNode->next = NULL;
  176.     firstNumberNode->child = NULL;
  177.  
  178.     struct Node* secondNumberNode = new Node;
  179.     secondNumberNode->data = phoneNumbers[1];
  180.     secondNumberNode->next = NULL;
  181.     secondNumberNode->child = NULL;
  182.     firstNumberNode->next = secondNumberNode;
  183.  
  184.     struct Node* thirdNumberNode = new Node;
  185.     thirdNumberNode->data = phoneNumbers[2];
  186.     thirdNumberNode->next = NULL;
  187.     thirdNumberNode->child = NULL;
  188.  
  189.     adress1Node->child = firstNumberNode;
  190.     adress3Node->child = thirdNumberNode;
  191.  
  192.     if (numberOfPeople > 0)
  193.     {
  194.         lastPersonAdded->next = first;
  195.         lastPersonAdded = first;
  196.     }
  197.     else
  198.     {
  199.         firstPersonAdded = first;
  200.         lastPersonAdded = first;
  201.     }
  202.     numberOfPeople++;
  203.     return first;
  204. }
  205.  
  206. void traverseList(struct Node* p)
  207. {
  208.     int traverseFlag = 1;
  209.     int temp = 0;   // helper variable
  210.     if (numberOfPeople > 1)
  211.     {
  212.         temp = 1;
  213.     }
  214.     if (counterTraverse == (numberOfPeople * nodesPerPerson) + temp)
  215.     {
  216.         counterTraverse = 0;
  217.         traverseFlag = 0;
  218.     }
  219.     counterTraverse++;
  220.  
  221.     if (traverseFlag)
  222.     {
  223.         if (p) // Print node's value
  224.         {
  225.             cout << " " << p->data;
  226.         }
  227.         if (p->child && p->next)
  228.         {
  229.             insertStack(p->next);
  230.             traverseList(p->child);
  231.         }
  232.         else if (p->child)
  233.         {
  234.             traverseList(p->child);
  235.         }
  236.         else if (p->next)
  237.         {
  238.             traverseList(p->next);
  239.         }
  240.         else if (top == NULL && p->child == NULL && p->next == NULL)
  241.         {
  242.         }
  243.  
  244.         // There are no child and next pointers but stack is not empty.
  245.         else if (p->next == NULL && p->child == NULL && top != NULL)
  246.         {
  247.             p = popStack();
  248.             traverseList(p);
  249.         }
  250.     }
  251. }
  252.  
  253. void traverseListEmployed(struct Node* p)
  254. {
  255.     struct Node* address2 = NULL;
  256.     struct Node* address3 = NULL;
  257.     counterTraverseEmployedList++;
  258.     if (counterTraverseEmployedList == nodesPerPerson + 1)
  259.     {
  260.         counterTraverseEmployedList = 1;
  261.         if (p->child != nullptr)
  262.         {
  263.             if (p->child->next)
  264.                 address2 = p->child->next;
  265.             if (p->child->next->next)
  266.                 address3 = p->child->next->next;
  267.         }
  268.  
  269.         // If address2 && address3 are empty printing is off
  270.         if (address2->data.empty() == 1 && address3->data.empty() == 1)
  271.         {
  272.             flagPrintEmployedList = 0;
  273.         }
  274.         else
  275.         {
  276.             flagPrintEmployedList = 1;
  277.         }
  278.     }
  279.  
  280.     if (p) // Print node's value
  281.     {
  282.         if (flagPrintEmployedList)
  283.         {
  284.             cout << " " << p->data;
  285.         }
  286.     }
  287.     if (p->child && p->next)
  288.     {
  289.         insertStack(p->next);
  290.         traverseListEmployed(p->child);
  291.     }
  292.     else if (p->child)
  293.     {
  294.         traverseListEmployed(p->child);
  295.     }
  296.     else if (p->next)
  297.     {
  298.         traverseListEmployed(p->next);
  299.     }
  300.     else if (top == NULL && p->child == NULL && p->next == NULL)
  301.     {
  302.         return;
  303.     }
  304.  
  305.     // There are no child and next pointers but stack is not empty.
  306.     else if (p->next == NULL && p->child == NULL && top != NULL)
  307.     {
  308.         p = popStack();
  309.         traverseListEmployed(p);
  310.     }
  311. }
  312.  
  313.  
  314. //------------------------------------------------ STACK FUNCTIONS ---------------------------------------------------
  315. void insertStack(struct Node* p)
  316. {
  317.     if (top == NULL)
  318.     {
  319.         top = new StackNode;
  320.         top->data = p;
  321.         top->next = NULL;
  322.     }
  323.     else
  324.     {
  325.         StackNode* t = new StackNode;
  326.         t->data = p;
  327.         t->next = top;
  328.         top = t;
  329.     }
  330. }
  331.  
  332. int stackNumberOfElements()
  333. {
  334.     int countStack = 0;
  335.     StackNode* p = top;
  336.     while (p)
  337.     {
  338.         p = p->next;
  339.         countStack++;
  340.     }
  341.     return countStack;
  342. }
  343.  
  344. struct Node* popStack()
  345. {
  346.     struct StackNode* p;
  347.     struct Node* t;
  348.     p = top;
  349.     t = top->data;
  350.     top = top->next;
  351.     delete p;
  352.     return t;
  353.  
  354. }
  355.  
  356. void printStack()
  357. {
  358.     struct StackNode* p = top;
  359.     while (p != NULL)
  360.     {
  361.         cout << p->data->data << " ";
  362.         p = p->next;
  363.     }
  364.     cout << endl;
  365. }
  366.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement