Untitled
unknown
plain_text
2 years ago
7.6 kB
2
Indexable
using System; class Program { public class ElementListy { public double Wartość; public ElementListy Następny; public ElementListy(double x) { Wartość = x; Następny = null; } public void Usuń() { if (Następny != null) { if (Następny.Następny != null) { Następny.Usuń(); } Następny = null; } } public bool Szukaj(double x) { bool wynik = false; // Domyślna wartość! if (Wartość == x) { wynik = true; } if (!wynik && Następny != null) { wynik = Następny.Szukaj(x); } return wynik; } public void Dopisz(double x) //dodawanie elementu na koniec listy { if (Następny == null) { Następny = new ElementListy(x); } else { Następny.Dopisz(x); } } } public class ListaJednokierunkowa { private ElementListy głowa; public ListaJednokierunkowa() { głowa = null; } public void DodajNaPoczątek(double wartość) { ElementListy nowyElement = new ElementListy(wartość); nowyElement.Następny = głowa; głowa = nowyElement; } private ElementListy DodajNaPoczątekRekurencyjnie(ElementListy obecny, double wartość) { if (obecny == null) { return new ElementListy(wartość); } obecny.Następny = DodajNaPoczątekRekurencyjnie(obecny.Następny, wartość); return obecny; } public void Wyświetl() { WyświetlRekurencyjnie(głowa); Console.WriteLine("null"); } private void WyświetlRekurencyjnie(ElementListy obecny) { if (obecny != null) { Console.Write($"{obecny.Wartość} -> "); WyświetlRekurencyjnie(obecny.Następny); } } public void Usun(double wartość) { głowa = UsunRekurencyjnie(głowa, wartość); } public void Dopisz(double wartość) { if (głowa == null) { głowa = new ElementListy(wartość); } else { głowa.Dopisz(wartość); } } private ElementListy UsunRekurencyjnie(ElementListy obecny, double wartość) { if (obecny == null) { return null; } // Usuwanie, gdy znaleziony element jest głową if (obecny.Wartość.Equals(wartość)) { ElementListy pomocniczy = obecny.Następny; obecny = null; // Zwolnienie pamięci (opcjonalne, bo Garbage Collector zadba o to) return pomocniczy; } // Usuwanie, gdy znaleziony element jest na końcu listy if (obecny.Następny != null && obecny.Następny.Wartość.Equals(wartość)) { if (obecny.Następny.Następny == null) { obecny.Następny = null; } else { obecny.Następny = obecny.Następny.Następny; } return obecny; } // Usuwanie, gdy znaleziony element jest wewnątrz listy if (obecny.Następny != null && obecny.Następny.Wartość.Equals(wartość)) { if (obecny.Następny.Następny != null) { ElementListy pomocniczy = obecny.Następny; obecny.Następny = obecny.Następny.Następny; pomocniczy = null; // Zwolnienie pamięci (opcjonalne) } } obecny.Następny = UsunRekurencyjnie(obecny.Następny, wartość); return obecny; } public void Usuń() { if (głowa != null) { głowa.Usuń(); głowa = null; } } public bool Szukaj(double x) { if (głowa != null) { return głowa.Szukaj(x); } return false; } public void Sortuj() { głowa = SortujRekurencyjnie(głowa); } private ElementListy SortujRekurencyjnie(ElementListy lista) { if (lista == null || lista.Następny == null) return lista; ElementListy posortowanaLista = null; ElementListy obecny = lista; while (obecny != null) { ElementListy następny = obecny.Następny; posortowanaLista = Wstaw(posortowanaLista, obecny); obecny = następny; } return posortowanaLista; } private ElementListy Wstaw(ElementListy posortowanaLista, ElementListy nowyElement) { if (posortowanaLista == null || posortowanaLista.Wartość >= nowyElement.Wartość) { nowyElement.Następny = posortowanaLista; return nowyElement; } ElementListy obecny = posortowanaLista; while (obecny.Następny != null && obecny.Następny.Wartość < nowyElement.Wartość) { obecny = obecny.Następny; } nowyElement.Następny = obecny.Następny; obecny.Następny = nowyElement; return posortowanaLista; } } // Pozostała część klasy Program bez zmian static void Main(string[] args) { ListaJednokierunkowa lista = new ListaJednokierunkowa(); lista.DodajNaPoczątek(3.5); lista.DodajNaPoczątek(2.2); lista.DodajNaPoczątek(1.1); Console.WriteLine("Lista jednokierunkowa przed usunięciem:"); lista.Wyświetl(); double elementDoUsunięcia = 2.2; Console.WriteLine($"\nUsuwam element {elementDoUsunięcia}"); lista.Usun(elementDoUsunięcia); Console.WriteLine("\nLista jednokierunkowa po usunięciu:"); lista.Wyświetl(); double szukanaWartość = 3.5; Console.WriteLine($"\nCzy lista zawiera wartość {szukanaWartość}: {lista.Szukaj(szukanaWartość)}"); szukanaWartość = 4.5; Console.WriteLine($"\nCzy lista zawiera wartość {szukanaWartość}: {lista.Szukaj(szukanaWartość)}"); double nowaWartość = 4.4; Console.WriteLine($"\nDopisuję wartość {nowaWartość} na koniec listy:"); lista.Dopisz(nowaWartość); lista.Wyświetl(); Console.WriteLine("\nSortowanie listy:"); lista.Sortuj(); lista.Wyświetl(); Console.WriteLine("\nUsuwanie całej listy z pamięci:"); lista.Usuń(); lista.Wyświetl(); } }
Editor is loading...
Leave a Comment