This commit is contained in:
Lendaia Mirai
2025-11-02 00:03:25 +01:00
parent 3301d77783
commit 0ffa65cac7
8 changed files with 1654 additions and 711 deletions

View File

@@ -1,274 +1,274 @@
using System; // using System;
using System.Collections; // using System.Collections;
using System.Collections.Generic; // using System.Collections.Generic;
namespace OE.ALGA.Adatszerkezetek // namespace OE.ALGA.Adatszerkezetek
{ // {
public class TombVerem<T> : Verem<T> // public class TombVerem<T> : Verem<T>
{ // {
T[] tomb; // T[] tomb;
int n = 0; // int n = 0;
public TombVerem(int l) // public TombVerem(int l)
{ // {
tomb = new T[l]; // tomb = new T[l];
} // }
public bool Ures // public bool Ures
{ // {
get { return n == 0; } // get { return n == 0; }
} // }
public T Felso() // public T Felso()
{ // {
return tomb[n - 1]; // return tomb[n - 1];
} // }
public void Verembe(T ertek) // public void Verembe(T ertek)
{ // {
if (n < tomb.Length) // if (n < tomb.Length)
{ // {
tomb[n] = ertek; // tomb[n] = ertek;
n++; // n++;
} // }
else // else
{ // {
throw new NincsHelyKivetel(); // throw new NincsHelyKivetel();
} // }
} // }
public T Verembol() // public T Verembol()
{ // {
if (Ures) // if (Ures)
{ // {
throw new NincsElemKivetel(); // throw new NincsElemKivetel();
} // }
else // else
{ // {
T ertek = tomb[n - 1]; // T ertek = tomb[n - 1];
n--; // n--;
return ertek; // return ertek;
} // }
} // }
} // }
public class TombSor<T> : Sor<T> // public class TombSor<T> : Sor<T>
{ // {
T[] tomb; // T[] tomb;
int n = 0; // int n = 0;
public TombSor(int l) // public TombSor(int l)
{ // {
tomb = new T[l]; // tomb = new T[l];
} // }
public bool Ures // public bool Ures
{ // {
get { return n == 0; } // get { return n == 0; }
} // }
public T Elso() // public T Elso()
{ // {
return tomb[0]; // return tomb[0];
} // }
public void Sorba(T ertek) // public void Sorba(T ertek)
{ // {
if (n < tomb.Length) // if (n < tomb.Length)
{ // {
tomb[n] = ertek; // tomb[n] = ertek;
n++; // n++;
} // }
else // else
{ // {
throw new NincsHelyKivetel(); // throw new NincsHelyKivetel();
} // }
} // }
public T Sorbol() // public T Sorbol()
{ // {
if (Ures) // if (Ures)
{ // {
throw new NincsElemKivetel(); // throw new NincsElemKivetel();
} // }
else // else
{ // {
T ertek = tomb[0]; // T ertek = tomb[0];
T[] tmp = new T[tomb.Length]; // T[] tmp = new T[tomb.Length];
n--; // n--;
for (int i = 1; i < tomb.Length; i++) // for (int i = 1; i < tomb.Length; i++)
{ // {
tmp[i - 1] = tomb[i]; // tmp[i - 1] = tomb[i];
} // }
tomb = tmp; // tomb = tmp;
return ertek; // return ertek;
} // }
} // }
} // }
public class TombLista<T> : Lista<T>, IEnumerable<T> // public class TombLista<T> : Lista<T>, IEnumerable<T>
{ // {
T[] tomb; // T[] tomb;
int n = 0; // int n = 0;
public bool Ures // public bool Ures
{ // {
get { return n <= 0; } // get { return n <= 0; }
} // }
public TombLista() // public TombLista()
{ // {
tomb = new T[1]; // tomb = new T[1];
} // }
public int Elemszam // public int Elemszam
{ // {
get { return n; } // get { return n; }
} // }
public void Bejar(Action<T> muvelet) // public void Bejar(Action<T> muvelet)
{ // {
for (int i = 0; i < n; i++) // for (int i = 0; i < n; i++)
{ // {
muvelet?.Invoke(tomb[i]); // muvelet?.Invoke(tomb[i]);
} // }
} // }
public void Beszur(int index, T ertek) // public void Beszur(int index, T ertek)
{ // {
if (index < 0) // if (index < 0)
{ // {
throw new HibasIndexKivetel(); // throw new HibasIndexKivetel();
} // }
else // else
{ // {
T[] tmp = new T[tomb.Length * 2]; // T[] tmp = new T[tomb.Length * 2];
for (int i = 0; i < index; i++) // for (int i = 0; i < index; i++)
{ // {
tmp[i] = tomb[i]; // tmp[i] = tomb[i];
} // }
tmp[index] = ertek; // tmp[index] = ertek;
for (int i = index + 1; i < tomb.Length; i++) // for (int i = index + 1; i < tomb.Length; i++)
{ // {
tmp[i] = tomb[i - 1]; // tmp[i] = tomb[i - 1];
} // }
tomb = tmp; // tomb = tmp;
n++; // n++;
} // }
} // }
public IEnumerator<T> GetEnumerator() // public IEnumerator<T> GetEnumerator()
{ // {
foreach (T t in tomb) // foreach (T t in tomb)
{ // {
if (t != null) // if (t != null)
{ // {
yield return t; // yield return t;
} // }
} // }
} // }
public void Hozzafuz(T ertek) // public void Hozzafuz(T ertek)
{ // {
if (n < tomb.Length) // if (n < tomb.Length)
{ // {
tomb[n] = ertek; // tomb[n] = ertek;
n++; // n++;
} // }
else // else
{ // {
T[] tmp = new T[tomb.Length * 2]; // T[] tmp = new T[tomb.Length * 2];
for (int i = 0; i < tomb.Length; i++) // for (int i = 0; i < tomb.Length; i++)
{ // {
tmp[i] = tomb[i]; // tmp[i] = tomb[i];
} // }
tmp[n] = ertek; // tmp[n] = ertek;
tomb = tmp; // tomb = tmp;
n++; // n++;
} // }
} // }
public T Kiolvas(int index) // public T Kiolvas(int index)
{ // {
if (index < 0 || index >= tomb.Length) // if (index < 0 || index >= tomb.Length)
{ // {
throw new HibasIndexKivetel(); // throw new HibasIndexKivetel();
} // }
return tomb[index]; // return tomb[index];
} // }
public void Modosit(int index, T ertek) // public void Modosit(int index, T ertek)
{ // {
if (index < 0 && index < tomb.Length) // if (index < 0 && index < tomb.Length)
{ // {
throw new HibasIndexKivetel(); // throw new HibasIndexKivetel();
} // }
else // else
{ // {
tomb[index] = ertek; // tomb[index] = ertek;
} // }
} // }
public void Torol(T ertek) // public void Torol(T ertek)
{ // {
int m = n; // int m = n;
T[] tmp = new T[tomb.Length]; // T[] tmp = new T[tomb.Length];
int index = 0; // int index = 0;
for (int i = 0; i < m; i++) // for (int i = 0; i < m; i++)
{ // {
if (ertek.Equals(tomb[i])) // if (ertek.Equals(tomb[i]))
{ // {
n--; // n--;
continue; // continue;
} // }
tmp[index] = tomb[i]; // tmp[index] = tomb[i];
index++; // index++;
} // }
tomb = tmp; // tomb = tmp;
} // }
IEnumerator IEnumerable.GetEnumerator() // IEnumerator IEnumerable.GetEnumerator()
{ // {
TombListaBejaro<T> bejaro = new TombListaBejaro<T>(tomb, n); // TombListaBejaro<T> bejaro = new TombListaBejaro<T>(tomb, n);
return bejaro; // return bejaro;
} // }
} // }
public class TombListaBejaro<T> : IEnumerator<T> // public class TombListaBejaro<T> : IEnumerator<T>
{ // {
T[] e; // T[] e;
int n; // int n;
int aktualisindex; // int aktualisindex;
T current; // T current;
public T Current // public T Current
{ // {
get { return current; } // get { return current; }
} // }
public TombListaBejaro(T[] e, int n) // public TombListaBejaro(T[] e, int n)
{ // {
this.n = n; // this.n = n;
this.e = e; // this.e = e;
aktualisindex = 0; // aktualisindex = 0;
current = e[0]; // current = e[0];
} // }
object IEnumerator.Current => Current; // object IEnumerator.Current => Current;
public void Dispose() // public void Dispose()
{ // {
throw new NotImplementedException(); // throw new NotImplementedException();
} // }
public bool MoveNext() // public bool MoveNext()
{ // {
if (aktualisindex < n - 1) // if (aktualisindex < n - 1)
{ // {
aktualisindex++; // aktualisindex++;
return true; // return true;
} // }
else // else
{ // {
return false; // return false;
} // }
} // }
public void Reset() // public void Reset()
{ // {
aktualisindex = -1; // aktualisindex = -1;
} // }
} // }
} // }

View File

@@ -1,325 +1,325 @@
using System; // using System;
using System.Collections; // using System.Collections;
using System.Collections.Generic; // using System.Collections.Generic;
using System.Runtime.CompilerServices; // using System.Runtime.CompilerServices;
namespace OE.ALGA.Adatszerkezetek; // namespace OE.ALGA.Adatszerkezetek;
public class LancElem<T> // public class LancElem<T>
{ // {
public T tart; // public T tart;
public LancElem<T>? kov; // public LancElem<T>? kov;
public LancElem<T>? elozo; // public LancElem<T>? elozo;
public LancElem(T tart, LancElem<T>? kov, LancElem<T>? elozo) // public LancElem(T tart, LancElem<T>? kov, LancElem<T>? elozo)
{ // {
this.tart = tart; // this.tart = tart;
this.kov = kov; // this.kov = kov;
this.elozo = elozo; // this.elozo = elozo;
} // }
} // }
public class LancoltVerem<T> : Verem<T> // public class LancoltVerem<T> : Verem<T>
{ // {
LancElem<T>? fej; // LancElem<T>? fej;
LancElem<T>? teteje; // LancElem<T>? teteje;
public LancoltVerem() // public LancoltVerem()
{ // {
} // }
public void Felszabadit() // public void Felszabadit()
{ // {
} // }
public bool Ures // public bool Ures
{ // {
get { return fej == null; } // get { return fej == null; }
} // }
public T Felso() // public T Felso()
{ // {
if (Ures) // if (Ures)
{ // {
throw new NincsElemKivetel(); // throw new NincsElemKivetel();
} // }
else // else
{ // {
LancElem<T>? tmp = fej; // LancElem<T>? tmp = fej;
while (tmp.kov != null) // while (tmp.kov != null)
{ // {
tmp = tmp.kov; // tmp = tmp.kov;
} // }
teteje = tmp; // teteje = tmp;
return tmp.tart; // return tmp.tart;
} // }
} // }
public void Verembe(T ertek) // public void Verembe(T ertek)
{ // {
if (Ures) // if (Ures)
{ // {
fej = new LancElem<T>(ertek, null, null); // fej = new LancElem<T>(ertek, null, null);
teteje = fej; // teteje = fej;
} // }
else // else
{ // {
teteje.kov = new LancElem<T>(ertek, null, teteje); // teteje.kov = new LancElem<T>(ertek, null, teteje);
teteje = teteje.kov; // teteje = teteje.kov;
} // }
} // }
public T Verembol() // public T Verembol()
{ // {
if (Ures) // if (Ures)
{ // {
throw new NincsElemKivetel(); // throw new NincsElemKivetel();
} // }
else // else
{ // {
if (teteje.elozo == null) // if (teteje.elozo == null)
{ // {
T ertek = fej.tart; // T ertek = fej.tart;
fej = null; // fej = null;
teteje = fej; // teteje = fej;
return ertek; // return ertek;
} // }
else // else
{ // {
T ertek = Felso(); // T ertek = Felso();
teteje.elozo.kov = null; // teteje.elozo.kov = null;
teteje = teteje.elozo; // teteje = teteje.elozo;
return ertek; // return ertek;
} // }
} // }
} // }
} // }
public class LancoltSor<T> : Sor<T> // public class LancoltSor<T> : Sor<T>
{ // {
LancElem<T>? fej; // LancElem<T>? fej;
LancElem<T>? vege; // LancElem<T>? vege;
public bool Ures // public bool Ures
{ // {
get { return ( vege == null); } // get { return ( vege == null); }
} // }
public LancoltSor() // public LancoltSor()
{ // {
} // }
public void Felszabadit() // public void Felszabadit()
{ // {
} // }
public T Elso() // public T Elso()
{ // {
return fej.tart; // return fej.tart;
} // }
public void Sorba(T ertek) // public void Sorba(T ertek)
{ // {
if (Ures) // if (Ures)
{ // {
fej = new LancElem<T>(ertek, null, null); // fej = new LancElem<T>(ertek, null, null);
vege = fej; // vege = fej;
} // }
else // else
{ // {
LancElem<T>? tmp = fej; // LancElem<T>? tmp = fej;
while (tmp.kov != null) // while (tmp.kov != null)
{ // {
tmp = tmp.kov; // tmp = tmp.kov;
} // }
tmp.kov = new LancElem<T>(ertek, null, tmp); // tmp.kov = new LancElem<T>(ertek, null, tmp);
vege = tmp.kov; // vege = tmp.kov;
} // }
} // }
public T Sorbol() // public T Sorbol()
{ // {
if (Ures) // if (Ures)
{ // {
throw new NincsElemKivetel(); // throw new NincsElemKivetel();
} // }
else // else
{ // {
if (vege.elozo == null) // if (vege.elozo == null)
{ // {
T ertek = vege.tart; // T ertek = vege.tart;
vege = null; // vege = null;
return ertek; // return ertek;
} // }
else // else
{ // {
T ertek = fej.tart; // T ertek = fej.tart;
fej = fej.kov; // fej = fej.kov;
fej.elozo = null; // fej.elozo = null;
return ertek; // return ertek;
} // }
} // }
} // }
} // }
public class LancoltLista<T> : Lista<T>, IEnumerable<T> // public class LancoltLista<T> : Lista<T>, IEnumerable<T>
{ // {
int n; // int n;
LancElem<T>? fej; // LancElem<T>? fej;
public int Elemszam // public int Elemszam
{ // {
get { return n; } // get { return n; }
} // }
public LancoltLista() // public LancoltLista()
{ // {
} // }
public void Felszabadit() // public void Felszabadit()
{ // {
} // }
public void Bejar(Action<T> muvelet) // public void Bejar(Action<T> muvelet)
{ // {
for (int i = 0; i < n; i++) // for (int i = 0; i < n; i++)
{ // {
muvelet(Indexedik(i).tart); // muvelet(Indexedik(i).tart);
} // }
} // }
public void Beszur(int index, T ertek) // public void Beszur(int index, T ertek)
{ // {
if (fej == null) // if (fej == null)
{ // {
if (index == 0) // if (index == 0)
{ // {
Hozzafuz(ertek); // Hozzafuz(ertek);
} // }
else // else
{ // {
throw new HibasIndexKivetel(); // throw new HibasIndexKivetel();
} // }
} // }
else // else
{ // {
if (index == 0) // if (index == 0)
{ // {
fej.elozo = new LancElem<T>(ertek, fej, null); // fej.elozo = new LancElem<T>(ertek, fej, null);
fej = fej.elozo; // fej = fej.elozo;
n++; // n++;
} // }
else if (index == n) // else if (index == n)
{ // {
Hozzafuz(ertek); // Hozzafuz(ertek);
n++; // n++;
} // }
else // else
{ // {
LancElem<T>? xd = Indexedik(index).elozo; // LancElem<T>? xd = Indexedik(index).elozo;
xd.kov = new LancElem<T>(ertek, Indexedik(index), xd.kov); // xd.kov = new LancElem<T>(ertek, Indexedik(index), xd.kov);
xd.kov.kov.elozo = xd.kov; // xd.kov.kov.elozo = xd.kov;
n++; // n++;
} // }
} // }
} // }
public void Hozzafuz(T ertek) // public void Hozzafuz(T ertek)
{ // {
if (Elemszam < 1) // if (Elemszam < 1)
{ // {
fej = new LancElem<T>(ertek, null, null); // fej = new LancElem<T>(ertek, null, null);
n++; // n++;
} // }
else // else
{ // {
LancElem<T> asd = Indexedik(n-1); // LancElem<T> asd = Indexedik(n-1);
asd.kov = new LancElem<T>(ertek, null, asd); // asd.kov = new LancElem<T>(ertek, null, asd);
n++; // n++;
} // }
} // }
public T Kiolvas(int index) // public T Kiolvas(int index)
{ // {
return Indexedik(index).tart; // return Indexedik(index).tart;
} // }
public void Modosit(int index, T ertek) // public void Modosit(int index, T ertek)
{ // {
Indexedik(index).tart = ertek; // Indexedik(index).tart = ertek;
} // }
public void Torol(T ertek) // public void Torol(T ertek)
{ // {
LancElem<T> tmp = fej; // LancElem<T> tmp = fej;
while (tmp.kov != null) // while (tmp.kov != null)
{ // {
if (ertek.Equals(tmp.tart)) // if (ertek.Equals(tmp.tart))
{ // {
tmp.elozo.kov = tmp.kov; // tmp.elozo.kov = tmp.kov;
tmp.kov.elozo = tmp.elozo; // tmp.kov.elozo = tmp.elozo;
} // }
tmp = tmp.kov; // tmp = tmp.kov;
} // }
} // }
private LancElem<T> Indexedik(int index) // private LancElem<T> Indexedik(int index)
{ // {
LancElem<T>? tmp = fej; // LancElem<T>? tmp = fej;
for (int i = 0; i < index; i++) // for (int i = 0; i < index; i++)
{ // {
tmp = tmp.kov; // tmp = tmp.kov;
} // }
return tmp; // return tmp;
} // }
public IEnumerator<T> GetEnumerator() // public IEnumerator<T> GetEnumerator()
{ // {
LancElem<T> tmp = fej; // LancElem<T> tmp = fej;
while(tmp != null) // while(tmp != null)
{ // {
if (tmp.tart != null) // if (tmp.tart != null)
{ // {
yield return tmp.tart; // yield return tmp.tart;
} // }
tmp = tmp.kov; // tmp = tmp.kov;
} // }
} // }
IEnumerator IEnumerable.GetEnumerator() // IEnumerator IEnumerable.GetEnumerator()
{ // {
LancoltListaBejaro<T> bejaro = new LancoltListaBejaro<T>(this.fej); // LancoltListaBejaro<T> bejaro = new LancoltListaBejaro<T>(this.fej);
return bejaro; // return bejaro;
} // }
} // }
class LancoltListaBejaro<T> : IEnumerator<T> // class LancoltListaBejaro<T> : IEnumerator<T>
{ // {
LancElem<T> fej; // LancElem<T> fej;
LancElem<T> aktualisElem; // LancElem<T> aktualisElem;
public T Current // public T Current
{ // {
get{ return aktualisElem.tart; } // get{ return aktualisElem.tart; }
} // }
object IEnumerator.Current => Current; // object IEnumerator.Current => Current;
public LancoltListaBejaro(LancElem<T> fej) // public LancoltListaBejaro(LancElem<T> fej)
{ // {
this.fej = fej; // this.fej = fej;
aktualisElem = fej; // aktualisElem = fej;
} // }
public void Dispose() // public void Dispose()
{ // {
throw new NotImplementedException(); // throw new NotImplementedException();
} // }
public bool MoveNext() // public bool MoveNext()
{ // {
if (aktualisElem.kov == null) // if (aktualisElem.kov == null)
{ // {
return false; // return false;
} // }
{ // {
aktualisElem = aktualisElem.kov; // aktualisElem = aktualisElem.kov;
return true; // return true;
} // }
} // }
public void Reset() // public void Reset()
{ // {
aktualisElem = fej; // aktualisElem = fej;
} // }
} // }

