关于后端:go语言rustc语言phpnodezig分别实现链式操作

46次阅读

共计 2160 个字符,预计需要花费 6 分钟才能阅读完成。

以下是 AI 别离实现 Go 语言、Rust、C 语言、PHP、Node.js 和 Zig 的链式操作代码示例:

Go 语言

package main

import "fmt"

type IntList struct {
    value int
    next  *IntList
}

func (l *IntList) Add(value int) *IntList {node := &IntList{value: value, next: l}
    return node
}

func main() {list := &IntList{}
    list = list.Add(3).Add(2).Add(1)
    
    // 遍历链表
    for node := list; node != nil; node = node.next {fmt.Println(node.value)
    }
}

Rust

struct IntList {
    value: i32,
    next: Option<Box<IntList>>,
}

impl IntList {fn new() -> IntList {IntList { value: 0, next: None}
    }
    
    fn add(mut self, value: i32) -> IntList {let next = Some(Box::new(self));
        IntList {value, next}
    }
}

fn main() {let mut list = IntList::new();
    list = list.add(3).add(2).add(1);
    
    // 遍历链表
    let mut node = Some(&list);
    while let Some(n) = node {println!("{}", n.value);
        node = n.next.as_deref();}
}

C 语言

#include <stdio.h>
#include <stdlib.h>

typedef struct IntList {
    int value;
    struct IntList* next;
} IntList;

IntList* add(IntList* list, int value) {IntList* node = (IntList*)malloc(sizeof(IntList));
    node->value = value;
    node->next = list;
    return node;
}

int main() {
    IntList* list = NULL;
    list = add(add(add(list, 1), 2), 3);
    
    // 遍历链表
    IntList* node = list;
    while (node != NULL) {printf("%d\n", node->value);
        node = node->next;
    }
    
    return 0;
}

PHP

<?php

class IntList {
    public $value;
    public $next;
    
    function __construct() {
        $this->value = null;
        $this->next = null;
    }
    
    function add($value) {$node = new IntList();
        $node->value = $value;
        $node->next = $this;
        return $node;
    }
}

$list = new IntList();
$list = $list->add(3)->add(2)->add(1);

// 遍历链表
$node = $list;
while ($node != null) {
    echo $node->value . "\n";
    $node = $node->next;
}
?>

Node.js

class IntList {constructor() {
        this.value = null;
        this.next = null;
    }
    
    add(value) {const node = new IntList();
        node.value = value;
        node.next = this;
        return node;
    }
}

let list = new IntList();
list = list.add(3).add(2).add(1);

// 遍历链表
let node = list;
while (node !== null) {console.log(node.value);
    node = node.next;
}

Zig

const std = @import("std");

pub const IntList = comptime struct {
    value: i32,
    next: ?*IntList,
};

pub fn add(list: ?*IntList, value: i32) ?*IntList {var node: [*]IntList = &[_]IntList{.{ .value = value, .next = list},
    };
    return &node[0];
}

pub fn main() !void {
    var list: ?*IntList = null;
    list = add(add(add(list, 1), 2), 3);
    
    // 遍历链表
    var node: ?*IntList = list;
    while (node) : (node = node.*next) {std.debug.print("{d}\n", .{node.*value});
    }
    
    return try std.io.getWriter().flush();
}

这些是链式操作的简略示例,用于介绍不同语言的链式操作概念,并不代表残缺的链表数据结构的实现。具体的实现形式可能依据需要和语言个性有所差别。

本文由 mdnice 多平台公布

正文完
 0