Doubly-linked list/Element definition: Difference between revisions
Content added Content deleted
mNo edit summary |
m (Changed over to headers.) |
||
Line 3: | Line 3: | ||
Define the data structure for a [[Linked List|doubly-linked list]] element. The element should include a data member to hold its value and pointers to both the next element in the list and the previous element in the list. The pointers should be mutable. |
Define the data structure for a [[Linked List|doubly-linked list]] element. The element should include a data member to hold its value and pointers to both the next element in the list and the previous element in the list. The pointers should be mutable. |
||
== |
=={{header|Ada}}== |
||
[[Category:Ada]] |
|||
type Link; |
type Link; |
||
type Link_Access is access Link; |
type Link_Access is access Link; |
||
Line 15: | Line 14: | ||
Ada's standard container library includes a generic doubly linked list. The structure of the link element is private. |
Ada's standard container library includes a generic doubly linked list. The structure of the link element is private. |
||
== |
=={{header|C}}== |
||
[[Category:C]] |
|||
struct link { |
struct link { |
||
struct * link next; |
struct * link next; |
||
Line 23: | Line 21: | ||
}; |
}; |
||
== |
=={{header|Perl}}== |
||
[[Category:Perl]] |
|||
Just use an array. You can traverse and splice it any way. Linked lists are way too low level. |
Just use an array. You can traverse and splice it any way. Linked lists are way too low level. |
||
Line 36: | Line 33: | ||
$node{next} = \%quux_node; # mutable |
$node{next} = \%quux_node; # mutable |
||
== |
=={{header|Pop11}}== |
||
[[Category:Pop11]] |
|||
uses objectclass; |
uses objectclass; |
||
Line 46: | Line 42: | ||
enddefine; |
enddefine; |
||
== |
=={{header|Ruby}}== |
||
[[Category:Ruby]] |
|||
class ListNode |
class ListNode |
Revision as of 20:03, 12 November 2007
Doubly-linked list/Element definition
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Define the data structure for a doubly-linked list element. The element should include a data member to hold its value and pointers to both the next element in the list and the previous element in the list. The pointers should be mutable.
Ada
type Link; type Link_Access is access Link; type Link is record Next : Link_Access := null; Prev : Link_Access := null; Data : Integer; end record;
Ada's standard container library includes a generic doubly linked list. The structure of the link element is private.
C
struct link { struct * link next; struct * link prev; int data; };
Perl
Just use an array. You can traverse and splice it any way. Linked lists are way too low level.
However, if all you got is a algorithm in a foreign language, you can use references to accomplish the translation.
my %node = ( data => 'say what', next => \%foo_node, prev => \%bar_node, ); $node{next} = \%quux_node; # mutable
Pop11
uses objectclass; define :class Link; slot next = []; slot prev = []; slot data = []; enddefine;
Ruby
class ListNode attr_accessor :val, :nxt, :prv def initialize(mval,mprv=nil,mnxt=nil) self.val=mval self.prv=mprv prv.nxt=self if prv self.nxt=mnxt nxt.prv=self if nxt end def each(&b) yield val nxt.each(&b) if nxt self end include Enumerable end