我想實現一個鏈表的對象,所以我有一個頭文件,像這樣一個節點類:消滅在Objective-C
@interface Node : NSObject
@property(nonatomic,assign)int data;
@property(nonatomic,strong) Node *right;
@property(nonatomic,strong) Node *left;
@end
然後在另一個班,我分配他們然後調用一個方法來破壞一個給定值的所有出現:
Node *node0 = [[Node alloc]init];
Node *node1 = [[Node alloc]init];
Node *node2 = [[Node alloc]init];
Node *node3 = [[Node alloc]init];
Node *node4 = [[Node alloc]init];
node0.data = 1;
node1.data = 2;
node2.data = 5;
node3.data = 5;
node4.data = 3;
node0.right = node1;
node1.right = node2;
node2.right = node3;
node3.right = node4;
node4.right = NULL;
[self removeNodeWithValue:node0 value:5];
NSLog(@"node %d, %d, %d, %d, %d", node0.data, node1.data, node2.data, node3.data, node4.data);
和這裏的方法本身:
-(void)removeNodeWithValue:(Node *)head value:(int)value
{
Node *toDelete;
while (head != NULL) {
if (head.data == value)
{
toDelete = head;
head = head.right;
toDelete = nil;
}
else
{
head = head.right;
}
}
}
==> 1, 2, 5, 5, 3
我知道我可以更改實例,因爲如果我將toDelete = nil
更改爲toDelete.data = 4
,則輸出爲==> 1, 2, 4, 4, 3
。我的問題是,我如何銷燬這些實例?謝謝。
看到這個:http://stackoverflow.com/questions/14508134/whats-the-correct-way-destroy-different-kinds-of-objects-in-objective-c?rq=1 –
他們建議設置對象爲零,但在我的代碼中,你可以看到這不起作用。 –