Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include<cstdlib>
- #include<iostream>
- using namespace std;
- //this is not multi list
- struct User {
- string name = "Default";
- string phone = "";
- User * next_user = NULL;
- private:
- int num_of_cup = 4;
- public:
- User (string new_name, string new_phone) {
- name = new_name;
- phone = new_phone;
- }
- User () {
- num_of_cup = 6;
- }
- int get_num_of_cups() {
- return num_of_cup;
- }
- User* add(string new_name, string new_phone){
- if (name == new_name) {
- return NULL;
- }
- else {
- if (next_user == NULL){
- User new_user = User(new_name, new_phone);//crete new element
- next_user = &new_user;
- return next_user;
- }
- User* res = (*next_user).add(new_name, new_phone);
- return res;
- }
- }
- bool remove(string name) {
- if (next_user == NULL) {
- return false;
- }
- if ((*next_user).name == name) {
- next_user = next_user->next_user;//remove element
- return true;
- }
- return next_user->remove(name);
- }
- int count() {
- if (next_user == NULL)
- return 1;
- cout << "HERE1\n";
- int res = next_user->count();
- cout << "HERE2\n";
- return res++;
- }
- };
- struct List {
- private:
- User * head = NULL;
- public:
- List () {
- }
- User * add(string new_name, string new_phone) {
- if (head == NULL) {
- User new_user = User(new_name, new_phone);//crete new element
- head = &new_user;
- }
- else {
- head->add(new_name, new_phone);
- }
- }
- bool remove(string name) {
- if (head == NULL) {
- return false;
- }
- if (head->name == name) {
- head = head->next_user; //remove element
- return true;
- }
- return head->next_user->remove(name);
- }
- int count() {
- if (head == NULL)
- return 0;
- return head->count();
- }
- };
- User noName;
- void f(int b) {
- b++;
- }
- void incorrect_change_Name(User user, string new_name) {
- user.name = new_name;
- }
- void change_Name(User * ptr_user, string new_name) {
- (*ptr_user).name = new_name; //синтаксический сахар ptr_user->name = new_name;
- }
- int main() {
- int a = 4;
- f(a);
- cout << a << "\n";
- User Masha("Masha", "777");
- cout << Masha.name << "\n";
- change_Name(&Masha, "Dima");
- cout << Masha.name << "\n";
- cout << Masha.get_num_of_cups() << " " << noName.get_num_of_cups();
- //cout << Masha.num_of_cup; - doesn't let to use out of structure
- Masha.next_user = &noName;
- if (Masha.next_user != NULL) {
- cout << "Name of next user is: " << Masha.next_user->name;
- }
- cout << "---\n";
- List test;
- cout << test.count() << "\n";
- test.add("Vasya", "666");
- cout << test.count() << "\n";
- test.add("Masha", "777");
- cout << test.count() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement