64,654
社区成员
发帖
与我相关
我的任务
分享
#include <memory>
#include <iostream>
using namespace std;
struct SmartPoint {
int val;
std::shared_ptr<SmartPoint> next;
std::shared_ptr<SmartPoint> prev;
SmartPoint() :val(0), next(nullptr), prev(nullptr) {}
};
void RightRotate(std::shared_ptr<SmartPoint>& root, std::shared_ptr<SmartPoint>& x) {
std::cout << x->val << std::endl;
std::shared_ptr<SmartPoint> y = x->next;
std::cout << x->val << std::endl;
y->prev = x->prev; //? x值改变了 但是我只是改变指针的值
if (x == nullptr) {
std::cout << "error" << std::endl;
}
}
void test2() {
std::shared_ptr<SmartPoint> root(new SmartPoint);
std::shared_ptr<SmartPoint> x = root;
x->val = 3;
std::shared_ptr<SmartPoint> y(new SmartPoint);
y->val = 2;
x->next = y;
y->prev = x;
std::shared_ptr<SmartPoint> z(new SmartPoint);
z->val = 1;
y->next = z;
z->prev = y;
RightRotate(root, z->prev->prev);
}
int main()
{
test2();
return 0;
}