c# 链表(LinkedList)使用
public struct test
{
public string name;
public string num;
}
public struct test1
{
public test st;
public string name;
}
下面代码中的使用都是正确的
1
public struct test2

{3
public string name;4
public string num;5
}6
public struct test17

{8
public test st;9
public string name;10
}11
static void Main(string[] args)12

{13
LinkedList<test> t = new LinkedList<test>();14
test t1, t2;15
test1 tt1, tt2;16
t1.name = "t1";17
t1.num = "1";18
t2.name = "t2";19
t2.num = "2";20
t.AddFirst(t1);21
t.AddFirst(t2);22

23
tt1.st = t1;24
tt2.st = t2;25
tt1.name = "tt1";26
tt2.name = "tt2";27
LinkedList<test1> tt = new LinkedList<test1>();28
tt.AddFirst(tt1);29
tt.AddFirst(tt2);30
LinkedListNode<test1> current11 = tt.FindLast(tt1);31
test1 tr1 = current11.Value;32

33
LinkedListNode<test> current1 = t.FindLast(t1);34
test tr = current1.Value;35
36

37

string[] words =
{ "the", "fox", "jumped", "over", "the", "dog" };38
LinkedList<string> sentence = new LinkedList<string>(words);39
Display(sentence);40

41
Console.WriteLine("sentence.Contains(\"jumped\") = {0}",42
sentence.Contains("jumped"));43

44
// Add the word "today" to the beginning of the linked list.45
// Remove the new node, and add it to the end of the list.46
sentence.AddFirst("today");47
Display(sentence);48

49
LinkedListNode<string> mark1 = sentence.First;50
sentence.RemoveFirst();51
sentence.AddLast(mark1);52
Display(sentence);53

54
sentence.RemoveLast();55
sentence.AddLast("yesterday");56
Display(sentence);57

58
mark1 = sentence.Last;59
sentence.RemoveLast();60
sentence.AddFirst(mark1);61
Display(sentence);62

63
sentence.RemoveFirst();64

65
LinkedListNode<string> current = sentence.FindLast("the");66
DisplayNode(current);67

68
//sentence.Remove(current);69
70
sentence.AddAfter(current, "old");71
sentence.AddAfter(current, "lazy");72
DisplayNode(current);73

74
current = sentence.Find("fox");75
DisplayNode(current);76

77
sentence.AddBefore(current, "quick");78
sentence.AddBefore(current, "brown");79
DisplayNode(current);80

81
// Keep a reference to the current node, "fox", and to the82
// previous node in the list. Use the Find method to locate83
// the node containing the value "dog". Show the position.84
mark1 = current;85
LinkedListNode<string> mark2 = current.Previous;86
current = sentence.Find("dog");87
DisplayNode(current);88

89
// The AddBefore method throws an InvalidOperationException90
// if you try to add a node that already belongs to a list.91
try92

{93
sentence.AddBefore(current, mark1);94
}95
catch (InvalidOperationException ex)96

{97
Console.WriteLine("Exception message: {0}", ex.Message);98
}99

100
// Remove the node referred to by mark1, and add it before 101
// the node referred to by current. Show the sentence, 102
// highlighting the position of the node referred to by103
// current.104
sentence.Remove(mark1);105
sentence.AddBefore(current, mark1);106
DisplayNode(current);107

108
// Remove the node referred to by current. If you try to show109
// its position now, the DisplayNode method prints a message.110
// Add the node after the node referred to by mark2, and 111
// display the sentence, highlighting current.112
sentence.Remove(current);113
DisplayNode(current);114
sentence.AddAfter(mark2, current);115
DisplayNode(current);116

117
// The Remove method finds and removes the first node that 118
// that has the specified value.119
sentence.Remove("old");120
Display(sentence);121

122
// When the linked list is cast to ICollection(Of String),123
// the Add method adds a node to the end of the list. 124
sentence.RemoveLast();125
ICollection<string> icoll = sentence;126
icoll.Add("rhinoceros");127
Display(sentence);128

129
// Create an array with the same number of elements as the130
// linked list.131
Console.WriteLine("\nCopy the list to an array.");132
string[] sArray = new string[sentence.Count];133
sentence.CopyTo(sArray, 0);134

135
foreach (string s in sArray)136

{137
Console.WriteLine(s);138
}139

140
// Release all the nodes.141
sentence.Clear();142

143
}144

145
private static void Display(LinkedList<string> words)146

{147
foreach (string word in words)148

{149
Console.Write(word + " ");150
}151
Console.WriteLine();152
}153

154
private static void DisplayNode(LinkedListNode<string> node)155

{156
if (node.List == null)157

{158

Console.WriteLine("Node \"
{0}\" is not in a list.",159
node.Value);160
return;161
}162

163
StringBuilder result = new StringBuilder("(" + node.Value + ")");164
LinkedListNode<string> nodeP = node.Previous;165

166
while (nodeP != null)167

{168
result.Insert(0, nodeP.Value + " ");169
nodeP = nodeP.Previous;170
}171

172
node = node.Next;173
while (node != null)174

{175
result.Append(" " + node.Value);176
node = node.Next;177
}178

179
Console.WriteLine(result);180
}

浙公网安备 33010602011771号