由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - how to destruct list with loop?
相关主题
请教一个基本的constructor和destrcutor问题两个继承问题
[合集] question about a static pointer in a member function为什么我看不懂下面的code,是不是水平还不够?
c++ copy reference 还是deep copyC++ 弱问一个
这段C++程序有错吗?C++疑问
C++: What is the difference between the two approaches?two c++ interview questions! (转载)
a simple question for C++ class请教一个作用域的问题
请问一个exception题目私有成员不能用类成员函数修改?
C++里面compare double to float
相关话题的讨论汇总
话题: node话题: sll话题: next话题: null话题: todel
进入Programming版参与讨论
1 (共1页)
s*w
发帖数: 729
1
#include
using namespace std;
struct node {
int val;
node* next;
node(int v):val(v),next(NULL) {}
};
struct sll {
node* head;
sll():head(NULL) {}
~sll() {
cout << "sll destructing starts here" << endl;
node* p = head;
while(p) { // i am having problems destrucing here with looped list
node* todel = p;
p = p->next;
delete todel; // i thought delete make todel NULL so it breaks loop
todel = NULL; // does this help? nonono
cout << "destructing " << p << endl;
}
cout << "sll destructed" << endl;
}

void insert(int v) {
node* p = new node(v);
p->next = head;
head = p;
}
};
int main() {
// make a loop list
sll lst;
lst.insert(5);
lst.insert(4);
lst.insert(3);
lst.insert(2);
lst.insert(1);
node *p5 = lst.head;
while(p5->next)
p5 = p5->next;
node *p3 = lst.head->next->next;
p5->next = p3;
}
1 (共1页)
进入Programming版参与讨论
相关主题
compare double to floatC++: What is the difference between the two approaches?
[合集] C++问题(copy constructor)a simple question for C++ class
setjmp() and longjmp()请问一个exception题目
关于c++的constructor的面试题C++里面
请教一个基本的constructor和destrcutor问题两个继承问题
[合集] question about a static pointer in a member function为什么我看不懂下面的code,是不是水平还不够?
c++ copy reference 还是deep copyC++ 弱问一个
这段C++程序有错吗?C++疑问
相关话题的讨论汇总
话题: node话题: sll话题: next话题: null话题: todel