Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.0 kB
2
Indexable
Never
using System;

namespace Labolatoria05
{
    public class ArrayList<T> where T : IComparable
    {
        public T Var { get; set; }
        public ArrayList<T> Next { get; set; }

        public ArrayList()
        {
            Next = null;
        }

        public ArrayList(T val)
        {
            Var = val;
            Next = null;
        }

        public void DodajDoListy(T val)
        {
            if (Var.CompareTo(val) < 0)
            {
                if (Next == null)
                {
                    Next = new ArrayList<T>(val);
                }
                else
                {
                    if (Next.Var.CompareTo(val) < 0)
                    {
                        Next.DodajDoListy(val);
                    }
                    else
                    {
                        var newOne = new ArrayList<T>(val);
                        newOne.Next = Next;
                        Next = newOne;
                    }
                }
            }
            else if (Var.CompareTo(val) == 0)
            {
                var newOne = new ArrayList<T>(val);
                newOne.Next = Next;
                Next = newOne;
            }
            else
            {
                var newOne = new ArrayList<T>(val);
                newOne.Var = Var;
                newOne.Next = Next;
                Var = newOne.Var;
                Next = newOne.Next;
            }
        }

        public void Add(T val, ref ArrayList<T> root)
        {
            if (val.CompareTo(root.Var) < 0)
            {
                var newOne = new ArrayList<T>(val);
                newOne.Next = root;
                root = newOne;
            }
            else
            {
                DodajDoListy(val);
            }
        }

        public bool Find(T val)
        {
            if (Var != null)
            {
                if (Var.CompareTo(val) == 0)
                {
                    return true;
                }
            }
            return Next != null && Next.Find(val);
        }

        public void Display()
        {
            Console.WriteLine(Var);

            if (Next != null)
            {
                Next.Display();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var list = new ArrayList<int>();
            Random rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                int a = rand.Next(20);
                list.DodajDoListy(a);
            }

            list.Display();

            if (list.Find(8))
            {
                Console.WriteLine("True");
            }
            else
            {
                Console.WriteLine("False");
            }

            Console.ReadKey();
        }
    }
}
Leave a Comment