一系列自测题目

#include
#include
#include
#include
using namespace std;
class A
{
private:
A()
{
cout << “create A” << endl;
}
int a1 = 100;
static A a;
static std::mutex mtx;
public:
static A
getA()
{
if (a == nullptr)
{
std::unique_lockstd::mutex lock(mtx);
if (a == nullptr)
{
a = new A();
}
lock.unlock();
}
return a;
}
/*virtual have virtual wrong */ static void display1()// const have const wrong
{

}
friend void hello(A a);

};
A* A::a = nullptr;
std::mutex A::mtx;
void display()
{
A* a = A::getA();
}
void hello(A a)
{
cout << a.a1 << endl;
}
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int val, TreeNode *left = nullptr, TreeNode *right = nullptr)
{
this->val = val;
this->left = left;
this->right = right;
}
TreeNode()
{
this->left = nullptr;
this->right = nullptr;
}
};
struct LinkNode
{
int val;
LinkNode *next;
LinkNode(int val, LinkNode *next = nullptr)
{
this->val = val;
this->next = next;
}
LinkNode()
{

}

};
class Test
{
public:
Test()
{
cout << “this is a test” << endl;
}
Test(const Test &t)
{
cout << “this is a copy test” << endl;
}
Test & operator=(Test &t)
{
if (this == &t)
{
return *this;
}
cout << “this is = test” << endl;
return t;

}

};
bool isSubTree(TreeNode *root1, TreeNode *root2)
{
if (root2 == nullptr)
{
return true;
}
if (root1 == nullptr)
{
return false;
}
if (root1->val == root2->val)
{
return isSubTree(root1->left, root2->left) && isSubTree(root1->right, root2->right);
}
return false;
}
bool hashTree(TreeNode *root1, TreeNode *root2)
{
if (root1 == nullptr || root2 == nullptr)
{
return false;
}
return isSubTree(root1, root2) || hashTree(root1->left, root2) || hashTree(root1->right, root2);
}
TreeNode *create(vector vec, int i)
{
TreeNode *node = nullptr;
if (i >= vec.size())
{
return node;
}
node = new TreeNode(vec[i]);
node->left = create(vec, i * 2 + 1);
node->right = create(vec, i * 2 + 2);
return node;
}
LinkNode *createLinkNode(vector vec,int i)
{
LinkNode *node = nullptr;
if (i >= vec.size())
{
return node;
}
node = new LinkNode(vec[i]);
node->next = createLinkNode(vec, i + 1);
return node;
}
void my_print(LinkNode *node)
{
while (node)
{
cout << node->val << " ";
node = node->next;
}
cout << endl;
}
LinkNode *reverse(LinkNode *node)
{
if (node != nullptr && node->next != nullptr)
{
LinkNode *temp = node->next->next;
LinkNode *one = node;
LinkNode *two = node->next;
two->next = one;
one->next = reverse(temp);
return two;
}
return node;
}
int main()
{
/*single ton */
std::thread th[10];
for (int i = 0; i < 10; i++)
{
th[i] = std::thread(display);
}
for (int i = 0; i < 10; i++)
{
th[i].join();
}
/friend function/
A a1=A::getA();
hello(a1);
/
a is b child tree
/
vector vec{ 1,2,3,4,5,6 };
TreeNode *root = create(vec, 0);

vector<int> vec1{ 1,2,3 };
TreeNode *root1 = create(vec1, 0);
cout << hashTree(root, root1) << endl;
/*slilge list reverse*/
vector<int> vec3{ 1,2,3,4,5,6,7,8 };
LinkNode *head1 = createLinkNode(vec3, 0);
my_print(head1);
LinkNode *head2 = reverse(head1);
my_print(head2);
/*copy and =*/
Test t,t1;
t = t1;/*t and t1 is exist =*/
Test ab = t;/*ab is not exist copy*/
system("pause");
return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值