How to make a linked list in Ruby

1

I have class Nodo and class Lista to call them from Main ; in Java I've done it but I do not know how to Ruby .

In Java I would have what it is:

class Nodo{

    String nombre, id, tel; Nodo enlace = null ;
}

with their respective get and set.

And class Lista that has all the insert by head methods, but I do not know how to do it in Ruby .

Help.

Class Node

public class Nodo {

int dato;
Nodo enlace;


/**
 * @return the dato
 */
 public int getDato() {
    return dato;
 }

/**
 * @param dato the dato to set
 */
 public void setDato(int dato) {
    this.dato = dato;
 }

/**
 * @return the enlace
 */
 public Nodo getEnlace() {
    return enlace;
 }

/**
 * @param enlace the enlace to set
 */
 public void setEnlace(Nodo enlace) {
    this.enlace = enlace;
 }

 public Nodo(int x){
    dato = x;
    enlace = null;
 }
 public Nodo(int x, Nodo n){ //el constructor en Java
    dato = x;
    enlace = null;
 }


}


-----------------------------------------------------------------------------

 public class Lista {
  private Nodo primero;

  public Lista(){
    primero = null;
 }

 public Lista insertarCabeceraLista(int entrada){
    Nodo nuevo;

    nuevo = new Nodo(entrada);
    nuevo.enlace=primero;
    primero = nuevo;

    return this;
 }

 public void visualizar(){
    Nodo n;
    int k = 0;

    n=primero;
    while(n != null){
        System.out.print(n.dato + " ");
        n = n.enlace;

        k++;

        System.out.print((k%10 != 0 ? " " : "\n"));
    }
 }
}

That would be all my code in Java, now I need someone to help me work in Ruby

    
asked by Erick Barillas 02.03.2018 в 05:21
source

1 answer

0

It does not differ much from the java process, I pass you a code on a similar LinkedList and Node class, which I hope will help you:

#!/usr/bin/ruby
# -*- coding: utf-8 -*- 

# lista enlazada

class ListaEnlazada
  attr_accessor :primero, :ultimo # crea get set de primero y ultimo de la lista

  def initialize # define elemento vacio de lista enlazada
    @primero = @ultimo = nil
  end

  def insertarAlPrincipio(valor)
    nodo = Nodo.new(valor) # creamos nodo nuvo
    nodo.enlace= @primero  # unimos con el primero
    @primero = nodo  # ahora el primero es el nuevo
    @ultimo = nodo if @ultimo.nil? # y el último si era lista vacía

  end

  def insertarAlFinal(valor)
    nodo = Nodo.new(valor)

    @primero = nodo if @primero.nil?
    @ultimo.enlace = nodo unless @ultimo.nil?

    @ultimo = nodo
  end

  def verLista()
    temp = @primero
    while !( temp.nil?)
      print temp.dato.to_s + ' '
      temp = temp.enlace
    end
    puts " "
  end 
end

class Nodo
  attr_accessor :dato # crea get set de dato
  def initialize(valor) # define un nodo
    @dato = valor
    @enlace = nil # llamado sig (de siguiente) en otras nomenclaturas
  end

  def enlace # getEnlace de forma manual
    @enlace
  end

  def enlace=(valor) # setEnlace
    @enlace = valor
  end

end

list = ListaEnlazada.new()
list.insertarAlPrincipio(1)
list.insertarAlPrincipio(2)
list.insertarAlPrincipio(3)
list.insertarAlFinal(0)
list.verLista
# => 3 2 1 0  # salida esperada

Tip I commented on the process of inserting at the beginning and insert method at the end. For this list and included "first" and "last" elements in its specification. Health

    
answered by 23.07.2018 в 18:51