View File

@@ -1,40 +1,137 @@
using System; // using System;
// using System.Drawing;
namespace OE.ALGA; // namespace OE.ALGA;
public class FaElem<T> where T : IComparable // internal class FaElem<T> where T : IComparable
{ // {
public T tart; // public T? tart;
public FaElem<T>? bal; // public FaElem<T>? bal;
public FaElem<T>? jobb; // public FaElem<T>? jobb;
public FaElem(T tart, FaElem<T>? bal, FaElem<T>? jobb) // public FaElem(T? tart, FaElem<T>? bal, FaElem<T>? jobb)
{ // {
this.tart = tart; // this.tart = tart;
this.bal = bal; // this.bal = bal;
this.jobb = jobb; // this.jobb = jobb;
} // }
} // }
public class FaHalmaz<T> : Halmaz<T> where T : IComparable // public class FaHalmaz<T> : Halmaz<T> where T : IComparable
{ // {
FaElem<T>? gyoker; // FaElem<T>? gyoker;
public void Bejar(Action<T> muvelet) // public void Bejar(Action<T> muvelet)
{ // {
throw new NotImplementedException(); // throw new NotImplementedException();
} // }
public void Beszur(T ertek) // private static FaElem<T> ReszfabaBeszur(FaElem<T> p, T ertek)
{ // {
throw new NotImplementedException(); // if (p == null)
} // {
// FaElem<T> uj = new FaElem<T>(ertek, null, null);
// return uj;
// }
// else
// {
// if (p.tart.CompareTo(ertek) > 0)
// {
// p.bal = ReszfabaBeszur(p.bal, ertek);
// }
// else
// {
// if (p.tart.CompareTo(ertek) < 0)
// {
// p.jobb = ReszfabaBeszur(p.jobb, ertek);
// }
// }
// return p;
// }
// }
// public void Beszur(T ertek)
// {
// gyoker = ReszfabaBeszur(gyoker, ertek);
// }
public bool Eleme(T ertek) // private static FaElem<T> ReszfaEleme(FaElem<T> p, T ertek)
{ // {
throw new NotImplementedException(); // FaElem<T>? tmp = null;
} // if (p == null)
// {
// return tmp;
// }
// else
// {
// if (p.tart.CompareTo(ertek) > 0)
// {
// tmp = ReszfaEleme(p.bal, ertek);
// }
// else if (p.tart.CompareTo(ertek) < 0)
// {
// tmp = ReszfabaBeszur(p.jobb, ertek);
// }
// else
// {
// return tmp;
// }
// return tmp;
// }
// }
// public bool Eleme(T ertek)
// {
// return !(ReszfaEleme(gyoker, ertek) == null);
// }
// private static FaElem<T> KetGyerekesTorles(FaElem<T> p, FaElem<T> ertek)
// {
// if (p.jobb != null)
// {
// p.jobb = KetGyerekesTorles(p.jobb, ertek);
// return p;
// }
// else
// {
// ertek.tart = p.tart;
// p = p.bal;
// return p;
public void Torol(T ertek) // }
{ // }
throw new NotImplementedException(); // private static FaElem<T> ReszfabolTorol(FaElem<T> p, T ertek)
} // {
} // if (p == null)
// {
// throw new NincsElemKivetel();
// }
// if (p.tart.CompareTo(ertek) < 0)
// {
// p.bal = ReszfabolTorol(p.bal, ertek);
// }
// else if (p.tart.CompareTo(ertek) > 0)
// {
// p.jobb = ReszfabolTorol(p.jobb, ertek);
// }
// else
// {
// if (p.jobb == null && p.bal == null)
// {
// p = null;
// }
// else if (p.jobb == null && p.bal != null)
// {
// p = p.bal;
// }
// else if (p.jobb != null && p.bal == null)
// {
// p = p.jobb;
// }
// else
// {
// p = KetGyerekesTorles(p, p);
// }
// }
// return p;
// }
// public void Torol(T ertek)
// {
// ReszfabolTorol(gyoker, ertek);
// }
// }

