Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedList in swift with node as structure

Can anyone tell me how to create a linked-list in swift with node as structure and linked-list as class.

I tried to create a node Node with reference to itself and faced a error "value type 'Node' cannot have a stored property that references itself"

for resolving it i created one more class Next and now I am unable to proceed further.

like image 846
Mohan Patil Avatar asked Feb 06 '18 16:02

Mohan Patil


1 Answers

You can not make node as a "struct" since Swift treats Struct and Enums as value types and Classes as reference type.

When you make the LinkedList Class you would very likely create a reference to your node structure. Now struct being a value type, capturing self for value types is dangerous practice and discouraged.

Therefore I would suggest you to create Node as Class and make use of the various access specifiers in Swift to achieve the abstraction you are trying to.

For more details on capturing self in value types you can refer to https://github.com/Wolox/ios-style-guide/blob/master/rules/avoid-struct-closure-self.md.

like image 98
Ultimate_93 Avatar answered Nov 07 '22 03:11

Ultimate_93