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 traverseListEmployed(struct Node* p); // Traversing the list, printing the employed people
- 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 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();
- struct Node* FIRST = addNode();
- break;
- }
- case 2:
- {
- traverseList(firstPersonAdded);
- break;
- }
- case 3:
- {
- traverseListEmployedNew(firstPersonAdded);
- counterEmployed = 0;
- }
- break;
- case 4:
- {
- cout << firstPersonAdded->data << endl;
- cout << firstPersonAdded->next->data << endl;
- }
- 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;
- prevPerson = first;
- Node* adress1Node = new Node;
- adress1Node->data = addresses[0];
- adress1Node->child = NULL;
- Node* adress2Node = new Node;
- adress2Node->data = addresses[1];
- adress1Node->next = adress2Node;
- adress2Node->child = NULL;
- Node* adress3Node = new Node;
- adress3Node->data = addresses[2];
- adress2Node->next = adress3Node;
- adress3Node->child = NULL;
- first->child = adress1Node;
- Node* firstNumberNode = new Node;
- firstNumberNode->data = phoneNumbers[0];
- firstNumberNode->next = NULL;
- firstNumberNode->child = NULL;
- Node* secondNumberNode = new Node;
- secondNumberNode->data = phoneNumbers[1];
- secondNumberNode->next = NULL;
- secondNumberNode->child = NULL;
- firstNumberNode->next = secondNumberNode;
- 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;
- counterTraverse++;
- if (counterTraverse == numberOfPeople * 8)
- {
- counterTraverse = 0;
- whileFlag = 0;
- }
- 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;
- static int counterTraverse = 0;
- int whileFlag = 1;
- int flagPrint = 1;
- counterTraverse++;
- if (counterTraverse == (numberOfPeople * 8)-1)
- {
- counterTraverse = 0;
- cout <<" "<< p->data << endl;
- whileFlag = 0;
- return;
- }
- if (counterTraverse == 1 || counterTraverse % 8 == 0)
- {
- 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;
- }
- }
- 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 traverseListEmployed(struct Node* p)
- {
- int whileLoop = 1;
- if (p != NULL)
- {
- static int flagPrint = 0;
- counterEmployed++;
- if (counterEmployed > 7) // 7 elements in the list
- {
- counterEmployed = 1;
- }
- cout << "counterEmployed i p->data " << counterEmployed << " " << p->data << endl;
- if (counterEmployed == 1)
- {
- struct Node* address2 = p->child->next;
- struct Node* address3 = p->child->next->next;
- cout << "address2 i address3 " <<
- address2->data << " " << address3->data << endl;
- if (address2->data.empty() == 1 && address3->data.empty() == 1)
- {
- cout << "vliza v empty" << endl;
- flagPrint = 0;
- if (p->next == NULL)
- {
- whileLoop = 0;
- cout << "if p->next==NULL" << endl;
- //traverseListEmployed(p->next);
- }
- else
- {
- // cout << "end list" << endl;
- }
- }
- else
- {
- flagPrint = 1;
- }
- }
- int static counterTraverse = 0;
- counterTraverse++;
- if (counterTraverse == numberOfPeople * 8)
- {
- counterTraverse = 0;
- whileLoop = 0;
- }
- while (whileLoop)
- {
- if (p)
- {
- int stackElements = stackNumberOfElements();
- if (flagPrint)
- cout << " " << p->data << endl;//<< "stack number of elements" << stackElements << endl;
- if (!flagPrint)
- cout << "!" << p->data << endl;//<< "stack number of elements" << stackElements << endl;
- // printStack();
- }
- if (p->child && p->next)
- {
- // cout << "insert stack p->next " << p->next->data << endl;
- insertStack(p->next);
- traverseListEmployed(p->child);
- break;
- }
- if (p->child)
- {
- traverseListEmployed(p->child);
- break;
- }
- if (p->next)
- {
- traverseListEmployed(p->next);
- break;
- }
- if (top == NULL)
- {
- //cout << "top == NULL p->data " << p->data << endl;
- }
- if (p->next == NULL && p->child == NULL && top != NULL)
- {
- cout << "print stack before pop: ";
- printStack();
- cout << endl;
- p = popStack();
- // cout << "p popped --------------------------------------------" << p->data << endl;
- // cout << "print stack after pop: ";
- // printStack();
- cout << endl;
- traverseListEmployed(p);
- break;
- }
- if (p->next == NULL && p->child == NULL)
- {
- cout << " p->next == NULL && p->child == NULL " << endl;
- 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;
- }
Add Comment
Please, Sign In to add comment