Advertisement
193030

Kursova SAA NEW_VERSION g/d raboti da se vidi pop funkciqta delete v stacka

Oct 21st, 2020 (edited)
1,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. // Raboti do nqkyde. next ne raboti
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. string names;
  7. string addresses[3];
  8. string phoneNumbers[3];
  9.  
  10. struct Node
  11. {
  12.     string data;
  13.     Node *next;
  14.     Node *child;
  15. } *prevPerson = NULL;
  16.  
  17.  
  18. struct StackNode
  19. {
  20.     struct Node* data;
  21.     struct StackNode *next;
  22. } *top = NULL;
  23.  
  24.  
  25. void enterPerson(); // Read input from the console
  26. // third name, address1, address2, address3, Phone number 1,2,3.
  27. struct Node* addNode(); // Create a new node from the values entered in enterPerson()
  28. void insertStack(struct Node *n); // Inserting an element in the stack
  29. struct Node* popStack(); // Poping element from stack
  30. void traverseList(struct Node *FIRST); // Traversing the list
  31. void printStack();
  32. int stackNumberOfElements();
  33.  
  34.  
  35.  
  36. struct Node* lastPersonAdded;
  37. struct Node* firstPersonAdded = NULL;
  38.  
  39. int numberOfPeople = 0;
  40.  
  41. int main()
  42. {
  43.     int loop =1;
  44.     while(loop)
  45.     {
  46.         cout << "Menu: " << endl;
  47.         cout << "1. Press 1 to add person" << endl;
  48.         cout << "2. Press 2 to show the list" << endl;
  49.         cout << "3. Press 3 to show unemployed people" << endl;
  50.         cout << "4. Press 4 to delete worker by index from the list" << endl;
  51.         cout << "5. Press 0 to exit" << endl;
  52.         int inputCommand;
  53.         cin >> inputCommand;
  54.         switch(inputCommand)
  55.         {
  56.         case 1:
  57.         {
  58.             enterPerson();
  59.             struct Node* FIRST = addNode();
  60.             break;
  61.         }
  62.         case 2:
  63.             {
  64.             traverseList(firstPersonAdded);
  65.             break;
  66.             }
  67.         case 3:
  68.         {
  69.             cout << lastPersonAdded->data << endl;
  70.         }
  71.         break;
  72.         case 4:
  73.         {
  74.             cout << firstPersonAdded->data << endl;
  75.             cout << firstPersonAdded->next->data << endl;
  76.         }
  77.         break;
  78.  
  79.  
  80.         case 0:
  81.             loop = 0;
  82.             break;
  83.         default:
  84.  
  85.             break;
  86.  
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92. void enterPerson()
  93. {
  94.     cin.ignore();
  95.     cout << "Enter first name: " << endl;
  96.     cin >> names;
  97.     cout << "Enter first address: " << endl;
  98.     cin >> addresses[0];
  99.     cout << "Enter second address: " << endl;
  100.     cin.ignore();
  101.     getline(cin,addresses[1]);
  102.     cout << "Enter third address: " << endl;
  103.     getline(cin,addresses[2]);
  104.     cout << "Enter first phone number: " << endl;
  105.     cin >> phoneNumbers[0];
  106.     cout << "Enter second phone number: " << endl;
  107.     cin >> phoneNumbers[1];
  108.     cout << "Enter third phone number: " << endl;
  109.     cin >> phoneNumbers[2];
  110.  
  111. }
  112.  
  113.  
  114. struct Node* addNode()
  115. {
  116.    struct Node* first;
  117.    first = new Node;
  118.    first->data = names;
  119.    prevPerson = first;
  120.  
  121.    Node* adress1Node = new Node;
  122.    adress1Node->data = addresses[0];
  123.    adress1Node->child = NULL;
  124.  
  125.    Node* adress2Node = new Node;
  126.    adress2Node->data = addresses[1];
  127.    adress1Node->next = adress2Node;
  128.    adress2Node->child = NULL;
  129.  
  130.  
  131.    Node* adress3Node = new Node;
  132.    adress3Node->data = addresses[2];
  133.    adress2Node->next = adress3Node;
  134.    adress3Node->child = NULL;
  135.  
  136.    first->child = adress1Node;
  137.  
  138.    Node* firstNumberNode = new Node;
  139.    firstNumberNode->data = phoneNumbers[0];
  140.    firstNumberNode->next = NULL;
  141.    firstNumberNode->child = NULL;
  142.  
  143.    Node* secondNumberNode = new Node;
  144.    secondNumberNode->data = phoneNumbers[1];
  145.    secondNumberNode->next = NULL;
  146.    secondNumberNode->child = NULL;
  147.    firstNumberNode->next = secondNumberNode;
  148.  
  149.    Node* thirdNumberNode = new Node;
  150.    thirdNumberNode->data = phoneNumbers[2];
  151.    thirdNumberNode->next = NULL;
  152.    thirdNumberNode->child = NULL;
  153.  
  154.    adress1Node->child = firstNumberNode;
  155.    adress3Node->child = thirdNumberNode;
  156.  
  157.    if(numberOfPeople>0)
  158.    {
  159.        lastPersonAdded->next = first;
  160.        lastPersonAdded = first;
  161.    }
  162.    else
  163.    {
  164.        firstPersonAdded = first;
  165.        lastPersonAdded = first;
  166.    }
  167.     numberOfPeople++;
  168.     return first;
  169. }
  170.  
  171. void traverseList(struct Node *p)
  172. {
  173.     while(1)
  174.     {
  175.         if(p)
  176.         {
  177.             int stackElements = stackNumberOfElements();
  178.  
  179.             cout << "p->data: " << p->data << "stack number of elements" << stackElements << endl;
  180.             printStack();
  181.  
  182.         }
  183.         if(p->child && p->next)
  184.         {
  185.             cout << "insert stack p->next " << p->next->data << endl;
  186.             insertStack(p->next);
  187.             traverseList(p->child);
  188.             break;
  189.         }
  190.         if(p->child)
  191.         {
  192.             traverseList(p->child);
  193.             break;
  194.  
  195.         }
  196.         if(p->next)
  197.         {
  198.             traverseList(p->next);
  199.             break;
  200.         }
  201.         if(top == NULL)
  202.             cout << "top == NULL p->data " << p->data << endl;
  203.         if(p->next == NULL && p->child == NULL && top!=NULL)
  204.         {
  205.             p = popStack();
  206.             cout << "p popped --------------------------------------------" << p->data << endl;
  207.             traverseList(p);
  208.             break;
  209.         }
  210.         else
  211.         {
  212.             break;
  213.         }
  214.     }
  215.  
  216. }
  217.  
  218. void insertStack(struct Node *p)
  219. {
  220.     if(top == NULL)
  221.     {
  222.         top = new StackNode;
  223.         top->data = p;
  224.         top->next = NULL;
  225.     }
  226.     else
  227.     {
  228.         StackNode *t = new StackNode;
  229.         t->data = p;
  230.         t->next = top;
  231.         top = t;
  232.     }
  233. }
  234.  
  235. int stackNumberOfElements()
  236. {
  237.     int countStack = 0;
  238.     StackNode *p = top;
  239.     while(p)
  240.     {
  241.         p = p->next;
  242.         countStack++;
  243.     }
  244.     return countStack;
  245. }
  246.  
  247. struct Node* popStack()
  248. {
  249.     struct StackNode *p;
  250.     struct Node* t;
  251.     p = top;
  252.     t = top->data;
  253.     top = top->next;
  254.     delete p;
  255.     return t;
  256.  
  257.  
  258. }
  259.  
  260. void printStack()
  261. {
  262.     struct StackNode *p = top;
  263.     cout << "Printing the stack with elements : " << stackNumberOfElements() << " | ";
  264.     while(p!=NULL)
  265.     {
  266.         cout << p->data->data << " ";
  267.         p = p->next;
  268.     }
  269.     cout << endl;
  270. }
  271.  
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement