Skip to content
Snippets Groups Projects
Commit b3faf04a authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

2020-05-02

parent 23da5085
No related branches found
No related tags found
No related merge requests found
Showing
with 200 additions and 36 deletions
No preview for this file type
No preview for this file type
...@@ -6,40 +6,91 @@ namespace _02_UebgSa_FiFoListe ...@@ -6,40 +6,91 @@ namespace _02_UebgSa_FiFoListe
{ {
class Element class Element
{ {
public string name;
public Element next;
public Element(string name) { this.name = name; }
public override string ToString() => name;
} }
Element anf, ende; Element anf, ende;
int anz; int anz;
public FiFoListe() public FiFoListe()
{ {
anf = ende = null;
anz = 0;
} }
/// <summary>
/// Fügt der Warteschlange ein neues Element hinzu
/// </summary>
/// <param name="text"></param>
public void Push(string text) public void Push(string text)
{ {
Element neuesElement = new Element(text);
anz++;
if (anf == null) // Noch leere Liste?
anf = ende = neuesElement;
else
ende = ende.next = neuesElement;
} }
public void Push(params string[] texte) /// <summary>
///
/// </summary>
/// <param name="textFeld"></param>
public void Push(params string[] textFeld)
{ {
foreach (string text in textFeld)
Push(text);
} }
/// <summary>
/// Liefert das erste Element zurück. Es bleibt aber in der Warteschlange
/// </summary>
/// <returns></returns>
public string First() public string First()
{ {
return (anf == null) ? null : anf.name;
} }
public string Pop() /// <summary>
/// Liefert Wert des ersten Ausgabeelements zurück UND entfernt dieses aus der Warteschlange
/// </summary>
/// <returns></returns>
public string Pop() //
{ {
// 1.Fall: Liste ist leer
// 2.Fall: Liste besteht nur aus *einem* Element
// 3.Fall: Liste hat mehr als ein Element
if (anf == null) // 1. Fall
return null;
string erg = anf.name; // Ein return anf.name würde die Methode zu früh beenden!!!
anz--;
anf = anf.next; // 3. Fall
if (anf == null) // 2. Fall
ende = null;
return erg;
} }
/// <summary>
/// Gibt alle Elemente der Warteschlange auf dem Bildschirm aus
/// </summary>
public void WriteAll() public void WriteAll()
{ {
for (Element tmp = anf; tmp != null; tmp = tmp.next)
Console.WriteLine(tmp);
} }
/// <summary>
/// liefert ein string-Feld mit allen gespeicherten Elementen zurück;
/// Elemente verbleiben in der Liste
/// </summary>
/// <returns></returns>
public string[] GetAll() public string[] GetAll()
{ {
string[] textFeld = new string[anz];
int i = 0;
for (Element tmp = anf; tmp != null; tmp = tmp.next,i++)
textFeld[i] = tmp.name;
return textFeld;
} }
public int ItemCount { } /// <summary>
/// Anzahl der Elemente in der Warteschlange zurückgeben
/// </summary>
public int ItemCount { get => anz; }
} }
class Program class Program
...@@ -57,9 +108,10 @@ namespace _02_UebgSa_FiFoListe ...@@ -57,9 +108,10 @@ namespace _02_UebgSa_FiFoListe
Console.WriteLine($"Pop: {f.Pop()}"); Console.WriteLine($"Pop: {f.Pop()}");
Console.WriteLine($"Anzahl der Elemente in der Liste: {f.ItemCount}"); Console.WriteLine($"Anzahl der Elemente in der Liste: {f.ItemCount}");
Console.WriteLine("--------------"); Console.WriteLine("--------------");
foreach (string item in f.GetAll()) string[] namen = f.GetAll();
foreach (string item in namen)
{ {
Console.WriteLine(item); Console.WriteLine($"foreach: {item}");
} }
} }
} }
......
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v3.1",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v3.1": {
"02 UebgSa FiFoListe/1.0.0": {
"runtime": {
"02 UebgSa FiFoListe.dll": {}
}
}
}
},
"libraries": {
"02 UebgSa FiFoListe/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
\ No newline at end of file
File added
File added
File added
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\wienkop\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\wienkop\\.nuget\\packages"
]
}
}
\ No newline at end of file
{
"runtimeOptions": {
"tfm": "netcoreapp3.1",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "3.1.0"
}
}
}
\ No newline at end of file
c0523642d3f3714f80c0cf31b3f8720f51a0bc09
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.csprojAssemblyReference.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.csproj.CoreCompileInputs.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.AssemblyInfoInputs.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.AssemblyInfo.cs
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\bin\Debug\netcoreapp3.1\02 UebgSa FiFoListe.exe
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\bin\Debug\netcoreapp3.1\02 UebgSa FiFoListe.deps.json
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\bin\Debug\netcoreapp3.1\02 UebgSa FiFoListe.runtimeconfig.json
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\bin\Debug\netcoreapp3.1\02 UebgSa FiFoListe.runtimeconfig.dev.json
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\bin\Debug\netcoreapp3.1\02 UebgSa FiFoListe.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\bin\Debug\netcoreapp3.1\02 UebgSa FiFoListe.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02 UebgSa FiFoListe\obj\Debug\netcoreapp3.1\02 UebgSa FiFoListe.genruntimeconfig.cache
File added
File added
86c8e15dd33445635927cfaf398408205fd11473
File added
using System; using System;
using System.Collections;
namespace _02VerketteteListe_2 namespace _02VerketteteListe_2
{ {
class StringListe class StringListe
{ {
// Die Klasse Element ist nun eine *private* Klasse der Klasse StringListe // Die Klasse Element ist nun eine *private* Klasse der Klasse StringListe
...@@ -11,51 +11,94 @@ namespace _02VerketteteListe_2 ...@@ -11,51 +11,94 @@ namespace _02VerketteteListe_2
private class Element // Element ist eine private Hilfsklasse der Klasse StringListe private class Element // Element ist eine private Hilfsklasse der Klasse StringListe
{ {
public string name; public string name;
public Element next = null;
public Element(string name) { this.name = name; } public Element(string name) { this.name = name; }
public Element next = null; // WAS IST NEXT???
public override string ToString() => name; public override string ToString() => name;
} }
Element anf, ende; Element anf=null, ende=null;
// int anz=0; // int anz=0;
public StringListe() { anf = ende = null; } //public StringListe() { }
public void AddEnd(string s) public void AddEnd(string name)
{ {
Element neuesElement = new Element(s); Element neuesElement = new Element(name); // 1. Neues Element anlegen
if (anf == null) // Noch leere Liste? if (anf == null) // 2. Leere Liste?
anf = ende = neuesElement; anf = ende = neuesElement;
else else
{ {
ende.next = neuesElement; ende.next = neuesElement; // 3. Neues Element am Ende anfügen
ende = ende.next; ende = ende.next;
} }
} }
public void AddFront(string name)
{
// 1. Leere Liste
// 2. Es existiert mind. ein Element
Element neuesElement = new Element(name); // Neues Element anlegen
if (anf == null) // 1.Fall: Leere Liste?
anf = ende = neuesElement;
else
{
neuesElement.next = anf; // 2. Fall
anf = neuesElement;
}
}
public bool Suche(string name) public bool Suche(string name)
{ {
for (Element tmp = anf; tmp != null; tmp = tmp.next) for (Element item = anf; item != null; item = item.next)
{ {
if (tmp.name == name) if (item.name == name)
return true; return true;
} }
return false; return false;
} }
public void Print() public void Print()
{ {
for (Element tmp = anf; tmp != null; tmp = tmp.next) for (Element item = anf; item != null; item = item.next)
{
Console.WriteLine(item);
}
}
public IEnumerator GetEnumerator()
{
for (Element item = anf; item != null; item = item.next)
{ {
Console.WriteLine(tmp); yield return item.name; // Merken dieser Ausführungsposition
// UND Zurückliefern von item.name
// Beim nächsten Aufruf von GetEnumerator() wird
// an der gespeicherten Pos. weitergemacht.
} }
} }
public void LoescheAnf() public void DeleteFront()
{ {
// 1.Fall: Liste ist leer // 1.Fall: Liste ist leer
// 2.Fall: Liste besteht nur aus einem Element // 2.Fall: Liste besteht nur aus einem Element
// 3.Fall: Liste hat mehr als ein Element // 3.Fall: Liste hat mehr als ein Element
if (anf == null) // 1. Fall if (anf == null) // 1. Fall
return; return;
if (anf == ende) // 2. Fall
anf = ende = null;
else
anf = anf.next; // 3. Fall anf = anf.next; // 3. Fall
if (anf == null) // 2. Fall }
ende = null; public void DeleteEnd()
{
// 1.Fall: Liste ist leer
// 2.Fall: Liste besteht nur aus einem Element
// 3.Fall: Liste hat mehr als ein Element
if (anf == null) // 1. Fall
throw new NullReferenceException("Die Liste ist leer");
if (anf == ende) // 2. Fall
anf = ende = null;
else // 3. Fall
{ // Wir wissen: Die Liste hat mehr als ein Element, d.h. es gibt ein
// vorletztes Element vorletzter.next == ende
Element vorletzter = anf;
while (vorletzter.next != ende)
vorletzter = vorletzter.next;
vorletzter.next = null;
ende = vorletzter;
}
} }
} }
class Program class Program
...@@ -68,13 +111,26 @@ namespace _02VerketteteListe_2 ...@@ -68,13 +111,26 @@ namespace _02VerketteteListe_2
//StringListe.Element e1 = new StringListe.Element("Anton"); //StringListe.Element e1 = new StringListe.Element("Anton");
//e1.next = null; //e1.next = null;
for (int i = 1; i <=5; i++) for (int i = 1; i <=3; i++)
{ {
l1.AddEnd("Name-" + i); l1.AddEnd("Name-" + i);
} }
l1.LoescheAnf(); foreach (string item in l1)
{
Console.WriteLine($"foreach: {item}");
}
//Console.WriteLine( l1.Print2());
Console.WriteLine("==========");
l1.DeleteEnd();
l1.Print();
Console.WriteLine("----------");
l1.DeleteEnd();
l1.Print(); l1.Print();
Console.WriteLine($"Suche nach Name-3: {l1.Suche("Name-3")}"); Console.WriteLine("----------");
//l1.LoescheAnf();
//l1.Print();
//Console.WriteLine($"Suche nach Name-3: {l1.Suche("Name-3")}");
} }
} }
} }
No preview for this file type
No preview for this file type
...@@ -10,3 +10,4 @@ C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug ...@@ -10,3 +10,4 @@ C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.dll C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.pdb C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.genruntimeconfig.cache C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.genruntimeconfig.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\02VerketteteListe-2\obj\Debug\netcoreapp3.1\02VerketteteListe-2.csprojAssemblyReference.cache
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment