Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Raboti do nqkyde. next ne raboti
- #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 printStack();
- int stackNumberOfElements();
- struct Node* lastPersonAdded;
- struct Node* firstPersonAdded = NULL;
- int numberOfPeople = 0;
- int main()
- {
- int loop =1;
- while(loop)
- {
- cout << "Menu: " << endl;
- cout << "1. Press 1 to add person" << endl;
- cout << "2. Press 2 to show the list" << endl;
- cout << "3. Press 3 to show unemployed people" << endl;
- cout << "4. Press 4 to delete worker by index from the list" << 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:
- {
- cout << lastPersonAdded->data << endl;
- }
- 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)
- {
- while(1)
- {
- if(p)
- {
- int stackElements = stackNumberOfElements();
- cout << "p->data: " << 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)
- cout << "top == NULL p->data " << p->data << endl;
- if(p->next == NULL && p->child == NULL && top!=NULL)
- {
- p = popStack();
- cout << "p popped --------------------------------------------" << p->data << endl;
- traverseList(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