View File

@@ -4,5 +4,271 @@ using System.Collections.Generic;
namespace OE.ALGA.Adatszerkezetek namespace OE.ALGA.Adatszerkezetek
{ {
// 3. heti labor feladat - Tesztek: 03_TombImplementacioTesztek.cs public class TombVerem<T> : Verem<T>
{
T[] tomb;
int n = 0;
public TombVerem(int l)
{
tomb = new T[l];
}
public bool Ures
{
get { return n == 0; }
}
public T Felso()
{
return tomb[n - 1];
}
public void Verembe(T ertek)
{
if (n < tomb.Length)
{
tomb[n] = ertek;
n++;
}
else
{
throw new NincsHelyKivetel();
}
}
public T Verembol()
{
if (Ures)
{
throw new NincsElemKivetel();
}
else
{
T ertek = tomb[n - 1];
n--;
return ertek;
}
}
}
public class TombSor<T> : Sor<T>
{
T[] tomb;
int n = 0;
public TombSor(int l)
{
tomb = new T[l];
}
public bool Ures
{
get { return n == 0; }
}
public T Elso()
{
return tomb[0];
}
public void Sorba(T ertek)
{
if (n < tomb.Length)
{
tomb[n] = ertek;
n++;
}
else
{
throw new NincsHelyKivetel();
}
}
public T Sorbol()
{
if (Ures)
{
throw new NincsElemKivetel();
}
else
{
T ertek = tomb[0];
T[] tmp = new T[tomb.Length];
n--;
for (int i = 1; i < tomb.Length; i++)
{
tmp[i - 1] = tomb[i];
}
tomb = tmp;
return ertek;
}
}
}
public class TombLista<T> : Lista<T>, IEnumerable<T>
{
T[] tomb;
int n = 0;
public bool Ures
{
get { return n <= 0; }
}
public TombLista()
{
tomb = new T[1];
}
public int Elemszam
{
get { return n; }
}
public void Bejar(Action<T> muvelet)
{
for (int i = 0; i < n; i++)
{
muvelet?.Invoke(tomb[i]);
}
}
public void Beszur(int index, T ertek)
{
if (index < 0)
{
throw new HibasIndexKivetel();
}
else
{
T[] tmp = new T[tomb.Length * 2];
for (int i = 0; i < index; i++)
{
tmp[i] = tomb[i];
}
tmp[index] = ertek;
for (int i = index + 1; i < tomb.Length; i++)
{
tmp[i] = tomb[i - 1];
}
tomb = tmp;
n++;
}
}
public IEnumerator<T> GetEnumerator()
{
foreach (T t in tomb)
{
if (t != null)
{
yield return t;
}
}
}
public void Hozzafuz(T ertek)
{
if (n < tomb.Length)
{
tomb[n] = ertek;
n++;
}
else
{
T[] tmp = new T[tomb.Length * 2];
for (int i = 0; i < tomb.Length; i++)
{
tmp[i] = tomb[i];
}
tmp[n] = ertek;
tomb = tmp;
n++;
}
}
public T Kiolvas(int index)
{
if (index < 0 || index >= tomb.Length)
{
throw new HibasIndexKivetel();
}
return tomb[index];
}
public void Modosit(int index, T ertek)
{
if (index < 0 && index < tomb.Length)
{
throw new HibasIndexKivetel();
}
else
{
tomb[index] = ertek;
}
}
public void Torol(T ertek)
{
int m = n;
T[] tmp = new T[tomb.Length];
int index = 0;
for (int i = 0; i < m; i++)
{
if (ertek.Equals(tomb[i]))
{
n--;
continue;
}
tmp[index] = tomb[i];
index++;
}
tomb = tmp;
}
IEnumerator IEnumerable.GetEnumerator()
{
TombListaBejaro<T> bejaro = new TombListaBejaro<T>(tomb, n);
return bejaro;
}
}
public class TombListaBejaro<T> : IEnumerator<T>
{
T[] e;
int n;
int aktualisindex;
T current;
public T Current
{
get { return current; }
}
public TombListaBejaro(T[] e, int n)
{
this.n = n;
this.e = e;
aktualisindex = 0;
current = e[0];
}
object IEnumerator.Current => Current;
public void Dispose()
{
throw new NotImplementedException();
}
public bool MoveNext()
{
if (aktualisindex < n - 1)
{
aktualisindex++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
aktualisindex = -1;
}
}
} }

View File

@@ -4,5 +4,322 @@ using System.Collections.Generic;
namespace OE.ALGA.Adatszerkezetek namespace OE.ALGA.Adatszerkezetek
{ {
// 4. heti labor feladat - Tesztek: 04_LancoltImplementacioTesztek.cs public class LancElem<T>
{
public T tart;
public LancElem<T>? kov;
public LancElem<T>? elozo;
public LancElem(T tart, LancElem<T>? kov, LancElem<T>? elozo)
{
this.tart = tart;
this.kov = kov;
this.elozo = elozo;
}
}
public class LancoltVerem<T> : Verem<T>
{
LancElem<T>? fej;
LancElem<T>? teteje;
public LancoltVerem()
{
}
public void Felszabadit()
{
}
public bool Ures
{
get { return fej == null; }
}
public T Felso()
{
if (Ures)
{
throw new NincsElemKivetel();
}
else
{
LancElem<T>? tmp = fej;
while (tmp.kov != null)
{
tmp = tmp.kov;
}
teteje = tmp;
return tmp.tart;
}
}
public void Verembe(T ertek)
{
if (Ures)
{
fej = new LancElem<T>(ertek, null, null);
teteje = fej;
}
else
{
teteje.kov = new LancElem<T>(ertek, null, teteje);
teteje = teteje.kov;
}
}
public T Verembol()
{
if (Ures)
{
throw new NincsElemKivetel();
}
else
{
if (teteje.elozo == null)
{
T ertek = fej.tart;
fej = null;
teteje = fej;
return ertek;
}
else
{
T ertek = Felso();
teteje.elozo.kov = null;
teteje = teteje.elozo;
return ertek;
}
}
}
}
public class LancoltSor<T> : Sor<T>
{
LancElem<T>? fej;
LancElem<T>? vege;
public bool Ures
{
get { return (vege == null); }
}
public LancoltSor()
{
}
public void Felszabadit()
{
}
public T Elso()
{
return fej.tart;
}
public void Sorba(T ertek)
{
if (Ures)
{
fej = new LancElem<T>(ertek, null, null);
vege = fej;
}
else
{
LancElem<T>? tmp = fej;
while (tmp.kov != null)
{
tmp = tmp.kov;
}
tmp.kov = new LancElem<T>(ertek, null, tmp);
vege = tmp.kov;
}
}
public T Sorbol()
{
if (Ures)
{
throw new NincsElemKivetel();
}
else
{
if (vege.elozo == null)
{
T ertek = vege.tart;
vege = null;
return ertek;
}
else
{
T ertek = fej.tart;
fej = fej.kov;
fej.elozo = null;
return ertek;
}
}
}
}
public class LancoltLista<T> : Lista<T>, IEnumerable<T>
{
int n;
LancElem<T>? fej;
public int Elemszam
{
get { return n; }
}
public LancoltLista()
{
}
public void Felszabadit()
{
}
public void Bejar(Action<T> muvelet)
{
for (int i = 0; i < n; i++)
{
muvelet(Indexedik(i).tart);
}
}
public void Beszur(int index, T ertek)
{
if (fej == null)
{
if (index == 0)
{
Hozzafuz(ertek);
}
else
{
throw new HibasIndexKivetel();
}
}
else
{
if (index == 0)
{
fej.elozo = new LancElem<T>(ertek, fej, null);
fej = fej.elozo;
n++;
}
else if (index == n)
{
Hozzafuz(ertek);
n++;
}
else
{
LancElem<T>? xd = Indexedik(index).elozo;
xd.kov = new LancElem<T>(ertek, Indexedik(index), xd.kov);
xd.kov.kov.elozo = xd.kov;
n++;
}
}
}
public void Hozzafuz(T ertek)
{
if (Elemszam < 1)
{
fej = new LancElem<T>(ertek, null, null);
n++;
}
else
{
LancElem<T> asd = Indexedik(n - 1);
asd.kov = new LancElem<T>(ertek, null, asd);
n++;
}
}
public T Kiolvas(int index)
{
return Indexedik(index).tart;
}
public void Modosit(int index, T ertek)
{
Indexedik(index).tart = ertek;
}
public void Torol(T ertek)
{
LancElem<T> tmp = fej;
while (tmp.kov != null)
{
if (ertek.Equals(tmp.tart))
{
tmp.elozo.kov = tmp.kov;
tmp.kov.elozo = tmp.elozo;
}
tmp = tmp.kov;
}
}
private LancElem<T> Indexedik(int index)
{
LancElem<T>? tmp = fej;
for (int i = 0; i < index; i++)
{
tmp = tmp.kov;
}
return tmp;
}
public IEnumerator<T> GetEnumerator()
{
LancElem<T> tmp = fej;
while (tmp != null)
{
if (tmp.tart != null)
{
yield return tmp.tart;
}
tmp = tmp.kov;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
LancoltListaBejaro<T> bejaro = new LancoltListaBejaro<T>(this.fej);
return bejaro;
}
}
class LancoltListaBejaro<T> : IEnumerator<T>
{
LancElem<T> fej;
LancElem<T> aktualisElem;
public T Current
{
get { return aktualisElem.tart; }
}
object IEnumerator.Current => Current;
public LancoltListaBejaro(LancElem<T> fej)
{
this.fej = fej;
aktualisElem = fej;
}
public void Dispose()
{
throw new NotImplementedException();
}
public bool MoveNext()
{
if (aktualisElem.kov == null)
{
return false;
}
{
aktualisElem = aktualisElem.kov;
return true;
}
}
public void Reset()
{
aktualisElem = fej;
}
}
} }

View File

@@ -1,6 +1,167 @@
using System; using System;
using System.Drawing;
namespace OE.ALGA.Adatszerkezetek namespace OE.ALGA;
internal class FaElem<T> where T : IComparable
{ {
// 5. heti labor feladat - Tesztek: 05_BinarisKeresoFaTesztek.cs public T? tart;
public FaElem<T>? bal;
public FaElem<T>? jobb;
public FaElem(T? tart, FaElem<T>? bal, FaElem<T>? jobb)
{
this.tart = tart;
this.bal = bal;
this.jobb = jobb;
}
}
public class FaHalmaz<T> : Halmaz<T> where T : IComparable
{
FaElem<T>? gyoker;
private void BejarasPreOrder(FaElem<T> p, Action<T> muvelet)
{
if (p != null)
{
muvelet(p.tart);
BejarasPreOrder(p.bal, muvelet);
BejarasPreOrder(p.jobb, muvelet);
}
}
private void BejarasInOrder(FaElem<T> p, Action<T> muvelet)
{
if (p != null)
{
BejarasInOrder(p.bal, muvelet);
muvelet(p.tart);
BejarasInOrder(p.jobb, muvelet);
}
}
private void BejarasPostOrder(FaElem<T> p, Action<T> muvelet)
{
if (p != null)
{
BejarasPostOrder(p.bal, muvelet);
BejarasPostOrder(p.jobb, muvelet);
muvelet(p.tart);
}
}
public void Bejar(Action<T> muvelet)
{
BejarasPreOrder(gyoker, muvelet);
}
private static FaElem<T> ReszfabaBeszur(FaElem<T> p, T ertek)
{
if (p == null)
{
FaElem<T> uj = new FaElem<T>(ertek, null, null);
return uj;
}
else
{
if (p.tart.CompareTo(ertek) > 0)
{
p.bal = ReszfabaBeszur(p.bal, ertek);
}
else
{
if (p.tart.CompareTo(ertek) < 0)
{
p.jobb = ReszfabaBeszur(p.jobb, ertek);
}
}
return p;
}
}
public void Beszur(T ertek)
{
gyoker = ReszfabaBeszur(gyoker, ertek);
}
private static FaElem<T> ReszfaEleme(FaElem<T> p, T ertek)
{
FaElem<T>? tmp = null;
if (p == null)
{
return tmp;
}
else
{
if (p.tart.CompareTo(ertek) > 0)
{
tmp = ReszfaEleme(p.bal, ertek);
}
else if (p.tart.CompareTo(ertek) < 0)
{
tmp = ReszfaEleme(p.jobb, ertek);
}
else
{
tmp = p;
}
return tmp;
}
}
public bool Eleme(T ertek)
{
return (ReszfaEleme(gyoker, ertek) != null);
}
private static FaElem<T> KetGyerekesTorles(FaElem<T> p, T ertek)
{
if (p.jobb != null)
{
p.jobb = KetGyerekesTorles(p.jobb, ertek);
return p;
}
else
{
ertek = p.tart;
p = p.bal;
return p;
}
}
private static FaElem<T> ReszfabolTorol(ref FaElem<T> p, T ertek)
{
if (p == null)
{
throw new NincsElemKivetel();
}
if (p.tart.CompareTo(ertek) > 0)
{
p.bal = ReszfabolTorol(ref p.bal, ertek);
}
else if (p.tart.CompareTo(ertek) < 0)
{
p.jobb = ReszfabolTorol(ref p.jobb, ertek);
}
else
{
if (p.jobb == null && p.bal == null)
{
p = null;
}
else if (p.jobb == null && p.bal != null)
{
p = p.bal;
}
else if (p.jobb != null && p.bal == null)
{
p = p.jobb;
}
else
{
p = KetGyerekesTorles(p, p.tart);
}
}
return p;
}
public void Torol(T ertek)
{
if (gyoker != null)
{
ReszfabolTorol(ref gyoker, ertek);
}
}
} }

102
ALGA/Product.cs Normal file
View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace OE.ALGA;
public class Product
{
[Validation(AttributeValidation.NotEmpty)]
public string Name { get; }
[Validation(AttributeValidation.PositiveNumber)]
public decimal Price { get; }
[Validation(AttributeValidation.NonNegative)]
public int Quantity { get; }
[Validation(AttributeValidation.FutureDate)]
public DateTime? Experiation { get; }
public Product(string name, decimal price, int quantity, DateTime? experiarion)
{
Name = name;
Price = price;
Quantity = quantity;
Experiation = experiarion;
}
}
public enum AttributeValidation
{
NotEmpty,
PositiveNumber,
NonNegative,
FutureDate
}
public class ValidationAttribute : Attribute
{
public AttributeValidation Rule { get; }
public ValidationAttribute(AttributeValidation rule)
{
Rule = rule;
}
}
public static class Validator
{
public static void Validate(Product product)
{
bool flag = false;
var t = product.GetType();
foreach (var p in t.GetProperties())
{
var value = p.GetValue(product);
switch (p.GetCustomAttribute<ValidationAttribute>().Rule)
{
case AttributeValidation.NotEmpty:
if (Convert.ToString(value) == "")
{
flag = true;
}
break;
case AttributeValidation.PositiveNumber:
if (Convert.ToInt32(value) <= 0)
{
flag = true;
}
break;
case AttributeValidation.NonNegative:
if (Convert.ToInt32(value) < 0)
{
flag = true;
}
break;
case AttributeValidation.FutureDate:
if (Convert.ToDateTime(value) < DateTime.Now)
{
flag = true;
}
break;
}
}
if (flag)
{
throw new WrongProductException();
}
else
{
Console.WriteLine("Product added successfuly");
}
}
}
public class WrongProductException : Exception
{
}

View File

@@ -1,125 +1,125 @@
//using NUnit.Framework; using NUnit.Framework;
//using OE.ALGA.Adatszerkezetek; using OE.ALGA.Adatszerkezetek;
//namespace OE.ALGA.Tesztek.Adatszerkezetek namespace OE.ALGA.Tesztek.Adatszerkezetek
//{ {
// [TestFixture(Category = "Adatszerkezetek", TestName = "05 - Fa Halmaz Tesztek")] [TestFixture(Category = "Adatszerkezetek", TestName = "05 - Fa Halmaz Tesztek")]
// public class FaHalmazTesztek public class FaHalmazTesztek
// { {
// [TestCase] [TestCase]
// public void BeszurasUres() public void BeszurasUres()
// { {
// Halmaz<string> v = new FaHalmaz<string>(); Halmaz<string> v = new FaHalmaz<string>();
// v.Beszur(""); v.Beszur("");
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(""), Is.True); Assert.That(v.Eleme(""), Is.True);
// }); });
// } }
// [TestCase] [TestCase]
// public void Beszuras() public void Beszuras()
// { {
// Halmaz<int> v = new FaHalmaz<int>(); Halmaz<int> v = new FaHalmaz<int>();
// v.Beszur(1); v.Beszur(1);
// v.Beszur(3); v.Beszur(3);
// v.Beszur(2); v.Beszur(2);
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(1), Is.True); Assert.That(v.Eleme(1), Is.True);
// Assert.That(v.Eleme(2), Is.True); Assert.That(v.Eleme(2), Is.True);
// Assert.That(v.Eleme(3), Is.True); Assert.That(v.Eleme(3), Is.True);
// }); });
// } }
// [TestCase] [TestCase]
// public void DuplaBeszuras() public void DuplaBeszuras()
// { {
// Halmaz<int> v = new FaHalmaz<int>(); Halmaz<int> v = new FaHalmaz<int>();
// v.Beszur(1); v.Beszur(1);
// v.Beszur(2); v.Beszur(2);
// v.Beszur(3); v.Beszur(3);
// v.Beszur(2); v.Beszur(2);
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(1), Is.True); Assert.That(v.Eleme(1), Is.True);
// Assert.That(v.Eleme(2), Is.True); Assert.That(v.Eleme(2), Is.True);
// Assert.That(v.Eleme(3), Is.True); Assert.That(v.Eleme(3), Is.True);
// }); });
// } }
// [TestCase] [TestCase]
// public void Torles() public void Torles()
// { {
// Halmaz<int> v = new FaHalmaz<int>(); Halmaz<int> v = new FaHalmaz<int>();
// v.Beszur(1); v.Beszur(1);
// v.Beszur(3); v.Beszur(3);
// v.Beszur(2); v.Beszur(2);
// v.Torol(2); v.Torol(2);
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(1), Is.True); Assert.That(v.Eleme(1), Is.True);
// Assert.That(v.Eleme(2), Is.False); Assert.That(v.Eleme(2), Is.False);
// Assert.That(v.Eleme(3), Is.True); Assert.That(v.Eleme(3), Is.True);
// }); });
// } }
// [TestCase] [TestCase]
// public void TorlesUres() public void TorlesUres()
// { {
// Halmaz<string> v = new FaHalmaz<string>(); Halmaz<string> v = new FaHalmaz<string>();
// v.Beszur(""); v.Beszur("");
// v.Beszur("1"); v.Beszur("1");
// v.Beszur(""); v.Beszur("");
// v.Torol(""); v.Torol("");
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(""), Is.False); Assert.That(v.Eleme(""), Is.False);
// Assert.That(v.Eleme("1"), Is.True); Assert.That(v.Eleme("1"), Is.True);
// }); });
// } }
// [TestCase] [TestCase]
// public void NemletezoTorles() public void NemletezoTorles()
// { {
// Halmaz<int> v = new FaHalmaz<int>(); Halmaz<int> v = new FaHalmaz<int>();
// v.Beszur(1); v.Beszur(1);
// v.Beszur(3); v.Beszur(3);
// v.Beszur(2); v.Beszur(2);
// Assert.Throws<NincsElemKivetel>(() => v.Torol(0)); Assert.Throws<NincsElemKivetel>(() => v.Torol(0));
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(1), Is.True); Assert.That(v.Eleme(1), Is.True);
// Assert.That(v.Eleme(2), Is.True); Assert.That(v.Eleme(2), Is.True);
// Assert.That(v.Eleme(3), Is.True); Assert.That(v.Eleme(3), Is.True);
// }); });
// } }
// [TestCase] [TestCase]
// public void DuplaTorles() public void DuplaTorles()
// { {
// Halmaz<int> v = new FaHalmaz<int>(); Halmaz<int> v = new FaHalmaz<int>();
// v.Beszur(1); v.Beszur(1);
// v.Beszur(2); v.Beszur(2);
// v.Beszur(3); v.Beszur(3);
// v.Beszur(2); v.Beszur(2);
// v.Torol(2); v.Torol(2);
// Assert.Multiple(() => Assert.Multiple(() =>
// { {
// Assert.That(v.Eleme(1), Is.True); Assert.That(v.Eleme(1), Is.True);
// Assert.That(v.Eleme(2), Is.False); Assert.That(v.Eleme(2), Is.False);
// Assert.That(v.Eleme(3), Is.True); Assert.That(v.Eleme(3), Is.True);
// Assert.That(v.Eleme(4), Is.False); Assert.That(v.Eleme(4), Is.False);
// }); });
// } }
// [TestCase] [TestCase]
// public void PreorderBejaras() public void PreorderBejaras()
// { {
// Halmaz<int> v = new FaHalmaz<int>(); Halmaz<int> v = new FaHalmaz<int>();
// v.Beszur(5); v.Beszur(5);
// v.Beszur(3); v.Beszur(3);
// v.Beszur(1); v.Beszur(1);
// v.Beszur(8); v.Beszur(8);
// v.Beszur(4); v.Beszur(4);
// v.Beszur(9); v.Beszur(9);
// v.Beszur(7); v.Beszur(7);
// string osszefuzo = ""; string osszefuzo = "";
// v.Bejar(x => osszefuzo += x); v.Bejar(x => osszefuzo += x);
// Assert.That(osszefuzo, Is.EqualTo("5314879")); Assert.That(osszefuzo, Is.EqualTo("5314879"));
// } }
// } }
//} }