Advertisement
193030

Kursova mai vsichko raboti

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