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()); } }
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; }
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; }
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()); }
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); }
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); }
public BinPoint(T dataValue) { Data = dataValue; Pref = null; Next = null; }