From af4209ae79bdd114e65e78b05bb632907a024d9f Mon Sep 17 00:00:00 2001
From: wienkop <uwe.wienkop@th-nuernberg.de>
Date: Mon, 17 May 2021 13:05:30 +0200
Subject: [PATCH] 2021-05-17 Generische Liste und KeyValue Liste

---
 09-1 KeyValueList/09-1 KeyValueList.csproj    |  9 +++
 09-1 KeyValueList/KeyValueList.cs             | 63 +++++++++++++++++
 09-1 KeyValueList/KeyValuePaar.cs             | 16 +++++
 09-1 KeyValueList/Program.cs                  | 50 ++++++++++++++
 .../09-Ubg GenericStack-Mo.csproj             |  9 +++
 09-Ubg GenericStack-Mo/GenericStack.cs        | 67 +++++++++++++++++++
 09-Ubg GenericStack-Mo/Program.cs             | 54 +++++++++++++++
 .../08-3 GenericList.csproj"                  |  2 +-
 "0\303\2378-3 GenericList/GenericList.cs"     | 31 ++++++++-
 "0\303\2378-3 GenericList/Person.cs"          |  2 +-
 "0\303\2378-3 GenericList/Program.cs"         | 42 +++++++++---
 Prog2WienkopSS2021.sln                        | 16 ++++-
 12 files changed, 343 insertions(+), 18 deletions(-)
 create mode 100644 09-1 KeyValueList/09-1 KeyValueList.csproj
 create mode 100644 09-1 KeyValueList/KeyValueList.cs
 create mode 100644 09-1 KeyValueList/KeyValuePaar.cs
 create mode 100644 09-1 KeyValueList/Program.cs
 create mode 100644 09-Ubg GenericStack-Mo/09-Ubg GenericStack-Mo.csproj
 create mode 100644 09-Ubg GenericStack-Mo/GenericStack.cs
 create mode 100644 09-Ubg GenericStack-Mo/Program.cs

diff --git a/09-1 KeyValueList/09-1 KeyValueList.csproj b/09-1 KeyValueList/09-1 KeyValueList.csproj
new file mode 100644
index 0000000..7efb70f
--- /dev/null
+++ b/09-1 KeyValueList/09-1 KeyValueList.csproj	
@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <RootNamespace>_09_1_KeyValueList</RootNamespace>
+  </PropertyGroup>
+
+</Project>
diff --git a/09-1 KeyValueList/KeyValueList.cs b/09-1 KeyValueList/KeyValueList.cs
new file mode 100644
index 0000000..9b83155
--- /dev/null
+++ b/09-1 KeyValueList/KeyValueList.cs	
@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace _09_1_KeyValueList
+{
+    class KeyValueList<K,V> where K:IComparable<K>
+    {
+        private class KVItem  
+        {
+            public K key;
+            public V val;
+            public KVItem next = null;
+            public KVItem(K key, V val) { this.key = key; this.val = val; }
+            public override string ToString() => $"{key}: {val}";
+        }
+        KVItem first = null, last = null;
+        int count = 0;
+
+        public void AddEnd(K key, V val)
+        {
+            KVItem newItem = new KVItem(key, val);   // 1. Neues Element anlegen
+            count++;
+            if (first == null)                            // 2. Leere Liste?
+                first = last = newItem;
+            else
+            {
+                last.next = newItem;               // 3. Neues Element am Ende anfügen
+                last = last.next;
+            }
+        }
+        public IEnumerator<KeyValuePaar<K,V>> GetEnumerator()
+        {
+            for (KVItem item = first; item!=null;item=item.next)
+                yield return new KeyValuePaar<K, V>(item.key, item.val);
+        }
+        private KVItem ItemSearch(K searchKey)
+        {
+            for (KVItem item = first; item != null; item = item.next)
+                if (item.key.CompareTo(searchKey) == 0)
+                    return item;
+            return null;
+        }
+        public V this[K key]
+        {
+            get
+            {
+                KVItem item = ItemSearch(key);
+                if (item != null)
+                    return item.val;
+                throw new Exception($"Der Schlüssel {key} wurde nicht gefunden");
+            }
+            set
+            {
+                KVItem item = ItemSearch(key);
+                if (item != null)       // Existiert der Schlüssel schon in der Liste?
+                    item.val = value;
+                else
+                    AddEnd(key, value);
+            }
+        }
+    }
+}
diff --git a/09-1 KeyValueList/KeyValuePaar.cs b/09-1 KeyValueList/KeyValuePaar.cs
new file mode 100644
index 0000000..f2c2d37
--- /dev/null
+++ b/09-1 KeyValueList/KeyValuePaar.cs	
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Text;
+
+namespace _09_1_KeyValueList
+{
+    // "Wienkop" | 1614
+    class KeyValuePaar<K, V> 
+    {
+        public K key;
+        public V val;
+        public KeyValuePaar(K key, V val) { this.key = key;this.val = val; }
+        public override string ToString() => $"{key}: {val}";
+    }
+}
diff --git a/09-1 KeyValueList/Program.cs b/09-1 KeyValueList/Program.cs
new file mode 100644
index 0000000..2779daf
--- /dev/null
+++ b/09-1 KeyValueList/Program.cs	
@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+
+namespace _09_1_KeyValueList
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            KeyValueList<string, int> telListe = new KeyValueList<string, int>();
+            //telListe.AddEnd("Wienkop", 1614);
+            //telListe.AddEnd("Otsa", 1855);
+
+            telListe["Wienkop"] = 1614;
+            telListe["Otsa"] = 1855;
+            Console.WriteLine(telListe["Otsa"]);
+
+            telListe["Otsa"] = 9999;
+            Console.WriteLine(telListe["Otsa"]);
+
+            foreach (var item in telListe)
+            {
+                Console.WriteLine($"Telefonnummer von {item.key}: {item.val}");
+            }
+
+
+            List<string> termineMo = new List<string>();
+            termineMo.Add("08:00 Dig. Bildbearbeitung");
+            termineMo.Add("09:45 Prog2 Übung");
+            termineMo.Add("11:30 Prog2 Vorlesung");
+
+            List<string> termineDi = new List<string>();
+            termineDi.Add("08:00 Prog2 Übung");
+            termineDi.Add("09:45 Prog2 Vorlesung");
+
+            KeyValueList<string, List<string>> terminkalender = new KeyValueList<string, List<string>>();
+            terminkalender["17.05.2021"] = termineMo;
+            terminkalender["18.05.2021"] = termineDi;
+
+            foreach (var tag in terminkalender)
+            {
+                Console.WriteLine($"\nTermine am {tag.key}:");
+                foreach (var item in tag.val)
+                {
+                    Console.WriteLine(item);
+                }
+            }
+        }
+    }
+}
diff --git a/09-Ubg GenericStack-Mo/09-Ubg GenericStack-Mo.csproj b/09-Ubg GenericStack-Mo/09-Ubg GenericStack-Mo.csproj
new file mode 100644
index 0000000..c5ad2ed
--- /dev/null
+++ b/09-Ubg GenericStack-Mo/09-Ubg GenericStack-Mo.csproj	
@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <RootNamespace>_09_Ubg_GenericStack_Mo</RootNamespace>
+  </PropertyGroup>
+
+</Project>
diff --git a/09-Ubg GenericStack-Mo/GenericStack.cs b/09-Ubg GenericStack-Mo/GenericStack.cs
new file mode 100644
index 0000000..310f419
--- /dev/null
+++ b/09-Ubg GenericStack-Mo/GenericStack.cs	
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace _09_Ubg_GenericStack_Mo
+{
+    // Erstellen Sie mit Hilfe einer Liste einen generischen Stapel (Stack), in dem beliebige Elemente
+    // gespeichert werden können.
+    // Ein Stack ist eine Datenstruktur, ähnlich wie ein Stapel auf Ihrem Schreibtisch. Das, was 
+    // zuoberst aufliegt, wird auch zuerst wieder heruntergenommen.
+    // Sie können einen Stapel leicht mithilfe einer Liste aufbauen. Hierfür benötigen Sie folgenden Methoden:
+    // - AddFirst: Als erstes Element einfügen
+    // - GetFirst(bool removeFromStack=true): Liefert das erste Element zurück und entfernt es vom Stapel; kann 
+    //            per removeFromStack unterbunden werden
+    // - Contains(T): Sucht nach dem übergebenen Element und liefert true/false zurück
+    // - GetEnumerator() : Ermöglicht einen Durchlauf durch den Stapel und liefert nach und nach alle Elemente zurück
+
+    class GenericStack<T> where T:IComparable<T>
+    {
+        class StackItem
+        {
+            public T data;
+            public StackItem next;
+            public StackItem (T Data) { data = Data; next = null; }
+        }
+        StackItem first = null;
+
+        public void AddFirst(T Data)
+        {
+            StackItem item = new StackItem(Data);
+            if (first == null)
+                first = item;
+            else
+            {
+                item.next = first;
+                first = item;
+            }
+        }
+        public bool IsEmpty { get => (first == null); }
+        public T GetFirst(bool removeFromStack = true)
+        {
+            if (first == null)
+                throw new Exception("Leerer Stack");
+                //return default(T);
+                // return null;    // Nur bei Referenztypen zulässig ~ class
+            T res = first.data;
+            if (removeFromStack)
+                first = first.next;
+            return res;
+        }
+
+        public bool Contains (T value)
+        {
+            for (StackItem item = first; item != null; item=item.next)
+            {
+                if (item.data.CompareTo(value) == 0)
+                    return true;
+            }
+            return false;
+        }
+        public IEnumerator<T> GetEnumerator()
+        {
+            for (StackItem item = first; item != null; item = item.next)
+                yield return item.data;
+        }
+    }
+}
diff --git a/09-Ubg GenericStack-Mo/Program.cs b/09-Ubg GenericStack-Mo/Program.cs
new file mode 100644
index 0000000..51969ee
--- /dev/null
+++ b/09-Ubg GenericStack-Mo/Program.cs	
@@ -0,0 +1,54 @@
+using System;
+
+namespace _09_Ubg_GenericStack_Mo
+{
+    class Kontakt : IComparable<Kontakt>
+    {
+        string name;
+        string telNr;
+        public Kontakt(string Name, string TelNr)
+        {
+            name = Name;
+            telNr = TelNr;
+        }
+        public int CompareTo(Kontakt other)  // a == other ? --> a.name == other.name?
+        {
+            return name.CompareTo(other.name);
+        }
+    }
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            GenericStack<int> iStack = new GenericStack<int>();
+            GenericStack<string> sStack = new GenericStack<string>();
+
+            iStack.AddFirst(10);
+            iStack.AddFirst(20);
+            iStack.AddFirst(30);
+            iStack.AddFirst(40);
+            iStack.AddFirst(50);
+            foreach (var item in iStack)
+                Console.WriteLine(item);
+            Console.WriteLine("--------------");
+
+            while (!iStack.IsEmpty)
+                Console.WriteLine(iStack.GetFirst());
+
+
+            sStack.AddFirst("Anton");
+            sStack.AddFirst("Berta");
+            sStack.AddFirst("Claudia");
+            sStack.AddFirst("Dieter");
+            foreach (var item in sStack)
+                Console.WriteLine(item);
+            Console.WriteLine("--------------");
+
+            while (!sStack.IsEmpty)
+                Console.WriteLine(sStack.GetFirst());
+
+            GenericStack<Kontakt> kStack = new GenericStack<Kontakt>();
+
+        }
+    }
+}
diff --git "a/0\303\2378-3 GenericList/08-3 GenericList.csproj" "b/0\303\2378-3 GenericList/08-3 GenericList.csproj"
index 38c17ab..40ff62e 100644
--- "a/0\303\2378-3 GenericList/08-3 GenericList.csproj"	
+++ "b/0\303\2378-3 GenericList/08-3 GenericList.csproj"	
@@ -3,7 +3,7 @@
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>netcoreapp3.1</TargetFramework>
-    <RootNamespace>_0ß8_3_GenericList</RootNamespace>
+    <RootNamespace>_08_3_GenericList</RootNamespace>
   </PropertyGroup>
 
 </Project>
