Doubly-linked list/Element definition: Difference between revisions

From Rosetta Code
Content added Content deleted
(Ada 2005 variant added)
Line 4: Line 4:


=={{header|Ada}}==
=={{header|Ada}}==
<ada>
type Link;
type Link_Access is access Link;
type Link;
type Link is record
type Link_Access is access Link;
type Link is record
Next : Link_Access := null;
Prev : Link_Access := null;
Next : Link_Access := null;
Prev : Link_Access := null;
Data : Integer;
end record;
</ada>
In Ada 2005 this example can be written without declaration of an access type:
<ada>
type Link is limited record
Next : not null access Link := Link'Unchecked_Access;
Prev : not null access Link := Link'Unchecked_Access;
Data : Integer;
Data : Integer;
end record;
end record;
</ada>
Here the list element is created already pointing to itself, so that no further initialization is required. The type of the element is marked as ''limited'' indicating that such elements have referential semantics and cannot be copied.


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|ALGOL 68}}==
=={{header|ALGOL 68}}==
MODE LINK = STRUCT (
MODE LINK = STRUCT (

Revision as of 13:59, 18 July 2008

Task
Doubly-linked list/Element definition
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

<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> In Ada 2005 this example can be written without declaration of an access type: <ada> type Link is limited record

  Next : not null access Link := Link'Unchecked_Access;
  Prev : not null access Link := Link'Unchecked_Access;
  Data : Integer;

end record; </ada> Here the list element is created already pointing to itself, so that no further initialization is required. The type of the element is marked as limited indicating that such elements have referential semantics and cannot be copied.

Ada's standard container library includes a generic doubly linked list. The structure of the link element is private.

ALGOL 68

MODE LINK = STRUCT (
  REF LINK next,
  REF LINK prev,
  INT data
);
LINK example;
~

C

struct link (
  struct link *next;
  struct link *prev;
  int data;
);

Fortran

In ISO Fortran 95 or later:

  type node
     real :: data
     type(node), pointer :: next => null(), previous => null()
  end type node
  !
  ! . . . .
  !
  type( node ), target :: head

Java

Works with: Java version 1.5+
public class Node<T> {
   private T element;
   private Node<T> next, prev;

   public Node<T>(){
      next = prev = element = null;
   }

   public Node<T>(Node<T> n, Node<T> p, T elem){
      next = n;
      prev = p;
      element = elem;
   }

   public void setNext(Node<T> n){
      next = n;
   }

   public Node<T> getNext(){
      return next;
   }

   public void setElem(T elem){
      element = elem;
   }

   public T getElem(){
      return element;
   }

   public void setNext(Node<T> n){
      next = n;
   }

   public Node<T> setPrev(Node<T> p){
      prev = p;
   }

   public getPrev(){
      return prev;
   }
}

For use with Java 1.4 and below, delete all "<T>"s and replace T's with "Object".

Pascal

type link_ptr = ^link;
     data_ptr = ^data; (* presumes that type 'data' is defined above *)
     link = record
              prev: link_ptr;
              next: link_ptr;
              data: data_ptr;
            end;


Perl

Just use an array. You can traverse and splice it any way. Linked lists are way too low level.

However, if all you have is an algorithm in another 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;

Python

class Node(object):
    def __init__(self, data = None, prev = None, next = None):
        self.prev = prev
        self.next = next
        self.data = data
    def __str__(self):
        return str(self.data)
    def __repr__(self):
        return repr(self.data)
    def iter_forward(self):
        c = self
        while c != None:
            yield c
            c = c.next
    def iter_backward(self):
        c = self
        while c != None:
            yield c
            c = c.prev

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