Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Raboti fixnata e.
- #include <iostream>
- #include <string>
- using namespace std;
- string names;
- string addresses[3];
- string phoneNumbers[3];
- struct Node
- {
- string data;
- Node * next;
- Node * child;
- }*prevPerson = NULL;
- struct StackNode
- {
- struct Node * data;
- struct StackNode * next;
- }*top = 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 insertStack(struct Node *n); // Inserting an element in the stack
- struct Node* popStack(); // Poping element from stack
- void traverseList(struct Node *FIRST); // Traversing the list
- void traverseListEmployedNew(struct Node *p); // Traversing the list, printing the employed people new version
- void printStack();
- int stackNumberOfElements();
- struct Node * lastPersonAdded;
- struct Node *firstPersonAdded = NULL;
- int numberOfPeople = 0;
- static int counterEmployed = 0;
- int whileFlag = 1;
- int flagPrint = 1;
- int static counterTraverse = 0;
- int static counterTraverse2 = 0;
- struct Node *TEMP = NULL;
- struct Node *LAST = NULL;
- int main()
- {
- int loop = 1;
- while (loop)
- {
- 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();
- 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)
- {
- traverseList(firstPersonAdded);
- }
- else
- {
- cout << "The list is empty." << endl;
- }
- counterTraverse = 0;
- break;
- }
- case 3:
- {
- if(firstPersonAdded)
- {
- traverseListEmployedNew(firstPersonAdded);
- }
- else
- {
- cout << "The list is empty. " << endl;
- }
- counterEmployed = 0;
- counterTraverse2 = 0;
- }
- break;
- case 0:
- loop = 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()
- {
- 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 static counterTraverse = 0;
- int whileFlag = 1;
- int temp = 0;
- if(numberOfPeople>1)
- {
- temp = 1;
- }
- if (counterTraverse == (numberOfPeople *7)+temp)
- {
- counterTraverse = 0;
- whileFlag = 0;
- }
- counterTraverse++;
- while (whileFlag)
- {
- if (p)
- {
- // int stackElements = stackNumberOfElements();
- cout << " " << p->data; //<< "stack number of elements" << stackElements << endl;
- // printStack();
- }
- if (p->child && p->next)
- {
- // cout << "insert stack p->next " << p->next->data << endl;
- insertStack(p->next);
- traverseList(p->child);
- break;
- }
- if (p->child)
- {
- traverseList(p->child);
- break;
- }
- if (p->next)
- {
- traverseList(p->next);
- break;
- }
- if (top == NULL && p->child == NULL && p->next == NULL)
- {
- //cout << "top == NULL p->data " << p->data << endl;
- break;
- }
- if (p->next == NULL && p->child == NULL && top != NULL)
- {
- p = popStack();
- // cout << "p popped --------------------------------------------" << p->data << endl;
- traverseList(p);
- break;
- }
- else
- {
- break;
- }
- }
- }
- void traverseListEmployedNew(struct Node *p)
- {
- struct Node *address2 = NULL;
- struct Node *address3 = NULL;
- counterTraverse2++;
- if (counterTraverse2 == 8)
- {
- counterTraverse2 = 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->data.empty() == 1 && address3->data.empty() == 1)
- {
- flagPrint = 0;
- }
- else
- {
- flagPrint = 1;
- }
- }
- while (whileFlag)
- {
- if (p)
- {
- // int stackElements = stackNumberOfElements();
- if (flagPrint)
- cout << " " << p->data; //<< "stack number of elements" << stackElements << endl;
- // printStack();
- }
- if (p->child && p->next)
- {
- // cout << "insert stack p->next " << p->next->data << endl;
- insertStack(p->next);
- traverseListEmployedNew(p->child);
- break;
- }
- if (p->child)
- {
- traverseListEmployedNew(p->child);
- break;
- }
- if (p->next)
- {
- traverseListEmployedNew(p->next);
- break;
- }
- if (top == NULL && p->child == NULL && p->next == NULL)
- {
- //cout << "top == NULL p->data " << p->data << endl;
- break;
- }
- if (p->next == NULL && p->child == NULL && top != NULL)
- {
- p = popStack();
- // cout << "p popped --------------------------------------------" << p->data << endl;
- traverseListEmployedNew(p);
- break;
- }
- else
- {
- break;
- }
- }
- }
- 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;
- // cout << "Printing the stack with elements : " << stackNumberOfElements() << " | ";
- while (p != NULL)
- {
- cout << p->data->data << " ";
- p = p->next;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement