双端链表

定义

双端链表和单链表类似,只不过需要多维护一个指向最后一个节点的指针 Tail,方便从最后插入数据

C# 实现

  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
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
void Main()
{
    DoubleEndedList list = new DoubleEndedList();
    list.InsertHead(2);
    list.InsertHead(1);
    list.InsertTail(3);
    list.InsertTail(4);
    list.DisplayList();

    Console.WriteLine("======Delete tail======");

    list.DeleteTail();
    list.DisplayList();

    list.DeleteTail();
    list.DisplayList();

    list.DeleteTail();
    list.DisplayList();

    list.DeleteTail();
    list.DisplayList();

    list.DeleteTail();
    list.DisplayList();

    list.InsertHead(2);
    list.InsertHead(1);
    list.InsertTail(3);
    list.InsertTail(4);
    list.DisplayList();

    Console.WriteLine("======Delete head======");

    list.DeleteHead();
    list.DisplayList();

    list.DeleteHead();
    list.DisplayList();

    list.DeleteHead();
    list.DisplayList();

    list.DeleteHead();
    list.DisplayList();

    list.DeleteHead();
    list.DisplayList();
}

public class DoubleEndedList
{
    public Node? Head { get; set; }

    public Node? Tail { get; set; }

    public DoubleEndedList()
    {
        Head = null;
        Tail = null;
    }

    public bool IsEmpty()
    {
        return Head == null;
    }

    // 表头插入
    public void InsertHead(int dd)
    {
        Node newNode = new Node(dd);
        if (IsEmpty())
        {
            Tail = newNode;
        }

        newNode.Next = Head;
        Head = newNode;
    }

    // 表尾插入
    public void InsertTail(int dd)
    {
        Node newNode = new Node(dd);
        if (IsEmpty())
        {
            Head = newNode;
        }
        else
        {
            Tail.Next = newNode;
        }

        Tail = newNode;
    }

    // 删除表头
    public void DeleteHead()
    {
        if (Head == null)
        {
            return;
        }

        Console.WriteLine($"Delete {Head.Value}");
        Head = Head.Next;
        if (Head == null)
        {
            Tail = null;
        }
    }

    // 删除表尾
    public void DeleteTail()
    {
        if (Tail == null)
        {
            return;
        }

        if (Head == Tail)
        {
            Console.WriteLine($"Delete {Tail.Value}");
            Head = null;
            Tail = null;
        }
        else
        {
            Node? before = null;
            Node? current = Head;
            while (current != Tail)
            {
                before = current;
                current = current.Next;
            }

            Tail = before;
            Tail.Next = null;
            Console.WriteLine($"Delete {current.Value}");
        }
    }

    public void DisplayList()
    {
        Node? current = Head;
        if (current == null)
        {
            Console.WriteLine("(Empty)");
            return;
        }

        while (current != null)
        {
            current.DisplayNode();
            current = current.Next;
        }
    }
}

public class Node
{
    public Node? Next { get; set; }

    public int Value { get; set; }

    public Node(int v)
    {
        Value = v;
    }

    internal void DisplayNode()
    {
        Console.Write(Value);
        if (Next != null)
        {
            Console.Write("->");
        }
        else
        {
            Console.Write(Environment.NewLine);
        }
    }
}