Singly-linked list/Element definition
< Singly-linked list(Redirected from
Singly-Linked List (element))
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.
You are encouraged to solve this taskaccording to the task description, using any language you may know.
-
See also
- Array
- Associative array: Creation, Iteration
- Collections
- Compound data type
- Doubly-linked list: Definition, Element definition, Element insertion, Traversal Linked list
- Queue: Definition, Usage
- Set
- Singly-linked list: Element definition, Element insertion, Traversal
- Stack
[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
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