各种语言的单链表实现方法

Singly-linked list/Element definition

Task
Singly-linked list/Element definition
You are encouraged to  solve this taskaccording to the task description, using any language you may know.
Define the data structure for a  singly-linked list element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.
See also

Contents

 [hide

[edit]ACL2

The built in pair type, cons, is sufficient for defining a linked list. ACL2 does not have mutable variables, so functions must instead return a copy of the original list.

(let ((elem 8)
      (next (list 6 7 5 3 0 9)))
  (cons elem next))

Output:

(8 6 7 5 3 0 9)

[edit]ActionScript

package
{
    
	public class Node
	{
    
		public var data:Object = null;
		public var link:Node = null;
 
		public function Node(obj:Object)
		{
    
			data = obj;
		}
	}
}

[edit]Ada

type Link;
type Link_Access is access Link;
type Link is record
   Next : Link_Access := null;
   Data : Integer;
end record;

[edit]ALGOL 68

Works withALGOL 68 version Revision 1
Works withALGOL 68G version Any - tested with release  algol68g-2.7.
Works withELLA ALGOL 68 version Any (with appropriate job cards) - tested with release  1.8-8d
File: prelude/single_link.a68
# -*- coding: utf-8 -*- #
CO REQUIRES:
  MODE OBJVALUE = ~ # Mode/type of actual obj to be stacked #
END CO
 
MODE OBJNEXTLINK = STRUCT(
  REF OBJNEXTLINK next,
  OBJVALUE value # ... etc. required #
);
 
PROC obj nextlink new = REF OBJNEXTLINK:
  HEAP OBJNEXTLINK;
 
PROC obj nextlink free = (REF OBJNEXTLINK free)VOID:
  next OF free := obj stack empty # give the garbage collector a BIG hint #
See also:  Stack

[edit]AutoHotkey

element = 5 ; data
element_next = element2  ; link to next element

[edit]AWK

Awk only has global associative arrays, which will be used for the list. Numerical indexes into the array will serve as node pointers. A list element will have the next node pointer separated from the value by the pre-defined SUBSEP value. A function will be used to access a node's next node pointer or value given a node pointer (array index). The first array element will serve as the list head.

 
BEGIN {
    
    NIL = 0
    HEAD = 1
    LINK = 1
    VALUE = 2
 
    delete list
    initList()
}
 
function initList() {
    
    delete list
    list[HEAD] = makeNode(NIL, NIL)
}
 
function makeNode
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值