Advertisement
193030

Kursova izchistena

Dec 13th, 2020
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 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.  
  64. // Connecting the list name[n] with name[n+1]
  65. if (TEMP == NULL)
  66. {
  67. TEMP = addNode();
  68. TEMP->next = NULL;
  69. LAST = TEMP;
  70. }
  71. else
  72. {
  73. TEMP = addNode();
  74. TEMP->next = NULL;
  75. LAST->next = TEMP;
  76. LAST = TEMP;
  77. }
  78. break;
  79. }
  80. case 2:
  81. {
  82. if (firstPersonAdded)
  83. {
  84. traverseList(firstPersonAdded);
  85. }
  86. else
  87. {
  88. cout << "The list is empty." << endl;
  89. }
  90. counterTraverse = 0;
  91. break;
  92. }
  93. case 3:
  94. {
  95. if (firstPersonAdded)
  96. {
  97. traverseListEmployedNew(firstPersonAdded);
  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 whileFlag = 1;
  200. int temp = 0;
  201. if (numberOfPeople > 1)
  202. {
  203. temp = 1;
  204. }
  205. if (counterTraverse == (numberOfPeople * 7) + temp)
  206. {
  207. counterTraverse = 0;
  208. whileFlag = 0;
  209. }
  210. counterTraverse++;
  211.  
  212. while (whileFlag)
  213. {
  214. if (p)
  215. {
  216. cout << " " << p->data;
  217. }
  218. if (p->child && p->next)
  219. {
  220. insertStack(p->next);
  221. traverseList(p->child);
  222. break;
  223. }
  224. if (p->child)
  225. {
  226. traverseList(p->child);
  227. break;
  228. }
  229. if (p->next)
  230. {
  231. traverseList(p->next);
  232. break;
  233. }
  234. if (top == NULL && p->child == NULL && p->next == NULL)
  235. {
  236. break;
  237. }
  238.  
  239. if (p->next == NULL && p->child == NULL && top != NULL)
  240. {
  241. p = popStack();
  242. traverseList(p);
  243. break;
  244. }
  245. else
  246. {
  247. break;
  248. }
  249. }
  250. }
  251.  
  252. void traverseListEmployedNew(struct Node* p)
  253. {
  254. struct Node* address2 = NULL;
  255. struct Node* address3 = NULL;
  256. counterTraverse2++;
  257. if (counterTraverse2 == 8)
  258. {
  259. counterTraverse2 = 1;
  260. if (p->child != nullptr)
  261. {
  262. if (p->child->next)
  263. address2 = p->child->next;
  264. if (p->child->next->next)
  265. address3 = p->child->next->next;
  266. }
  267. if (address2->data.empty() == 1 && address3->data.empty() == 1)
  268. {
  269. flagPrint = 0;
  270. }
  271. else
  272. {
  273. flagPrint = 1;
  274. }
  275. }
  276. while (whileFlag)
  277.  
  278. {
  279. if (p)
  280. {
  281. if (flagPrint)
  282. {
  283. cout << " " << p->data;
  284. }
  285. }
  286. if (p->child && p->next)
  287. {
  288. insertStack(p->next);
  289. traverseListEmployedNew(p->child);
  290. break;
  291. }
  292. if (p->child)
  293. {
  294. traverseListEmployedNew(p->child);
  295. break;
  296. }
  297. if (p->next)
  298. {
  299. traverseListEmployedNew(p->next);
  300. break;
  301. }
  302. if (top == NULL && p->child == NULL && p->next == NULL)
  303. {
  304. break;
  305. }
  306.  
  307. // There are no child and next pointers but stack is not empty.
  308. if (p->next == NULL && p->child == NULL && top != NULL)
  309. {
  310. p = popStack();
  311. traverseListEmployedNew(p);
  312. break;
  313. }
  314. else
  315. {
  316. break;
  317. }
  318. }
  319. }
  320.  
  321.  
  322.  
  323. void insertStack(struct Node* p)
  324. {
  325. if (top == NULL)
  326. {
  327. top = new StackNode;
  328. top->data = p;
  329. top->next = NULL;
  330. }
  331. else
  332. {
  333. StackNode* t = new StackNode;
  334. t->data = p;
  335. t->next = top;
  336. top = t;
  337. }
  338. }
  339.  
  340. int stackNumberOfElements()
  341. {
  342. int countStack = 0;
  343. StackNode* p = top;
  344. while (p)
  345. {
  346. p = p->next;
  347. countStack++;
  348. }
  349. return countStack;
  350. }
  351.  
  352. struct Node* popStack()
  353. {
  354. struct StackNode* p;
  355. struct Node* t;
  356. p = top;
  357. t = top->data;
  358. top = top->next;
  359. delete p;
  360. return t;
  361.  
  362. }
  363.  
  364. void printStack()
  365. {
  366. struct StackNode* p = top;
  367. while (p != NULL)
  368. {
  369. cout << p->data->data << " ";
  370. p = p->next;
  371. }
  372. cout << endl;
  373. }
Advertisement
RAW Paste Data Copied
Advertisement