static void Main(string[] args) { List <int> list = new List <int>(); LinkedList2 s1 = new LinkedList2(); s1.Add(new Node(5)); s1.Add(new Node(30)); s1.Add(new Node(15)); s1.Add(new Node(20)); s1.Add(new Node(10)); Console.WriteLine("LinkedList:"); s1.PrintList(); Console.WriteLine(); Console.WriteLine("Add 100 after 30"); s1.add_after(30, 100); s1.PrintList(); Console.WriteLine(); Console.WriteLine("Add zero to first"); s1.AddFirst(0); s1.PrintList(); Console.WriteLine(); Console.WriteLine("Delete 15 from the LinkedList"); s1.Del(15); s1.PrintList(); Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { //// тест метода InsertAfter, если список непустой и узел _nodeAfter непустой //LinkedList2 testList = new LinkedList2(); //testList.AddInTail(new Node(1)); //testList.AddInTail(new Node(2)); //testList.AddInTail(new Node(4)); //testList.AddInTail(new Node(5)); //Console.WriteLine("Вставка в непустой список"); //Console.WriteLine("Перед вставкой:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //testList.InsertAfter(new Node(2), new Node(3)); //Console.WriteLine("\nПосле вставки:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //Node node = testList.head; //while (node != null) //{ // if (node.value == 3) // { // Console.WriteLine("Предыдущий узел: {0}", node.prev.value); // Console.WriteLine("Следующий узел: {0}", node.next.value); // } // node = node.next; //} //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("=============================="); //// тест метода InsertAfter, если список пустой и узел _nodeAfter пустой //testList = new LinkedList2(); //Console.WriteLine("Вставка в пустой список после пустого узла"); //Console.WriteLine("Перед вставкой:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("Голова: {0}", testList.head); //Console.WriteLine("Хвост: {0}", testList.tail); //testList.InsertAfter(null, new Node(3)); //Console.WriteLine("\nПосле вставки:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //node = testList.head; //while (node != null) //{ // if (node.value == 3) // { // Console.WriteLine("Предыдущий узел: {0}", node.prev == null); // Console.WriteLine("Следующий узел: {0}", node.next == null); // } // node = node.next; //} //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("=============================="); //Console.WriteLine("Вставка в непустой список если узел _nodeAfter не существует"); //Console.WriteLine("Перед вставкой:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //testList.InsertAfter(null, new Node(4)); //Console.WriteLine("\nПосле вставки:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //node = testList.head; //while (node != null) //{ // if (node.value == 4) // { // Console.WriteLine("Предыдущий узел: {0}", node.prev.value); // Console.WriteLine("Следующий узел: {0}", node.next == null ? "null" : node.next.value.ToString()); // } // node = node.next; //} //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("=============================="); //Console.WriteLine("Вставка в конец непустого списка если узел _nodeAfter существует"); //Console.WriteLine("Перед вставкой:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Число узлов: {0}", testList.Count()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //testList.InsertAfter(new Node(4), new Node(5)); //Console.WriteLine("\nПосле вставки:"); //Console.WriteLine("Список: " + testList.PrintList()); //Console.WriteLine("Голова: {0}", testList.head.value); //Console.WriteLine("Хвост: {0}", testList.tail.value); //node = testList.head; //while (node != null) //{ // if (node.value == 5) // { // Console.WriteLine("Предыдущий узел: {0}", node.prev.value); // Console.WriteLine("Следующий узел: {0}", node.next == null ? "null" : node.next.value.ToString()); // } // node = node.next; //} LinkedList2 testList2 = new LinkedList2(); testList2.InsertAfter(null, new Node(1)); testList2.InsertAfter(null, new Node(2)); testList2.InsertAfter(null, new Node(3)); testList2.InsertAfter(null, new Node(4)); testList2.InsertAfter(null, new Node(5)); testList2.InsertAfter(null, new Node(6)); testList2.InsertAfter(null, new Node(7)); testList2.InsertAfter(null, new Node(8)); testList2.InsertAfter(null, new Node(9)); testList2.InsertAfter(null, new Node(10)); //Console.WriteLine("Удаление с конца непустого списка если удаляемый узел существует"); //Console.WriteLine("Перед удалением:"); //Console.WriteLine("Список: " + testList2.PrintList()); //Console.WriteLine("Число узлов: {0}", testList2.Count()); //Console.WriteLine("Голова: {0}", testList2.head.value); //Console.WriteLine("Хвост: {0}", testList2.tail.value); //testList2.Remove(4); //Console.WriteLine("\nПосле удаления:"); //Console.WriteLine("Список: " + testList2.PrintList()); //Console.WriteLine("Голова: {0}", testList2.head.value); //Console.WriteLine("Хвост: {0}", testList2.tail.value); //Console.WriteLine("Предыдущий узел хвоста: {0}", testList2.tail.prev.value); //Console.WriteLine("Следующий узел хвоста: {0}", testList2.tail.next == null ? "null" : testList2.tail.next.value.ToString()); //Console.WriteLine("\nУдаление узла 2: {0}", testList2.Remove(2)); //Console.WriteLine("Список: " + testList2.PrintList()); //Console.WriteLine("Голова: {0}", testList2.head.value); //Console.WriteLine("Хвост: {0}", testList2.tail.value); //Console.WriteLine("Предыдущий узел до удаленного узла считает следующим: {0}", testList2.head.next.value); //Console.WriteLine("Следующий узел после удаленного узла считает предыдущим: {0}", testList2.tail.prev.value); //Console.WriteLine("Предыдущий узел хвоста: {0}", testList2.tail.prev.value); //Console.WriteLine("Следующий узел хвоста: {0}", testList2.tail.next == null ? "null" : testList2.tail.next.value.ToString()); //Console.WriteLine("\nУдаление узла из головы (1): {0}", testList2.Remove(1)); //Console.WriteLine("Список: " + testList2.PrintList()); //Console.WriteLine("Голова: {0}", testList2.head.value); //Console.WriteLine("Хвост: {0}", testList2.tail.value); //Console.WriteLine("Предыдущий узел до головы: {0}", testList2.head.prev == null ? "null" : testList2.head.prev.value.ToString()); //Console.WriteLine("Следующий узел после головы: {0}", testList2.head.next == null ? "null" : testList2.head.next.value.ToString()); //Console.WriteLine("Узел перед хвостом: {0}", testList2.tail.prev == null ? "null" : testList2.tail.prev.value.ToString()); //Console.WriteLine("Узел после хвоста: {0}", testList2.tail.next == null ? "null" : testList2.tail.next.value.ToString()); Console.WriteLine("Список: " + testList2.PrintList()); Console.WriteLine("\nУдаление нескольких узлов из списка: "); testList2.RemoveAll(9); Console.WriteLine("Список: " + testList2.PrintList()); Console.WriteLine("Голова: {0}", testList2.head.value); Console.WriteLine("Хвост: {0}", testList2.tail.value); Console.WriteLine("Предыдущий узел до головы: {0}", testList2.head.prev == null ? "null" : testList2.head.prev.value.ToString()); Console.WriteLine("Следующий узел после головы: {0}", testList2.head.next == null ? "null" : testList2.head.next.value.ToString()); Console.WriteLine("Узел перед хвостом: {0}", testList2.tail.prev == null ? "null" : testList2.tail.prev.value.ToString()); Console.WriteLine("Узел после хвоста: {0}", testList2.tail.next == null ? "null" : testList2.tail.next.value.ToString()); //Console.WriteLine("\nУдаление единственного узла в списке (3): {0}", testList2.Remove(3)); //Console.WriteLine("Список: " + testList2.PrintList()); //Console.WriteLine("Голова: {0}", testList2.head == null); //Console.WriteLine("Хвост: {0}", testList2.tail == null); Console.ReadKey(true); }