diff --git "a/0\303\2378-3 GenericList/GenericList.cs" "b/0\303\2378-3 GenericList/GenericList.cs"
index 67471a7..29e0b53 100644
--- "a/0\303\2378-3 GenericList/GenericList.cs"	
+++ "b/0\303\2378-3 GenericList/GenericList.cs"	
@@ -3,8 +3,14 @@ using System.Collections.Generic;
 using System.Text;
 namespace _08_3_GenericList
 {
+    //string a = "Hallo";
+    //string o = "Welt";
 
-    class Liste<T> where T:IComparable<T>
+    // if (a < o)
+    // if (a.CompareTo(o) < 0)   // string.CompareTo(string)   T:IComparable<T>
+    //                          Liste<int>.CompareTo(Liste<int>)
+    // if ( ... )                // string.CompareTo(object)   T:IComparable
+    class Liste<T> : IComparable<Liste<T>> where T:IComparable<T>
     {
         class LItem
         {
@@ -17,10 +23,14 @@ namespace _08_3_GenericList
         }
 
         LItem first = null, last = null;
+        public int Count { get; private set; }
+        public Liste() { Count = 0; }
+        public int CompareTo(Liste<T> other) => Count - other.Count;
 
         public void AddEnd(T Data)
         {
             LItem newItem = new LItem(Data);
+            Count++;
             if (first == null)
                 first = last = newItem;
             else  // Es gibt schon Listenelemente
@@ -33,6 +43,7 @@ namespace _08_3_GenericList
         public void AddFront(T Data)
         {
             LItem newItem = new LItem(Data);
+            Count++; 
             if (first == null)
                 first = last = newItem;
             else
@@ -54,6 +65,7 @@ namespace _08_3_GenericList
             {
                 // Wir wissen: Das neue Element ist NICHT das erste und nicht das letzte Element
                 LItem newItem = new LItem(InsData);
+                Count++;
 
                 LItem item = first;
                 while (item.next.data.CompareTo(InsData) < 0)
@@ -73,6 +85,7 @@ namespace _08_3_GenericList
 
             if (first == null)  // Fall 1
                 return;
+            Count--;
             if (first == last)  // Fall 2
                 first = last = null;
             else
@@ -89,6 +102,7 @@ namespace _08_3_GenericList
 
             if (first == null)  // Fall 1
                 return;
+            Count--;
             if (first == last)  // Fall 2
                 first = last = null;
             else
@@ -115,6 +129,7 @@ namespace _08_3_GenericList
                 // Fall 4: Wir wissen:
                 // Das zu löschende Element befindet sich weder am Anfang noch am Ende
                 //    --> Es gibt ein Vorgänger- UND ein Nachfolger-Element
+                Count--;
                 LItem item = first.next;
                 while (item.next != null && item.data.CompareTo(DelData) != 0)
                     item = item.next;
@@ -135,6 +150,16 @@ namespace _08_3_GenericList
             }
             Console.WriteLine("-------------------");
         }
+        public override string ToString()
+        {
+            StringBuilder sb = new StringBuilder();
+            for (LItem item = first; item != null; item = item.next)
+            {
+                sb.Append(item.data.ToString());
+                sb.Append("  ");
+            }
+            return sb.ToString();
+        }
         public void PrintReverse()
         {
             for (LItem item = last; item != null; item = item.prev)
@@ -152,10 +177,10 @@ namespace _08_3_GenericList
             }
             return false;
         }
-        public IEnumerator<T> Reverse()
+        public IEnumerable<T> Reverse()
         {
             for (LItem item = last; item != null; item = item.prev)
-            yield return item.data;
+                yield return item.data;
         }
         public IEnumerator<T> GetEnumerator()
         {
diff --git "a/0\303\2378-3 GenericList/Person.cs" "b/0\303\2378-3 GenericList/Person.cs"
index a7abf40..a967017 100644
--- "a/0\303\2378-3 GenericList/Person.cs"	
+++ "b/0\303\2378-3 GenericList/Person.cs"	
@@ -17,6 +17,6 @@ namespace _08_3_GenericList
             //string vollname2 = other.name + other.vorname;      // "MeierBerta"
             //return vollname1.CompareTo(vollname2);              // "MeierAnton".CompareTo("MeierBerta")
         }
-        public override string ToString() => $"{vorname} {name}";
+        public override string ToString() => $"{name}, {vorname}";
     }
 }
