Advertisement
193030

Kursova izchistena bez while-a

Dec 13th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  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 traverseFlag = 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. traverseFlag = 0;
  209. }
  210. counterTraverse++;
  211.  
  212. if (traverseFlag)
  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. }
  223. else if (p->child)
  224. {
  225. traverseList(p->child);
  226. }
  227. else if (p->next)
  228. {
  229. traverseList(p->next);
  230. }
  231. else if (top == NULL && p->child == NULL && p->next == NULL)
  232. {
  233. }
  234.  
  235. else if (p->next == NULL && p->child == NULL && top != NULL)
  236. {
  237. p = popStack();
  238. traverseList(p);
  239. }
  240. }
  241. }
  242.  
  243. void traverseListEmployedNew(struct Node* p)
  244. {
  245. struct Node* address2 = NULL;
  246. struct Node* address3 = NULL;
  247. counterTraverse2++;
  248. if (counterTraverse2 == 8)
  249. {
  250. counterTraverse2 = 1;
  251. if (p->child != nullptr)
  252. {
  253. if (p->child->next)
  254. address2 = p->child->next;
  255. if (p->child->next->next)
  256. address3 = p->child->next->next;
  257. }
  258. if (address2->data.empty() == 1 && address3->data.empty() == 1)
  259. {
  260. flagPrint = 0;
  261. }
  262. else
  263. {
  264. flagPrint = 1;
  265. }
  266. }
  267.  
  268. if (p)
  269. {
  270. if (flagPrint)
  271. {
  272. cout << " " << p->data;
  273. }
  274. }
  275. if (p->child && p->next)
  276. {
  277. insertStack(p->next);
  278. traverseListEmployedNew(p->child);
  279. }
  280. else if (p->child)
  281. {
  282. traverseListEmployedNew(p->child);
  283. }
  284. else if (p->next)
  285. {
  286. traverseListEmployedNew(p->next);
  287. }
  288. else if (top == NULL && p->child == NULL && p->next == NULL)
  289. {
  290. return;
  291. }
  292.  
  293. // There are no child and next pointers but stack is not empty.
  294. else if (p->next == NULL && p->child == NULL && top != NULL)
  295. {
  296. p = popStack();
  297. traverseListEmployedNew(p);
  298. }
  299. }
  300.  
  301.  
  302.  
  303. void insertStack(struct Node* p)
  304. {
  305. if (top == NULL)
  306. {
  307. top = new StackNode;
  308. top->data = p;
  309. top->next = NULL;
  310. }
  311. else
  312. {
  313. StackNode* t = new StackNode;
  314. t->data = p;
  315. t->next = top;
  316. top = t;
  317. }
  318. }
  319.  
  320. int stackNumberOfElements()
  321. {
  322. int countStack = 0;
  323. StackNode* p = top;
  324. while (p)
  325. {
  326. p = p->next;
  327. countStack++;
  328. }
  329. return countStack;
  330. }
  331.  
  332. struct Node* popStack()
  333. {
  334. struct StackNode* p;
  335. struct Node* t;
  336. p = top;
  337. t = top->data;
  338. top = top->next;
  339. delete p;
  340. return t;
  341.  
  342. }
  343.  
  344. void printStack()
  345. {
  346. struct StackNode* p = top;
  347. while (p != NULL)
  348. {
  349. cout << p->data->data << " ";
  350. p = p->next;
  351. }
  352. cout << endl;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement