Exemple #1
0
        public void Show()
        {
            BinPoint <T> prefBranch = Pref;
            Stack <T>    prefs      = new Stack <T>();

            while (prefBranch != null)
            {
                prefs.Push(prefBranch.Data);
                prefBranch = prefBranch.Pref;
            }
            while (prefs.Count > 0)
            {
                Console.Write("{0} ", prefs.Pop());
            }
            Console.Write("{0} ", Data);
            BinPoint <T> nextBranch = Next;
            Stack <T>    nexts      = new Stack <T>();

            while (nextBranch != null)
            {
                nexts.Push(nextBranch.Data);
                nextBranch = nextBranch.Next;
            }
            while (nexts.Count > 0)
            {
                Console.Write("{0} ", nexts.Pop());
            }
        }
Exemple #2
0
        public void AddNext(T value)
        {
            BinPoint <T> nElem = new BinPoint <T>(value);
            BinPoint <T> cur   = this;

            while (cur.Next != null)
            {
                cur = cur.Next;
            }
            cur.Next   = nElem;
            nElem.Pref = cur;
        }
Exemple #3
0
        public void AddPref(T value)
        {
            BinPoint <T> nElem = new BinPoint <T>(value);
            BinPoint <T> cur   = this;

            while (cur.Pref != null)
            {
                cur = cur.Pref;
            }
            cur.Pref   = nElem;
            nElem.Next = cur;
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Введите размер создаваемого списка");
            int            size     = ReadNaturalNum();
            BinPoint <int> taskList = GenerateIntBinPoint(size);

            taskList.Show();
            Console.WriteLine(@"
Количество элементов в двунаправленном списке:
Вычислено рекурсивно: {0}
Вычислено нерекурсивно: {1}", taskList.CountElems_Recur(), taskList.CountElems_NonRecur());
        }
Exemple #5
0
        public int CountElems_NonRecur()
        {
            BinPoint <T> prefBranch = Pref;
            int          num        = 1;

            while (prefBranch != null)
            {
                ++num;
                prefBranch = prefBranch.Pref;
            }
            BinPoint <T> nextBranch = Next;

            while (nextBranch != null)
            {
                ++num;
                nextBranch = nextBranch.Next;
            }
            return(num);
        }
Exemple #6
0
        static BinPoint <int> GenerateIntBinPoint(int size)
        {
            BinPoint <int> cur = new BinPoint <int>(rnd.Next(-15, 16));

            for (var i = 0; i < size - 1; ++i)
            {
                switch (rnd.Next(0, 2))
                {
                case 0:
                    cur.AddNext(rnd.Next(-15, 16));
                    break;

                case 1:
                    cur.AddPref(rnd.Next(-15, 16));
                    break;
                }
            }
            return(cur);
        }
Exemple #7
0
 public BinPoint(T dataValue)
 {
     Data = dataValue;
     Pref = null;
     Next = null;
 }