//Element x sortiert hinzufügen public void AddSorted(int x) { LElement newElement = new LElement(x); if (root == null) //Wenn Liste leer ist { root = newElement; return; } //Vor dem ersten x einfügen, wenn neues x kleiner als erstes x der Liste if (newElement.x <= root.x) { newElement.next = root; root = newElement; return; } //Stelle zum Einfügen suchen LElement current = root; while (current.next != null && newElement.x >= current.x) // nur wenn Liste bereits existiert & neues Element größer als Anfangselement ist { current = current.next; } newElement.next = current.next; //um einen Platz in der Liste frei zu machen current.next = newElement; //Element an diesem Platz einfügen }
public void Hinzufügen(string name, int anzahl) { if (!SucheArtikel(name, anzahl)) { //Vorne einfügen: LElement neu = new LElement(name, anzahl); neu.next = root; root = neu; } }
public override string ToString() { string s = ""; for (LElement tmp = root; tmp != null; tmp = tmp.next) { s += $"Name: {tmp.name}, Anzahl: {tmp.anzahl}\n"; } return(s); }
/// <summary> /// Ist der Name in der Liste vorhanden wird die Anzahl im LElement erhöht /// </summary> /// <param name="name"></param> /// <returns>Name gefunden und anzahl erhöht oder nicht gefunden</returns> private bool SucheArtikel(string name, int anzahl) { for (LElement tmp = root; tmp != null; tmp = tmp.next) { if (tmp.name == name) { tmp.anzahl += anzahl; return(true); } } return(false); }
public Liste Bestellung(Auswahl a) { Liste l = new Liste(); for (LElement tmp = root; tmp != null; tmp = tmp.next) { if (a(tmp.anzahl)) { l.Hinzufügen(tmp.name, tmp.anzahl); } } return(l); }
public bool Search_Help(int elem) { LElement current = root; while (current != null) { if (current.element == elem) { return(true); } current = current.next; } return(false); }
// nach Element x suchen public bool Search(int x) { LElement current = root; while (current != null) //Wenn Liste nicht leer ist { if (current.x == x) { return(true); } current = current.next; } return(false); }
protected void _search_(int elem) { prev = null; LElement current = root; if (Search_Help(elem) == true) // wenn element in Liste existiert { if (elem == root.element) { } else { while (current != null) // wenn element nicht am anfang { if (current.next.element == elem) { prev = current; break; } current = current.next; } } } else // wenn element nicht in Liste ( für sorte liste) { if (root == null) { } else { if (root.element > elem) //falls kleiner als root { } else { while (current != null) // { if (current.next == null || current.next.element > elem) { prev = current; break; } current = current.next; } } } } }
public bool Search(int elem) { LElement current = root; while (current != null) { if (current.element == elem) { Console.WriteLine("found"); return(true); } current = current.next; } Console.WriteLine("not found"); return(false); }
//Einfügen als letztes Element public void Enque(int x) { if (root == null) //Wenn Liste leer ist { root = new LElement(x); //als erstes Element einfügen } else { LElement current = root; while (current.next != null) //Liste so lange durchgehen, bis man beim letzten Element ist { current = current.next; } current.next = new LElement(x); //hinter letztes Element einfügen } }
//komplette Liste ausgeben public void Print() { LElement current = root; if (current == null) //wenn Liste leer ist { Console.WriteLine(" Leere Liste "); return; } while (current.next != null) //bis ein Element kein Nachfolger mehr hat { Console.WriteLine(current.x); current = current.next; } Console.WriteLine(current.x); }
public bool Insert(int elem) { if (root == null) { root = new LElement(elem); } else { LElement current = root; while (current.next != null) { current = current.next; } current.next = new LElement(elem); } return(false); }
public bool Insert(int elem) { LElement newelem = new LElement(elem); _search_(elem); if (prev == null) { newelem.next = root; root = newelem; } else { newelem.next = prev.next; prev.next = newelem; } return(true); }
public void Print() { Console.WriteLine(); Console.WriteLine(); LElement current = root; if (current == null) { Console.WriteLine(" Liste ist leer "); return; } while (current.next != null) { Console.Write(" " + current.element + ","); current = current.next; } Console.WriteLine(" " + current.element); }
public bool Delete(int elem) { if (Search(elem) == true) { _search_(elem); if (prev == null) { root = root.next; return(true); } else { prev.next = prev.next.next; return(true); } } return(false); }
public new bool Insert(int elem) { if (Search_Help(elem) == true)//element enthalten { return(false); } else { LElement newelem = new LElement(elem); _search_(elem); if (prev == null) { newelem.next = root; root = newelem; } else { newelem.next = prev.next; prev.next = newelem; } return(true); } }
abstract public bool Insert(int x); //damit alle Unterklassen eine eigene Methode Insert definieren müssen //löschen eines Elementes public bool Delete(int x) { if (Search(x) == true) //wenn x gefunden wurde { LElement current = root; LElement previous = null; while (current.next != null && x != current.x) { previous = current; current = current.next; } if (current.x == root.x) //wenn es das erste Element ist { root = root.next; //dann wird das Zweite zum Ersten } else { previous.next = current.next; //hier wird gelöscht } return(true); } return(false); }
public LElement(int element) { this.element = element; next = null; }
LElement root; //erstes Element der Liste public SupportList() { root = null; }
public LElement(int x) { this.x = x; next = null; }