c# 链表(LinkedList)使用

下面代码中的使用都是正确的

ContractedBlock.gif ExpandedBlockStart.gif Code
  1public struct test
  2ExpandedBlockStart.gifContractedBlock.gif        {
  3            public string name;
  4            public string num;
  5        }

  6        public struct test1
  7ExpandedBlockStart.gifContractedBlock.gif        {
  8            public test st;
  9            public string name;
 10        }

 11        static void Main(string[] args)
 12ExpandedBlockStart.gifContractedBlock.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
 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
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            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 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
 89            // The AddBefore method throws an InvalidOperationException
 90            // if you try to add a node that already belongs to a list.
 91            try
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 93                sentence.AddBefore(current, mark1);
 94            }

 95            catch (InvalidOperationException ex)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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 by
103            // 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 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
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 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
135            foreach (string s in sArray)
136ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
146ExpandedBlockStart.gifContractedBlock.gif        {
147            foreach (string word in words)
148ExpandedSubBlockStart.gifContractedSubBlock.gif            {
149                Console.Write(word + " ");
150            }

151            Console.WriteLine();
152        }

153
154        private static void DisplayNode(LinkedListNode<string> node)
155ExpandedBlockStart.gifContractedBlock.gif        {
156            if (node.List == null)
157ExpandedSubBlockStart.gifContractedSubBlock.gif            {
158ExpandedSubBlockStart.gifContractedSubBlock.gif                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)
167ExpandedSubBlockStart.gifContractedSubBlock.gif            {
168                result.Insert(0, nodeP.Value + " ");
169                nodeP = nodeP.Previous;
170            }

171
172            node = node.Next;
173            while (node != null)
174ExpandedSubBlockStart.gifContractedSubBlock.gif            {
175                result.Append(" " + node.Value);
176                node = node.Next;
177            }

178
179            Console.WriteLine(result);
180        }

转载于:https://www.cnblogs.com/aooyu/archive/2009/11/06/1597599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值