1 votes

How to concatenate two lists in c # with nodes?

Given two lists of products (price, name), join the first list with the second, in a third list.

 LISTA 1 = [4,7,3]
LISTA 2 = [1,6]
LISTA 3 = [4,7,3,1,6]

I found a code equal to this exercise that is in c++ , and, I don't know how to put it in c# . If you could help me, I would really appreciate it.

 //Clase Producto
    class Product
    {
        public string Name;
        public int Units;
        public float Cost;
    }

//Clase Nodo Producto
    class ProductNode
    {
        public Product Product;
        public ProductNode Next;
    }

//Clase lista
 class ProductList
    {
        ProductNode Head;

        public void AddProduct (Product product)
        {
            ProductNode newPNode = new ProductNode();
            newPNode.Product = product;

            if (Head == null)
            {
                Head = newPNode;
            }
            else
            {
                ProductNode last = Head;
                while (last.Next != null)
                {
                    last = last.Next;
                }
                last.Next = newPNode;
            }
        }

        public void Print()
        {
            ProductNode pr = Head;
            while (pr != null)
            {
                Console.WriteLine($"Product name: {pr.Product.Name} - Units: {pr.Product.Units} - Total Cost: {pr.Product.Cost}");
                pr = pr.Next;
            }

        }
    }

//Main
    class Program
    {
        public static void Main (string[] args)
        {
            ProductList productList = new ProductList();

            Product p1 = new Product();
            p1.Name = "Banana";
            p1.Units = 20;
            p1.Cost = 3000;
            productList.AddProduct(p1);

            Product p2 = new Product();
            p2.Name = "Apple";
            p2.Units = 30;
            p2.Cost = 5000;
            productList.AddProduct(p2);

            productList.Print();

            Console.WriteLine("----- LIST #3 -----");

        }
    }

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X