Data Structures and Algorithms DotNet - 2、单向和双向链表

Created at 2013-03-01 Updated at 2018-05-03 Category Data Structures and Algorithms Tag Data Structures and Algorithms / DotNet

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
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkedList1
{
class Program
{
static void Main(string[] args)
{
DoublyLinkedList list = new DoublyLinkedList();
list.Insert("Hellow","header");
list.Insert("World", "Hellow");
list.PrintReverse();
Console.ReadKey();
}
}

//双向链表(Tow-Way Linked List/doubly linked list)
class DoublyNode
{
public object element;
public DoublyNode FLink;
public DoublyNode BLink;

public DoublyNode()
{
element = null;
FLink = null;
BLink=null;
}

public DoublyNode(object item)
{
element = item;
FLink = null;
BLink=null;
}
}

class DoublyLinkedList
{
protected DoublyNode header;

public DoublyLinkedList()
{
header = new DoublyNode("header");
}

private DoublyNode FindCurrent(object item)
{
DoublyNode current = new DoublyNode();
current = header;

while (current.element != item)
current = current.FLink;
return current;
}

public void Insert(object item, object after)
{
DoublyNode current = new DoublyNode();
DoublyNode newNode = new DoublyNode(item);

current = FindCurrent(after);

newNode.FLink = current.FLink;
newNode.BLink = current;
current.FLink = newNode;
}

public void Remove(object item)
{
DoublyNode current = new DoublyNode();

current = (DoublyNode)item;

current.BLink.FLink = current.FLink;
current.FLink.BLink = current.BLink;
current.BLink = null;
current.FLink = null;
}

public DoublyNode FindLast()
{
DoublyNode current = new DoublyNode();
current = header;
while (current.FLink != null)
current = current.FLink;

return current;
}

public void PrintReverse()
{
DoublyNode current = new DoublyNode();
current = FindLast();

while (current.BLink != null)
{
Console.WriteLine(current.element);
current = current.BLink;
}
}
}

//单向链表(One-Way Linked List/Singly linked list)
class Node
{
public object element;
public Node link;

public Node()
{
element = null;
link = null;
}

public Node(object item)
{
element = item;
link = null;
}
}

class SinglyLikedList
{
protected Node header;

public SinglyLikedList()
{
header = new Node("header");
}

//查找本项
private Node FindCurrentLink(object item)
{
Node current=new Node();
current=header;
while (current.element != item)
current = current.link;
return current;
}

//查找本项的上一项
private Node FindPreviousLink(object item)
{
Node current = new Node();
current = header;
while (current.link != null && current.link.element != item)
current = current.link;
return current;
}

public void Insert(object item, object after)
{
Node current = new Node();
Node newNode = new Node(item);
current = FindCurrentLink(after);
newNode.link = current.link;
current.link = newNode;
}

public void Remove(object item)
{
Node node = FindPreviousLink(item);
if (node.link != null)
node.link = node.link.link;
}

public void PrintList()
{
Node current = new Node();
current = header;

while (current.link != null)
{
Console.WriteLine(current.link.element);
current = current.link;
}
}
}
}
Site by Reinhard Hsu using Hexo & Random

Hide