ProgaOra/20241121/ConsoleApp1/ConsoleApp1/Program.cs
2024-11-21 10:08:03 +01:00

147 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Csomopont
{
public int Adat { get; set; }
public Csomopont Kovetkezo { get; set; }
public Csomopont(int adat) {
this.Adat = adat;
Kovetkezo = null;
}
}
public class LancoltLista
{
private Csomopont fej;
public LancoltLista() {
fej = null;
}
public void Hozzaad(int adat) {
Csomopont ujcsomopont = new Csomopont(adat);
if (fej == null)
{
fej = ujcsomopont;
}
else
{
Csomopont jelenlegi = fej;
while (jelenlegi.Kovetkezo != null)
{
jelenlegi = jelenlegi.Kovetkezo;
}
jelenlegi.Kovetkezo = ujcsomopont;
}
}
public void Torol(int adat)
{
if (fej == null)
{
Console.WriteLine("A lista üres");
return;
}
if (fej.Adat == adat)
{
fej = fej.Kovetkezo;
return;
}
Csomopont jelenlegi = fej;
while(jelenlegi.Kovetkezo != null && jelenlegi.Kovetkezo.Adat != adat)
{
jelenlegi = jelenlegi .Kovetkezo;
}
if (jelenlegi.Kovetkezo == null)
{
Console.WriteLine("At elem nem található a listában.");
}
else {
jelenlegi.Kovetkezo = jelenlegi.Kovetkezo.Kovetkezo;
}
}
public void Kiir()
{
if (fej == null)
{
Console.WriteLine("A lista üres");
return;
}
Csomopont jelenlegi = fej;
while (jelenlegi != null)
{
Console.WriteLine(jelenlegi.Adat + " ");
jelenlegi = jelenlegi.Kovetkezo;
}
Console.WriteLine();
}
}
internal class Program
{
static void Main(string[] args)
{
LancoltLista lancoltLista = new LancoltLista();
lancoltLista.Hozzaad(10);
lancoltLista.Hozzaad(20);
lancoltLista.Hozzaad(30);
lancoltLista.Hozzaad(40);
Console.WriteLine("Lista elemei: ");
lancoltLista.Kiir();
Console.WriteLine("Elem törlése (20):");
lancoltLista.Torol(20);
lancoltLista.Kiir();
Console.WriteLine("Elem törlése (50):");
lancoltLista.Torol(50);
lancoltLista.Kiir();
//kétszeresen láncolt lista
LinkedList<string> láncoltlista = new LinkedList<string>();
láncoltlista.AddLast("vár");
láncoltlista.AddLast("kávé");
láncoltlista.AddLast("autó");
láncoltlista.AddFirst("asd");
láncoltlista.RemoveLast();
LinkedListNode<string> newNOde = new LinkedListNode<string>("value");
láncoltlista.AddAfter(láncoltlista.First, newNOde);
LinkedListNode<string> linkedListNode = láncoltlista.First;
//Console.WriteLine(linkedListNode.Value);
foreach (string s in láncoltlista) { Console.WriteLine(s); }
LinkedListNode<string> nodelast = láncoltlista.Last;
while (nodelast != null)
{
Console.WriteLine(nodelast.Value);
nodelast = nodelast.Previous;
}
Console.ReadLine();
}
}
}