public struct test
{
public string name;
public string num;
}
public struct test1
{
public test st;
public string name;
}
下面代码中的使用都是正确的
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public struct test
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
public string name;
4
public string num;
5
}
6
public struct test1
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8
public test st;
9
public string name;
10
}
11
static void Main(string[] args)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
LinkedListNode<test> current1 = t.FindLast(t1);
34
test tr = current1.Value;
35
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] words =
{ "the", "fox", "jumped", "over", "the", "dog" };
38
LinkedList<string> sentence = new LinkedList<string>(words);
39
Display(sentence);
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
Console.WriteLine("sentence.Contains(\"jumped\") = {0}",
42
sentence.Contains("jumped"));
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
LinkedListNode<string> mark1 = sentence.First;
50
sentence.RemoveFirst();
51
sentence.AddLast(mark1);
52
Display(sentence);
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
sentence.RemoveLast();
55
sentence.AddLast("yesterday");
56
Display(sentence);
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
mark1 = sentence.Last;
59
sentence.RemoveLast();
60
sentence.AddFirst(mark1);
61
Display(sentence);
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
sentence.RemoveFirst();
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
LinkedListNode<string> current = sentence.FindLast("the");
66
DisplayNode(current);
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
//sentence.Remove(current);
69
70
sentence.AddAfter(current, "old");
71
sentence.AddAfter(current, "lazy");
72
DisplayNode(current);
73![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
current = sentence.Find("fox");
75
DisplayNode(current);
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
sentence.AddBefore(current, "quick");
78
sentence.AddBefore(current, "brown");
79
DisplayNode(current);
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
// Keep a reference to the current node, "fox", and to the
82
// previous node in the list. Use the Find method to locate
83
// 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
// The AddBefore method throws an InvalidOperationException
90
// if you try to add a node that already belongs to a list.
91
try
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
sentence.AddBefore(current, mark1);
94
}
95
catch (InvalidOperationException ex)
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
Console.WriteLine("Exception message: {0}", ex.Message);
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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 by
103
// current.
104
sentence.Remove(mark1);
105
sentence.AddBefore(current, mark1);
106
DisplayNode(current);
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
// Remove the node referred to by current. If you try to show
109
// 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
// Create an array with the same number of elements as the
130
// linked list.
131
Console.WriteLine("\nCopy the list to an array.");
132
string[] sArray = new string[sentence.Count];
133
sentence.CopyTo(sArray, 0);
134![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
foreach (string s in sArray)
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
137
Console.WriteLine(s);
138
}
139![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
140
// Release all the nodes.
141
sentence.Clear();
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
}
144![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
145
private static void Display(LinkedList<string> words)
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
147
foreach (string word in words)
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
149
Console.Write(word + " ");
150
}
151
Console.WriteLine();
152
}
153![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
154
private static void DisplayNode(LinkedListNode<string> node)
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
156
if (node.List == null)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Console.WriteLine("Node \"
{0}\" is not in a list.",
159
node.Value);
160
return;
161
}
162![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
163
StringBuilder result = new StringBuilder("(" + node.Value + ")");
164
LinkedListNode<string> nodeP = node.Previous;
165![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
while (nodeP != null)
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
result.Insert(0, nodeP.Value + " ");
169
nodeP = nodeP.Previous;
170
}
171![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
172
node = node.Next;
173
while (node != null)
174![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
175
result.Append(" " + node.Value);
176
node = node.Next;
177
}
178![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
179
Console.WriteLine(result);
180
}