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

2020-05-05

parent 05844781
No related branches found
No related tags found
No related merge requests found
Showing
with 230 additions and 42 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
using System;
using System.Collections;
using System.Security.Cryptography;
namespace _02VerketteteListe_2
{
......@@ -17,11 +18,12 @@ namespace _02VerketteteListe_2
}
Element anf = null, ende = null;
// int anz=0;
int anz = 0; // Sehr sinnvoll für Index-Ops
//public StringListe() { }
public void AddEnd(string name)
{
Element neuesElement = new Element(name); // 1. Neues Element anlegen
anz++;
if (anf == null) // 2. Leere Liste?
anf = ende = neuesElement;
else
......@@ -35,6 +37,7 @@ namespace _02VerketteteListe_2
// 1. Leere Liste
// 2. Es existiert mind. ein Element
Element neuesElement = new Element(name); // Neues Element anlegen
anz++;
if (anf == null) // 1.Fall: Leere Liste?
anf = ende = neuesElement;
else
......@@ -69,25 +72,27 @@ namespace _02VerketteteListe_2
// an der gespeicherten Pos. weitergemacht.
}
}
public void DeleteFront()
public void DeleteFirst()
{
// 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;
anz--;
if (anf == ende) // 2. Fall
anf = ende = null;
else
anf = anf.next; // 3. Fall
}
public void DeleteEnd()
public void DeleteLast()
{
// 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");
anz--;
if (anf == ende) // 2. Fall
anf = ende = null;
else // 3. Fall
......@@ -100,59 +105,129 @@ namespace _02VerketteteListe_2
ende = vorletzter;
}
}
public void DeleteNth(int Ind)
public void DeleteNth(int index)
{
// 1.Fall: Liste ist leer ODER Ind>=anz --> Fehler
// 2.Fall: Ind == 0 --> DeleteFirst
// 3.Fall: Ind == anz-1 --> DeleteLast
// 4. Bis hierher: Liste hat mind. ein Element und das zu löschende Element
// ist nicht das Erste oder Letzte
if (anz == 0 || index >= anz) // 1. Fall
throw new ArgumentOutOfRangeException("Außerhalb der Anzahl der Listenelemente");
if (index == 0) // 2. Fall
DeleteFirst();
else if (index == anz - 1) // 3. Fall
DeleteLast();
else
{
// 1.Fall: Liste ist leer
// 2.Fall: Liste besteht nur aus einem Element UND Ind == 0
// 3.Fall: Liste hat mehr als ein Element
// a) Ind==0 --> DeleteFront()
// b) Ind==max -> DeleteLast()
// c) Ind < max
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)
for (int i = 1; i < index; i++)
vorletzter = vorletzter.next;
vorletzter.next = null;
ende = vorletzter;
}
vorletzter.next = vorletzter.next.next;
anz--;
}
}
class Program
public void DeleteByName(string name)
{
static void Main(string[] args)
// leere Liste ohne Elemente
if (anf == null)
return;
// erstes Element = gesuchtes Element
if (anf.name.CompareTo(name) == 0)
DeleteFirst();
// Wir wissen jetzt:
// Liste hat mind. ein Element UND das erste Element ist nicht das gesuchte Element
else
{
StringListe l1 = new StringListe();
StringListe l2 = new StringListe();
//StringListe.Element e1 = new StringListe.Element("Anton");
//e1.next = null;
Element item = anf;
// Durchlaufen der Liste
// solange Elemente in Liste vorhanden und gesuchtes Element noch nicht gefunden
while (item.next != null && item.next.name.CompareTo(name) != 0)
item = item.next;
// Wenn gesuchtes Element in Liste vorhanden
if (item.next != null)
{
// Nächstes Element überspringen = löschen
item.next = item.next.next;
// Wenn letztes Element = gesuchtes Element
if (item.next == null)
ende = item;
}
}
}
for (int i = 1; i <=3; i++)
public void AddBeforeNth(int index, string name)
{
if (index < 0 || index > anz)
throw new ArgumentOutOfRangeException("Index außerhalb der Listengröße");
if (index == 0)
AddFront(name);
else if (index == anz)
AddEnd(name);
else
{
Element neu = new Element(name);
anz++;
Element item = anf;
for (int i = 1; i < index; i++)
item = item.next;
neu.next = item.next;
item.next = neu;
}
}
public void AddSorted(string name)
{
// 1. Fall: Leere Liste oder Anfügen am Listenende
if (anf == null || ende.name.CompareTo(name) <= 0)
AddEnd(name);
else
{
if (name.CompareTo(anf.name) <= 0)
AddFront(name);
else
{
l1.AddEnd("Name-" + i);
Element neu = new Element(name);
anz++;
Element item = anf;
while (item.next.name.CompareTo(name) < 0)
item = item.next;
neu.next = item.next;
item.next = neu;
}
}
}
private Element NthElem(int index)
{
if (index < 0 || index >= anz)
throw new IndexOutOfRangeException("Listenindex außerhalb des gültigen Bereichs");
Element item = anf;
while (index > 0)
{
item = item.next;
index--;
}
return item;
}
foreach (string item in l1)
public string this[int index]
{
Console.WriteLine($"foreach: {item}");
get => NthElem(index).name;
set { NthElem(index).name = value; }
}
//Console.WriteLine( l1.Print2());
Console.WriteLine("==========");
l1.DeleteEnd();
l1.Print();
Console.WriteLine("----------");
l1.DeleteEnd();
}
class Program
{
static void Main(string[] args)
{
StringListe l1 = new StringListe();
StringListe l2 = new StringListe();
l1.AddSorted("Emil ");
l1.AddSorted("Anton");
l1.AddSorted("Berta");
l1.AddSorted("Claudia");
l1.AddSorted("Dieter");
l1.Print();
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
No preview for this file type
No preview for this file type
No preview for this file type
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_03_UebgDi_ZyklischeListe</RootNamespace>
</PropertyGroup>
</Project>
using System;
namespace _03_UebgDi_ZyklischeListe
{
class CycleList
{
class Element
{
public string name;
public Element next = null;
public Element(string name) { this.name = name; }
public override string ToString() => name;
}
Element anf = null, ende = null;
Element iterator = null;
public void AddEnd(string name)
{
Element neuesElement = new Element(name); // 1. Neues Element anlegen
if (anf == null) // 2. Leere Liste?
anf = ende = neuesElement;
else
ende = ende.next = neuesElement; // 3. Neues Element am Ende anfügen
}
public void CloseCycle()
{
ende.next = anf;
}
public void Print()
{
}
public void SetIterator(int plus)
{
}
public string GetName() => ;
public void MoveIterator() { }
}
class Program
{
static void Main(string[] args)
{
CycleList cl = new CycleList();
cl.AddEnd("Anton");
cl.AddEnd("Berta");
cl.CloseCycle();
cl.Print();
Console.WriteLine("-------------");
cl.AddEnd("Claudia");
cl.AddEnd("Dieter");
cl.CloseCycle();
cl.Print();
Console.WriteLine("-------------");
//cl.SetIterator(2);
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine(cl.GetName());
// cl.MoveIterator();
//}
}
}
}
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v3.1",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v3.1": {
"03 UebgDi ZyklischeListe/1.0.0": {
"runtime": {
"03 UebgDi ZyklischeListe.dll": {}
}
}
}
},
"libraries": {
"03 UebgDi ZyklischeListe/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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment