无聊用C#2.0泛型实现的链表(C#数据结构一)

本文介绍了一个简单的双向链表实现,使用C# 2.0泛型特性。该链表支持添加、删除节点,获取长度等基本操作,并实现了IEnumerable<T>接口,允许使用foreach进行迭代。
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Collections.Generic;
  4None.gifnamespace MyLinkList
  5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  6InBlock.gif    public class LNode<T>  //结点
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  8InBlock.gif        private T data; //结点值
  9InBlock.gif        private LNode<T> nextNode;
 10InBlock.gif        private LNode<T> prevNode;
 11InBlock.gif        public LNode()
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 13InBlock.gif 
 14ExpandedSubBlockEnd.gif        }

 15InBlock.gif        public T Data
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn data; }
 18ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ data = value; }
 19ExpandedSubBlockEnd.gif        }

 20InBlock.gif        public LNode(LNode<T> prNode,LNode<T> nexNode,T dat)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            data = dat;
 23InBlock.gif            nextNode = nexNode;
 24InBlock.gif            prevNode=prNode;
 25ExpandedSubBlockEnd.gif        }

 26InBlock.gif        public LNode<T> NextNode
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn nextNode; }
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ nextNode = value; }
 30ExpandedSubBlockEnd.gif        }

 31InBlock.gif        public LNode<T> PreviousNode
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn prevNode; }
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ prevNode = value; }
 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif  
 37ExpandedSubBlockEnd.gif    }

 38InBlock.gif    public class LkList<T> : IEnumerable<T>//链表
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 40InBlock.gif        LNode<T> Current;               //当前结点
 41InBlock.gif        private LNode<T> Head;          //头结点
 42InBlock.gif        private int NodeCount=0;        //结点数目
 43InBlock.gif        private int currentIndex = 0;   //当前结点索引位置
 44InBlock.gif
 45InBlock.gif        public LkList()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            Current = null;
 48InBlock.gif            Head = Current;
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif        public int CurrentIndex
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn currentIndex; }
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ currentIndex = value; }
 54ExpandedSubBlockEnd.gif        }

 55InBlock.gif        public LNode<T> CurrentNode
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn Current; }
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif        public void Add(T DataValue)
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61InBlock.gif            //添加结点
 62InBlock.gif            LNode<T> newNode = new LNode<T>(null,null,DataValue);
 63InBlock.gif            if (NodeCount == 0)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 65InBlock.gif                Current = newNode;
 66InBlock.gif                Head = Current;
 67InBlock.gif                NodeCount++;
 68ExpandedSubBlockEnd.gif            }

 69InBlock.gif            else
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                Current.NextNode = newNode;
 72InBlock.gif                newNode.PreviousNode = Current;
 73InBlock.gif                Current = newNode;
 74InBlock.gif                NodeCount++;
 75ExpandedSubBlockEnd.gif            }

 76InBlock.gif            currentIndex++;
 77InBlock.gif            
 78InBlock.gif
 79ExpandedSubBlockEnd.gif        }

 80InBlock.gif        public void Del(int i)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 82InBlock.gif          //i 为索引参数
 83InBlock.gif            if (i < 0 || i > this.GetLength())
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 85InBlock.gif                Console.WriteLine("not exist data!");
 86ExpandedSubBlockEnd.gif            }

 87InBlock.gif            else
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 89InBlock.gif                this.MoveToindex(i);
 90InBlock.gif                LNode<T> temp = Current.PreviousNode;
 91InBlock.gif                temp.NextNode = Current.NextNode;
 92InBlock.gif                NodeCount--;
 93InBlock.gif                Current = Head;
 94InBlock.gif                currentIndex = 1;
 95InBlock.gif                
 96ExpandedSubBlockEnd.gif            }

 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif        public void Insert(int i,T data)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            //在第i个元素后添加结点
101InBlock.gif
102InBlock.gif            this.MoveToindex(i);
103InBlock.gif            LNode<T> temp = Current.NextNode;
104InBlock.gif            this.Add(data);
105InBlock.gif            Current.NextNode = temp;
106InBlock.gif 
107ExpandedSubBlockEnd.gif        }

108InBlock.gif        public void Next()
109ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
110InBlock.gif            //取下个结点
111InBlock.gif            if (Current.NextNode == null)
112InBlock.gif                Console.WriteLine("out of index");
113InBlock.gif            else
114InBlock.gif                Current = Current.NextNode;
115InBlock.gif            currentIndex++;
116ExpandedSubBlockEnd.gif        }

117InBlock.gif        public void Previous()
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            //取上个结点
120InBlock.gif            if (Current.PreviousNode == null)
121ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
122InBlock.gif                Console.WriteLine("out of index");
123ExpandedSubBlockEnd.gif            }

124InBlock.gif            else
125ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
126InBlock.gif                Current = Current.PreviousNode;
127InBlock.gif                currentIndex--;
128ExpandedSubBlockEnd.gif            }

129ExpandedSubBlockEnd.gif        }

130InBlock.gif        public void MoveToindex(int nodeIndex)
131ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
132InBlock.gif            //跳转到第i个结点
133InBlock.gif            if (nodeIndex < 0 || nodeIndex > this.GetLength())
134ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
135InBlock.gif                Console.WriteLine("out of index!\n");
136ExpandedSubBlockEnd.gif            }

137InBlock.gif            else
138ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
139InBlock.gif                while (currentIndex > nodeIndex)
140ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
141InBlock.gif                    Previous();
142ExpandedSubBlockEnd.gif                }

143InBlock.gif                while (currentIndex < nodeIndex)
144ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
145InBlock.gif                    Next();
146ExpandedSubBlockEnd.gif                }

147InBlock.gif 
148ExpandedSubBlockEnd.gif            }

149InBlock.gif 
150ExpandedSubBlockEnd.gif        }

151InBlock.gif        public T GetByindex(int nodeIndex)
152ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
153InBlock.gif            if (nodeIndex < 0 || nodeIndex > this.GetLength())
154ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
155InBlock.gif                Console.WriteLine("out of index!\n");
156ExpandedSubBlockEnd.gif            }

157InBlock.gif            else
158InBlock.gif                MoveToindex(nodeIndex);
159InBlock.gif            return Current.Data;
160InBlock.gif
161ExpandedSubBlockEnd.gif        }

162InBlock.gif        public int GetLength()
163ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
164InBlock.gif            //获取结点数目
165InBlock.gif            LNode<T> temp = Current;
166InBlock.gif            int listlength = 0;
167InBlock.gif            Current = Head;
168InBlock.gif            while (Current !=null)
169ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
170InBlock.gif                Current = Current.NextNode;
171InBlock.gif                listlength++;
172ExpandedSubBlockEnd.gif            }

173InBlock.gif            Current = temp;
174InBlock.gif            return listlength;
175ExpandedSubBlockEnd.gif        }

176InBlock.gif        public void Display()
177ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
178InBlock.gif            //显示链表
179InBlock.gif            LNode<T> temp = Current;
180InBlock.gif            Current = Head;
181InBlock.gif            Console.Write("The list data is:");
182InBlock.gif            while (Current != null)
183ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{   
184InBlock.gif                Console.Write("{0}--->",Current.Data);
185InBlock.gif                Current = Current.NextNode;
186InBlock.gif                
187ExpandedSubBlockEnd.gif            }

188InBlock.gif            Current = temp;
189ExpandedSubBlockEnd.gif        }

190InBlock.gif        public IEnumerator<T> GetEnumerator()
191ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
192InBlock.gif            // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
193InBlock.gif            // foreach 迭代。请注意,在 C# 2.0 中, 
194InBlock.gif            // 不需要实现 Current 和 MoveNext。
195InBlock.gif            // 编译器将创建实现 IEnumerator<T> 的类。
196InBlock.gif            LNode<T> temp = Current;
197InBlock.gif            Current = Head;
198InBlock.gif            while (Current != null)
199ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
200InBlock.gif                yield return Current.Data;
201InBlock.gif                Current = Current.NextNode;
202ExpandedSubBlockEnd.gif            }

203InBlock.gif            Current = temp;
204ExpandedSubBlockEnd.gif        }

205InBlock.gif        IEnumerator IEnumerable.GetEnumerator()
206ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
207InBlock.gif
208InBlock.gif            return GetEnumerator();
209ExpandedSubBlockEnd.gif        }

210InBlock.gif
211InBlock.gif
212InBlock.gif
213ExpandedSubBlockEnd.gif    }

214InBlock.gif    public class test
215ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
216InBlock.gif        //当然是泛型,你可以用字符串去做结点
217InBlock.gif        public static void Main()
218ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
219InBlock.gif            string value;
220InBlock.gif            int intvalue;
221InBlock.gif            LkList<int> mylist = new LkList<int>();
222InBlock.gif            Console.WriteLine("input three nodes  which type is int");
223InBlock.gif     
224InBlock.gif                value = Console.ReadLine();
225InBlock.gif                intvalue = Convert.ToInt32(value);
226InBlock.gif                mylist.Add(intvalue);
227InBlock.gif                value = Console.ReadLine();
228InBlock.gif                intvalue = Convert.ToInt32(value);
229InBlock.gif                mylist.Add(intvalue);
230InBlock.gif                value = Console.ReadLine();
231InBlock.gif                intvalue = Convert.ToInt32(value);
232InBlock.gif                mylist.Add(intvalue);
233InBlock.gif                mylist.Display();
234InBlock.gif
235InBlock.gif                //可以用foreach迭代
236InBlock.gif                //foreach (int a in mylist)
237InBlock.gif                //{
238InBlock.gif                //    Console.WriteLine(a);
239InBlock.gif                //}
240InBlock.gif                Console.WriteLine("\n");
241InBlock.gif                Console.WriteLine("the length of list is:" + mylist.GetLength());
242InBlock.gif                LNode<int> t = mylist.CurrentNode;
243InBlock.gif
244InBlock.gif                Console.WriteLine("this is current index is:");
245InBlock.gif                Console.WriteLine(mylist.CurrentIndex);
246InBlock.gif
247InBlock.gif                mylist.Del(2);
248InBlock.gif                Console.WriteLine("the list after delete the 2th node:");
249InBlock.gif                mylist.Display();
250InBlock.gif                Console.WriteLine("\n");
251InBlock.gif                Console.WriteLine("insert a node after the 2th node");
252InBlock.gif                intvalue = Convert.ToInt32(Console.ReadLine());
253InBlock.gif                mylist.Insert(2, intvalue);
254InBlock.gif                mylist.Display();
255InBlock.gif                Console.Write("\n");
256InBlock.gif                Console.Write("GetByindex method return the 2th node data value:");
257InBlock.gif                Console.Write(mylist.GetByindex(2).ToString());
258InBlock.gif
259InBlock.gif
260InBlock.gif            Console.ReadLine();
261InBlock.gif
262ExpandedSubBlockEnd.gif        }

263ExpandedSubBlockEnd.gif    }

264InBlock.gif
265InBlock.gif
266ExpandedBlockEnd.gif}

267None.gif
学数据结构,顺便用C#2.0的泛型实现链表.功能较少,代码也很笨拙的说,但链表基本功能实现,并实现IEnumerable<T>接口.测试是用INT类型测试,当然你也可以用STRING类型

转载于:https://www.cnblogs.com/solo/archive/2006/12/16/594375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值