diff --git "a/0\303\2378-3 GenericList/Program.cs" "b/0\303\2378-3 GenericList/Program.cs"
index 4207335..050993e 100644
--- "a/0\303\2378-3 GenericList/Program.cs"	
+++ "b/0\303\2378-3 GenericList/Program.cs"	
@@ -2,18 +2,28 @@
 
 namespace _08_3_GenericList
 {
+
     class Program
     {
         static void Main(string[] args)
         {
-            Liste<int> il = new Liste<int>();
-            il.AddSorted(10);
-            il.AddSorted(90);
-            il.AddSorted(60);
-            il.AddSorted(30);
-            il.AddSorted(80);
-            il.AddSorted(20);
-            il.Print();
+            Liste<int> il1 = new Liste<int>();
+            il1.AddSorted(10);
+            il1.AddSorted(90);
+            il1.AddSorted(60);
+            il1.AddSorted(30);
+            il1.AddSorted(80);
+            il1.AddSorted(20);
+            il1.Print();
+
+
+            Liste<int> il2 = new Liste<int>();
+            il2.AddSorted(100);
+            il2.AddSorted(900);
+            il2.AddSorted(600);
+            il2.AddSorted(300);
+            il2.Print();
+
 
             Liste<string> sl = new Liste<string>();
             sl.AddSorted("Berta");
@@ -23,14 +33,23 @@ namespace _08_3_GenericList
             sl.AddSorted("Anton");
             sl.Print();
 
+            //Liste<object> ol = new Liste<object>();
+            //ol.AddEnd(1);
+            //ol.AddEnd(3.14);
+            //ol.AddEnd("Hallo");
+
             Liste<Person> pl = new Liste<Person>();
             pl.AddSorted(new Person("Meier", "Berta"));
             pl.AddSorted(new Person("Meier", "Anton"));
             pl.AddSorted(new Person("Huber", "Claudia"));
             pl.Print();
 
-            //Liste<Liste<int>> ll = new Liste<Liste<int>>();
-            //ll.AddSorted(il);
+
+            Liste<Liste<int>> ll = new Liste<Liste<int>>();
+            ll.AddSorted(il1);
+            ll.AddSorted(il2);
+            ll.Print();
+
 
             Liste<Kfz> kl = new Liste<Kfz>();
             kl.AddSorted(new Pkw(1599, 100, "N-AB 123"));
@@ -38,7 +57,8 @@ namespace _08_3_GenericList
             kl.AddSorted(new LKW("FÜ-MN 543", 20000, 5000));
             kl.Print();
 
-            foreach (var item in kl)
+            // foreach (var item in kl)
+            foreach (var item in kl.Reverse())
             {
                 Console.WriteLine(item);
             }
diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln
index 3927216..3b01304 100644
--- a/Prog2WienkopSS2021.sln
+++ b/Prog2WienkopSS2021.sln
@@ -61,9 +61,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "08-2 Generics Intro", "08-2
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "08x HTML", "08-x HTMLEngine\08x HTML.csproj", "{BCED82D0-F95F-4D82-9019-2C63D6B3CC8A}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "08-UbgExceptionsDi", "08-UbgExceptionsDi\08-UbgExceptionsDi.csproj", "{57F7EAD8-D0C8-4080-B939-D2B4D0A7CC56}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "08-UbgExceptionsDi", "08-UbgExceptionsDi\08-UbgExceptionsDi.csproj", "{57F7EAD8-D0C8-4080-B939-D2B4D0A7CC56}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "08-3 GenericList", "0ß8-3 GenericList\08-3 GenericList.csproj", "{05E06BC5-768E-44D2-82C3-B473AEEDBD6B}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "08-3 GenericList", "0ß8-3 GenericList\08-3 GenericList.csproj", "{05E06BC5-768E-44D2-82C3-B473AEEDBD6B}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09-Ubg GenericStack-Mo", "09-Ubg GenericStack-Mo\09-Ubg GenericStack-Mo.csproj", "{E81D4D2B-71A8-4B75-827B-18249E4D30B5}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09-1 KeyValueList", "09-1 KeyValueList\09-1 KeyValueList.csproj", "{DCBBE729-6B39-46C4-A072-8B793A6245DF}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -195,6 +199,14 @@ Global
 		{05E06BC5-768E-44D2-82C3-B473AEEDBD6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{05E06BC5-768E-44D2-82C3-B473AEEDBD6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{05E06BC5-768E-44D2-82C3-B473AEEDBD6B}.Release|Any CPU.Build.0 = Release|Any CPU
+		{E81D4D2B-71A8-4B75-827B-18249E4D30B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{E81D4D2B-71A8-4B75-827B-18249E4D30B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{E81D4D2B-71A8-4B75-827B-18249E4D30B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{E81D4D2B-71A8-4B75-827B-18249E4D30B5}.Release|Any CPU.Build.0 = Release|Any CPU
+		{DCBBE729-6B39-46C4-A072-8B793A6245DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{DCBBE729-6B39-46C4-A072-8B793A6245DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{DCBBE729-6B39-46C4-A072-8B793A6245DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{DCBBE729-6B39-46C4-A072-8B793A6245DF}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
-- 
GitLab