Advertisement
193030

Kursova raboti donqkude

Dec 13th, 2020
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 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 traverseListEmployed(struct Node *p); // Traversing the list, printing the employed people
  30. void traverseListEmployedNew(struct Node *p); // Traversing the list, printing the employed people new version
  31. void printStack();
  32. int stackNumberOfElements();
  33.  
  34. struct Node * lastPersonAdded;
  35. struct Node *firstPersonAdded = NULL;
  36.  
  37. int numberOfPeople = 0;
  38. static int counterEmployed = 0;
  39. int whileFlag = 1;
  40. int flagPrint = 1;
  41.  
  42. struct Node *TEMP = NULL;
  43. struct Node *LAST = NULL;
  44. int main()
  45. {
  46. int loop = 1;
  47. while (loop)
  48. {
  49. cout << "\nMenu: " << endl;
  50. cout << "1. Press 1 to add person" << endl;
  51. cout << "2. Press 2 to show the whole list" << endl;
  52. cout << "3. Press 3 to show the list of employed people" << endl;
  53. cout << "5. Press 0 to exit" << endl;
  54. int inputCommand;
  55. cin >> inputCommand;
  56. switch (inputCommand)
  57. {
  58. case 1:
  59. {
  60. enterPerson();
  61. if (TEMP == NULL)
  62. {
  63. TEMP = addNode();
  64. TEMP->next = NULL;
  65. LAST = TEMP;
  66. }
  67. else
  68. {
  69. cout << " merge last data: " << LAST->data << endl;
  70. TEMP = addNode();
  71. TEMP->next = NULL;
  72. LAST->next = TEMP;
  73. LAST = TEMP;
  74. }
  75. break;
  76. }
  77. case 2:
  78. {
  79. traverseList(firstPersonAdded);
  80. break;
  81. }
  82. case 3:
  83. {
  84. traverseListEmployedNew(firstPersonAdded);
  85. counterEmployed = 0;
  86. }
  87. break;
  88. case 4:
  89. {
  90. cout << "The last is: " << LAST->data << endl;
  91. }
  92. break;
  93.  
  94. case 0:
  95. loop = 0;
  96. break;
  97. default:
  98.  
  99. break;
  100. }
  101. }
  102. }
  103.  
  104. void enterPerson()
  105. {
  106. cin.ignore();
  107. cout << "Enter first name: " << endl;
  108. cin >> names;
  109. cout << "Enter first address: " << endl;
  110. cin >> addresses[0];
  111. cout << "Enter second address: " << endl;
  112. cin.ignore();
  113. getline(cin, addresses[1]);
  114. cout << "Enter third address: " << endl;
  115. getline(cin, addresses[2]);
  116. cout << "Enter first phone number: " << endl;
  117. cin >> phoneNumbers[0];
  118. cout << "Enter second phone number: " << endl;
  119. cin >> phoneNumbers[1];
  120. cout << "Enter third phone number: " << endl;
  121. cin >> phoneNumbers[2];
  122.  
  123. }
  124.  
  125. struct Node* addNode()
  126. {
  127. struct Node * first;
  128. first = new Node;
  129. first->data = names;
  130. first->next = NULL;
  131. prevPerson = first;
  132.  
  133. struct Node *adress1Node = new Node;
  134. adress1Node->data = addresses[0];
  135. adress1Node->child = NULL;
  136.  
  137. struct Node *adress2Node = new Node;
  138. adress2Node->data = addresses[1];
  139. adress1Node->next = adress2Node;
  140. adress2Node->child = NULL;
  141.  
  142. struct Node *adress3Node = new Node;
  143. adress3Node->data = addresses[2];
  144. adress2Node->next = adress3Node;
  145. adress3Node->child = NULL;
  146.  
  147. first->child = adress1Node;
  148.  
  149. struct Node *firstNumberNode = new Node;
  150. firstNumberNode->data = phoneNumbers[0];
  151. firstNumberNode->next = NULL;
  152. firstNumberNode->child = NULL;
  153.  
  154. struct Node *secondNumberNode = new Node;
  155. secondNumberNode->data = phoneNumbers[1];
  156. secondNumberNode->next = NULL;
  157. secondNumberNode->child = NULL;
  158. firstNumberNode->next = secondNumberNode;
  159.  
  160. struct Node *thirdNumberNode = new Node;
  161. thirdNumberNode->data = phoneNumbers[2];
  162. thirdNumberNode->next = NULL;
  163. thirdNumberNode->child = NULL;
  164.  
  165. adress1Node->child = firstNumberNode;
  166. adress3Node->child = thirdNumberNode;
  167.  
  168. if (numberOfPeople > 0)
  169. {
  170. lastPersonAdded->next = first;
  171. lastPersonAdded = first;
  172. }
  173. else
  174. {
  175. firstPersonAdded = first;
  176. lastPersonAdded = first;
  177. }
  178. numberOfPeople++;
  179. return first;
  180. }
  181.  
  182. void traverseList(struct Node *p)
  183. {
  184. cout <<" TL ";
  185. int static counterTraverse = 0;
  186. int whileFlag = 1;
  187. int temp = 0;
  188. if(numberOfPeople>1)
  189. {
  190. temp = 1;
  191. }
  192. if (counterTraverse == (numberOfPeople *7)+temp)
  193. {
  194. counterTraverse = 0;
  195. whileFlag = 0;
  196. }
  197. counterTraverse++;
  198.  
  199. while (whileFlag)
  200. {
  201. if (p)
  202. {
  203. // int stackElements = stackNumberOfElements();
  204.  
  205. cout << " " << p->data; //<< "stack number of elements" << stackElements << endl;
  206. // printStack();
  207. }
  208. if (p->child && p->next)
  209. {
  210. // cout << "insert stack p->next " << p->next->data << endl;
  211. insertStack(p->next);
  212. traverseList(p->child);
  213. break;
  214. }
  215. if (p->child)
  216. {
  217. traverseList(p->child);
  218. break;
  219. }
  220. if (p->next)
  221. {
  222. traverseList(p->next);
  223. break;
  224. }
  225. if (top == NULL && p->child == NULL && p->next == NULL)
  226. {
  227. //cout << "top == NULL p->data " << p->data << endl;
  228. break;
  229. }
  230.  
  231. if (p->next == NULL && p->child == NULL && top != NULL)
  232. {
  233. p = popStack();
  234. // cout << "p popped --------------------------------------------" << p->data << endl;
  235. traverseList(p);
  236. break;
  237. }
  238. else
  239. {
  240. break;
  241. }
  242. }
  243. }
  244.  
  245. void traverseListEmployedNew(struct Node *p)
  246. {
  247. struct Node *address2 = NULL;
  248. struct Node *address3 = NULL;
  249. static int counterTraverse = 0;
  250. counterTraverse++;
  251. if (counterTraverse == 8)
  252. {
  253. counterTraverse = 1;
  254. if (p->child != nullptr)
  255. {
  256. if (p->child->next)
  257. address2 = p->child->next;
  258. if (p->child->next->next)
  259. address3 = p->child->next->next;
  260. }
  261. if (address2->data.empty() == 1 && address3->data.empty() == 1)
  262. {
  263. flagPrint = 0;
  264. }
  265. else
  266. {
  267. flagPrint = 1;
  268. }
  269. }
  270. while (whileFlag)
  271.  
  272. {
  273. if (p)
  274. {
  275. // int stackElements = stackNumberOfElements();
  276. if (flagPrint)
  277. cout << " " << p->data; //<< "stack number of elements" << stackElements << endl;
  278.  
  279. // printStack();
  280. }
  281. if (p->child && p->next)
  282. {
  283. // cout << "insert stack p->next " << p->next->data << endl;
  284. insertStack(p->next);
  285. traverseListEmployedNew(p->child);
  286. break;
  287. }
  288. if (p->child)
  289. {
  290. traverseListEmployedNew(p->child);
  291. break;
  292. }
  293. if (p->next)
  294. {
  295. traverseListEmployedNew(p->next);
  296. break;
  297. }
  298. if (top == NULL && p->child == NULL && p->next == NULL)
  299. {
  300. //cout << "top == NULL p->data " << p->data << endl;
  301. break;
  302. }
  303.  
  304. if (p->next == NULL && p->child == NULL && top != NULL)
  305. {
  306.  
  307. p = popStack();
  308. // cout << "p popped --------------------------------------------" << p->data << endl;
  309. traverseListEmployedNew(p);
  310.  
  311. break;
  312. }
  313. else
  314. {
  315. break;
  316. }
  317. }
  318. }
  319.  
  320. void traverseListEmployed(struct Node *p)
  321. {
  322.  
  323. int whileLoop = 1;
  324. if (p != NULL)
  325. {
  326. static int flagPrint = 0;
  327. counterEmployed++;
  328. if (counterEmployed > 7) // 7 elements in the list
  329. {
  330. counterEmployed = 1;
  331. }
  332. cout << "counterEmployed i p->data " << counterEmployed << " " << p->data << endl;
  333.  
  334. if (counterEmployed == 1)
  335. {
  336. struct Node *address2 = p->child->next;
  337. struct Node *address3 = p->child->next->next;
  338. cout << "address2 i address3 " <<
  339. address2->data << " " << address3->data << endl;
  340. if (address2->data.empty() == 1 && address3->data.empty() == 1)
  341. {
  342.  
  343. cout << "vliza v empty" << endl;
  344.  
  345. flagPrint = 0;
  346. if (p->next == NULL)
  347. {
  348. whileLoop = 0;
  349. cout << "if p->next==NULL" << endl;
  350. //traverseListEmployed(p->next);
  351. }
  352. else
  353. {
  354. // cout << "end list" << endl;
  355. }
  356. }
  357. else
  358. {
  359. flagPrint = 1;
  360. }
  361. }
  362.  
  363. int static counterTraverse = 0;
  364. counterTraverse++;
  365. if (counterTraverse == numberOfPeople *8)
  366. {
  367. counterTraverse = 0;
  368. whileLoop = 0;
  369. }
  370. while (whileLoop)
  371. {
  372. if (p)
  373. {
  374. int stackElements = stackNumberOfElements();
  375. if (flagPrint)
  376. cout << " " << p->data << endl; //<< "stack number of elements" << stackElements << endl;
  377. if (!flagPrint)
  378. cout << "!" << p->data << endl; //<< "stack number of elements" << stackElements << endl;
  379.  
  380. // printStack();
  381. }
  382. if (p->child && p->next)
  383. {
  384. // cout << "insert stack p->next " << p->next->data << endl;
  385. insertStack(p->next);
  386. traverseListEmployed(p->child);
  387. break;
  388. }
  389. if (p->child)
  390. {
  391. traverseListEmployed(p->child);
  392. break;
  393. }
  394. if (p->next)
  395. {
  396. traverseListEmployed(p->next);
  397. break;
  398. }
  399. if (top == NULL)
  400. {
  401. //cout << "top == NULL p->data " << p->data << endl;
  402. }
  403.  
  404. if (p->next == NULL && p->child == NULL && top != NULL)
  405. {
  406. cout << "print stack before pop: ";
  407. printStack();
  408. cout << endl;
  409. p = popStack();
  410. // cout << "p popped --------------------------------------------" << p->data << endl;
  411. // cout << "print stack after pop: ";
  412. // printStack();
  413. cout << endl;
  414. traverseListEmployed(p);
  415. break;
  416. }
  417. if (p->next == NULL && p->child == NULL)
  418. {
  419. cout << " p->next == NULL && p->child == NULL " << endl;
  420. break;
  421. }
  422. else
  423. {
  424. break;
  425. }
  426. }
  427. }
  428. }
  429.  
  430. void insertStack(struct Node *p)
  431. {
  432. if (top == NULL)
  433. {
  434. top = new StackNode;
  435. top->data = p;
  436. top->next = NULL;
  437. }
  438. else
  439. {
  440. StackNode *t = new StackNode;
  441. t->data = p;
  442. t->next = top;
  443. top = t;
  444. }
  445. }
  446.  
  447. int stackNumberOfElements()
  448. {
  449. int countStack = 0;
  450. StackNode *p = top;
  451. while (p)
  452. {
  453. p = p->next;
  454. countStack++;
  455. }
  456. return countStack;
  457. }
  458.  
  459. struct Node* popStack()
  460. {
  461. struct StackNode * p;
  462. struct Node * t;
  463. p = top;
  464. t = top->data;
  465. top = top->next;
  466. delete p;
  467. return t;
  468.  
  469. }
  470.  
  471. void printStack()
  472. {
  473. struct StackNode *p = top;
  474. // cout << "Printing the stack with elements : " << stackNumberOfElements() << " | ";
  475. while (p != NULL)
  476. {
  477. cout << p->data->data << " ";
  478. p = p->next;
  479. }
  480. cout << endl;
  481. }
  482.  
Advertisement
RAW Paste Data Copied
Advertisement