В двусвязном-циклическом указатель на предыдущий элемент не получился - C#
Формулировка задачи:
суть такая:
Имеется код:
с указателем next у меня всё на ништяках, но вот расстановка previous у меня косячная.
при проверке вывода
head.previous получается null.
в голове затуп и я никак не допру что я ту тперекосячиваю.
весь код для наглядности:
public void Insert_Element(int value)
{
if(Head == null)
{
Element temp = new Element(value, Head, Head);
Head = temp;
}
else
{
Element temp = new Element(value, null, null);
Head.Previous = temp;
temp.Next = Head;
Head = temp;
}
}public void Show_The_List_From_Tail()
{
Element temp = Head.Previous;
if(Head.Previous==null)
{
Console.WriteLine("ggg");
return;
}
while (temp != Head)
{
if (temp == null)
{
Console.WriteLine("gg");
break;
}
Console.Write(temp.Value + " ");
temp = temp.Previous;
}
Console.WriteLine(" ");
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*Организовать циклический двунаправленный линейный с функцией
* удаления диапазона значений из списка (дан первый номер и последний
* номер элементов, которые необходимо удалить из списка).*/
namespace Lab_2_EVM
{
class Program
{
class Element
{
public Element Next;
public Element Previous;
public int Value;
public Element(int value, Element next, Element prev)
{
Value = value;
Next = next;
Previous = prev;
}
}
class List
{
public Element Head = null;
public List() { }
public void Insert_Element(int value)
{
if(Head == null)
{
Element temp = new Element(value, Head, Head);
Head = temp;
}
else
{
Element temp = new Element(value, null, null);
Head.Previous = temp;
temp.Next = Head;
Head = temp;
}
}
public void Show_The_List_From_Head()
{
Element temp = Head;
while (temp != Head.Previous)
{
Console.Write(temp.Value + " ");
temp = temp.Next;
}
Console.WriteLine(" ");
}
public void Show_The_List_From_Tail()
{
Element temp = Head.Previous;
if(Head.Previous==null)
{
Console.WriteLine("ggg");
return;
}
while (temp != Head)
{
if (temp == null)
{
Console.WriteLine("gg");
break;
}
Console.Write(temp.Value + " ");
temp = temp.Previous;
}
Console.WriteLine(" ");
}
public void Search_The_Element_By_Key(int key)
{
Element temp = Head;
int done = 0;
while (temp != Head.Previous)
{
if(temp.Value == key)
{
done = 1;
Console.Write(temp.Value + " ");
temp = temp.Next;
}
else
{
temp = temp.Next;
}
if (temp == Head.Previous && done != 1)
{
Console.WriteLine("Нет такого Элемента!");
}
}
Console.WriteLine(" ");
}
public void Delete_Elements_From_Place_To_Place(int first_place,int last_place)
{
}
}
static void menu()
{
List MyList = new List();
while(true)
{
Console.WriteLine("Выберите необходимое действие:");
Console.WriteLine("1 - Ввод списка");
Console.WriteLine("2 - Показ списка");
Console.WriteLine("3 - Поиск элемента по ключу");
Console.WriteLine("4 - Удаление диапазона элементов");
Console.WriteLine("5 - Очистка списка");
Console.WriteLine("Выход любая другая цифра");
int menuitem = Convert.ToInt32(Console.ReadLine());
switch (menuitem)
{
case 1:
Console.Clear();
Console.Write("Введите элемент: ");
int elem = Convert.ToInt32(Console.ReadLine());
MyList.Insert_Element(elem);
break;
case 2:
Console.Clear();
Console.Write("В прямом порядке: ");
MyList.Show_The_List_From_Head();
Console.Write("В обратном порядке: ");
MyList.Show_The_List_From_Tail();
break;
case 3:
Console.Clear();
Console.Write("Введите элемент для поиска: ");
int key = Convert.ToInt32(Console.ReadLine());
MyList.Search_The_Element_By_Key(key);
break;
case 4:
Console.Clear();
Console.Write("Задать начальный элемент издиапазона удаления: ");
int first = Convert.ToInt32(Console.ReadLine());
Console.Write("Задать последний элемент издиапазона удаления: ");
int last = Convert.ToInt32(Console.ReadLine());
MyList.Delete_Elements_From_Place_To_Place(first, last);
break;
default:
return;
break;
}
}
}
static void Main(string[] args)
{
Program.menu();
}
}
}Решение задачи: «В двусвязном-циклическом указатель на предыдущий элемент не получился»
textual
Листинг программы
class Element
{
public Element Next;
public Element Previous;
public int Value;
public Element(int value, Element next, Element prev)
{
Value = value;
Next = next;
Previous = prev;
}
}
internal class List
{
public Element Head = null;
public void Insert_Element(int value)
{
if (Head == null)
{
Head = new Element(value, null, null);
Head.Previous = Head;
Head.Next = Head;
}
else
{
var temp = new Element(value, Head, Head.Previous);
Head.Previous.Next = temp;
Head.Previous = temp;
Head = temp;
}
}
}