Program.cs 2.29 KB
Newer Older
Uwe Wienkop's avatar
Uwe Wienkop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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()
        {
Uwe Wienkop's avatar
Uwe Wienkop committed
30
31
32
33
34
35
36
37
38
39
40
            //for (Element item = anf; item != null; item = item.next)
            //    Console.WriteLine(item);
            if (anf == null)
                return;
            // Wir wissen: Es existiert mindestens EIN Element
            Element tmp = anf;
            do
            {
                Console.WriteLine(tmp);
                tmp = tmp.next;
            } while (tmp!=null && tmp != anf);
Uwe Wienkop's avatar
Uwe Wienkop committed
41
        }
Uwe Wienkop's avatar
Uwe Wienkop committed
42
        public void SetIterator(int offset = 0)
Uwe Wienkop's avatar
Uwe Wienkop committed
43
        {
Uwe Wienkop's avatar
Uwe Wienkop committed
44
45
46
47
48
            iterator = anf;
            for (int i = 0; i < offset; i++)
            {
                iterator = iterator.next;
            }
Uwe Wienkop's avatar
Uwe Wienkop committed
49
        }
Uwe Wienkop's avatar
Uwe Wienkop committed
50
51
        public string GetName() => iterator.name;
        public void MoveNext() { iterator = iterator.next; }
Uwe Wienkop's avatar
Uwe Wienkop committed
52
    }
Uwe Wienkop's avatar
Uwe Wienkop committed
53
54

    // ===========================================================
Uwe Wienkop's avatar
Uwe Wienkop committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    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("-------------");
Uwe Wienkop's avatar
Uwe Wienkop committed
70
71
72
73
74
75
            cl.SetIterator(2);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(cl.GetName());
                cl.MoveNext();
            }
Uwe Wienkop's avatar
Uwe Wienkop committed
76
77
78
        }
    }
}