Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Jan Brosius <jan.brosius@village.uunet.be>, caml-list@inria.fr
Subject: Re: simulation of doubly linked lists in OCaml?
Date: Fri, 28 Apr 2000 13:55:43 +0200	[thread overview]
Message-ID: <20000428135543.40826@pauillac.inria.fr> (raw)
In-Reply-To: <002601bfb050$b47b8790$f902bed4@jannt>; from Jan Brosius on Thu, Apr 27, 2000 at 03:58:01PM +0200

> I wonder if it is possible to simulate by datatype constructors a
> doubly linked list in OCaml/

In addition to the implementation that Dennis Gang Chen posted, you
might also be interested in the following implementation of circular
doubly linked lists.  It's really sweet.

- Xavier Leroy

type 'a cdllist =
  { data: 'a; mutable prev: 'a cdllist; mutable next: 'a cdllist }

let create d =
  let rec l = { data = d; prev = l; next = l } in l

let insert_before d list =
  let newcons = { data = d; prev = list.prev; next = list } in
  list.prev.next <- newcons;
  list.prev <- newcons;
  newcons

let insert_after list d =
  let newcons = { data = d; prev = list; next = list.ext } in
  list.next.prev <- newcons;
  list.next <- newcons;
  newcons

let remove list =
  list.prev.next <- list.next;
  list.next.prev <- list.prev

let iter f list =
  let rec iter_rec l =
    f l.data;
    if l.next != list then iter_rec l.next
  in iter_rec list




      parent reply	other threads:[~2000-04-28 13:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-27 13:58 Jan Brosius
2000-04-28  5:24 ` Dennis (Gang) Chen
2000-04-28 11:55 ` Xavier Leroy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20000428135543.40826@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=jan.brosius@village.uunet.be \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox