Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. //this is not multi list
  8. struct User {
  9. string name = "Default";
  10. string phone = "";
  11. User * next_user = NULL;
  12. private:
  13. int num_of_cup = 4;
  14.  
  15. public:
  16. User (string new_name, string new_phone) {
  17. name = new_name;
  18. phone = new_phone;
  19. }
  20.  
  21. User () {
  22. num_of_cup = 6;
  23. }
  24.  
  25. int get_num_of_cups() {
  26. return num_of_cup;
  27. }
  28.  
  29. User* add(string new_name, string new_phone){
  30. if (name == new_name) {
  31. return NULL;
  32. }
  33. else {
  34. if (next_user == NULL){
  35. User new_user = User(new_name, new_phone);//crete new element
  36. next_user = &new_user;
  37. return next_user;
  38. }
  39. User* res = (*next_user).add(new_name, new_phone);
  40. return res;
  41. }
  42. }
  43.  
  44. bool remove(string name) {
  45. if (next_user == NULL) {
  46. return false;
  47. }
  48. if ((*next_user).name == name) {
  49. next_user = next_user->next_user;//remove element
  50. return true;
  51. }
  52. return next_user->remove(name);
  53. }
  54.  
  55. int count() {
  56. if (next_user == NULL)
  57. return 1;
  58. cout << "HERE1\n";
  59. int res = next_user->count();
  60. cout << "HERE2\n";
  61. return res++;
  62. }
  63. };
  64.  
  65. struct List {
  66. private:
  67. User * head = NULL;
  68.  
  69. public:
  70. List () {
  71. }
  72.  
  73. User * add(string new_name, string new_phone) {
  74. if (head == NULL) {
  75. User new_user = User(new_name, new_phone);//crete new element
  76. head = &new_user;
  77. }
  78. else {
  79. head->add(new_name, new_phone);
  80. }
  81. }
  82.  
  83. bool remove(string name) {
  84. if (head == NULL) {
  85. return false;
  86. }
  87. if (head->name == name) {
  88. head = head->next_user; //remove element
  89. return true;
  90. }
  91. return head->next_user->remove(name);
  92. }
  93.  
  94. int count() {
  95. if (head == NULL)
  96. return 0;
  97. return head->count();
  98. }
  99. };
  100.  
  101.  
  102. User noName;
  103.  
  104. void f(int b) {
  105. b++;
  106. }
  107.  
  108. void incorrect_change_Name(User user, string new_name) {
  109. user.name = new_name;
  110. }
  111.  
  112. void change_Name(User * ptr_user, string new_name) {
  113. (*ptr_user).name = new_name; //синтаксический сахар ptr_user->name = new_name;
  114. }
  115.  
  116.  
  117.  
  118. int main() {
  119. int a = 4;
  120. f(a);
  121. cout << a << "\n";
  122. User Masha("Masha", "777");
  123. cout << Masha.name << "\n";
  124. change_Name(&Masha, "Dima");
  125. cout << Masha.name << "\n";
  126. cout << Masha.get_num_of_cups() << " " << noName.get_num_of_cups();
  127. //cout << Masha.num_of_cup; - doesn't let to use out of structure
  128. Masha.next_user = &noName;
  129. if (Masha.next_user != NULL) {
  130. cout << "Name of next user is: " << Masha.next_user->name;
  131. }
  132. cout << "---\n";
  133. List test;
  134. cout << test.count() << "\n";
  135. test.add("Vasya", "666");
  136. cout << test.count() << "\n";
  137. test.add("Masha", "777");
  138. cout << test.count() << "\n";
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement