c#数据结构之集合的实现(数组及链表两种实现)

集合的概念
集合是由一些确定的、彼此不同的成员或者元素构成的一个整体。如果将紧密相关的数据组合到一个集合中,则能够更有效地处理这些紧密相关的数据。代替编写不同的代码来处理每一单独的对象,您可以使用相同的调用代码来处理一个集合的所有元素。

 在c#中可以使用 Array 类和 System.Collections 类添加、移除和修改集合中的个别元素或某一范围内的元素。甚至可以将整个集合复制到另一个集合中。在 .NET Framework 2.0 版中,泛型集合类提供了新功能,并使得创建强类型集合变得容易。

虽然c#中提供了多种集合类可供我们选择,但为了加深对这种数据类型的理解,构造自己的集合类无疑是最好的选择。下面给出了基于数组及链表两种实现的集合类。

  1//集合的接口,定义了集合类中应实现的所有方法

  2    public interface IBag:IEnumerable

  3ExpandedBlockStart.gifContractedBlock.gif    {

  4        void Add(object element);//添加一个对象到集合中
  5        void AddAll(IBag bag);//把一个集合中的所有对象添加到另一个集合中
  6ExpandedSubBlockStart.gifContractedSubBlock.gif        int Size{get;}//获得集合的大小
  7        bool IsEmpty();//检查集合是否为空
  8        object RemoveRandom();//随机从集合中删除一个对象
  9        object Remove(object target);//删除指定的元素
 10        IBag Union(IBag bag);//合并两个集合中的元素,得到一个新的集合
 11        bool Contains(object target);检查指定对象是否在集合中
 12    }
 13
 14
 15// 集合的数组实现
 16
 17      public   class  ArrayBag:IBag
 18 ExpandedBlockStart.gifContractedBlock.gif     {
 19        int DEDAULT_CAPACITY = 1000;
 20        Random random = new Random();
 21        int count=0;
 22        object[] contents;
 23
 24        public ArrayBag()
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 26            contents = new object[DEDAULT_CAPACITY];
 27            //count = 0;
 28        }

 29        public ArrayBag(int initialCapacity)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 31            contents = new object[initialCapacity];
 32           // count = 0;
 33        }

 34ContractedSubBlock.gifExpandedSubBlockStart.gif        IBag 成员#region IBag 成员
 35
 36        public void Add(object element)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 38            if (Size == contents.Length)
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 40                ExpandCapacity();
 41            }

 42            contents[count] = element;
 43            count++;
 44        }

 45        private void ExpandCapacity()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 47            object[] temp = new object[contents.Length * 2];
 48            for (int index = 0; index < contents.Length; index++)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 50                temp[index] = contents[index];
 51            }

 52            contents = temp;
 53        }

 54
 55        public void AddAll(IBag bag)
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 57            foreach (object o in bag)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 59                Add(o);
 60            }

 61        }

 62
 63        public int Size
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            get return count; }
 66        }

 67
 68        public bool IsEmpty()
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 70           return (count==0);
 71        }

 72
 73        public object RemoveRandom()
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 75            if (IsEmpty())
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 77                throw new Exception("this colleciton is empty!");
 78            }

 79            int choice = random.Next(count);
 80            
 81            object result = contents[choice];
 82            contents[choice] = contents[count - 1];
 83            contents[count - 1= null;
 84            count--;
 85            return result;
 86        }

 87
 88        public object Remove(object target)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 90            if (IsEmpty())
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 92                throw new Exception("the collection is empty");
 93            }

 94            int index = Find(target);
 95            if (index == -1)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 97                throw new Exception("not found!");
 98            }

 99            else
100ExpandedSubBlockStart.gifContractedSubBlock.gif            {
101                object result = contents[index];
102                contents[index] = contents[count - 1];
103                contents[count - 1= null;
104                count--;
105                return result;
106            }

107        }

108
109        private int Find(object target)
110ExpandedSubBlockStart.gifContractedSubBlock.gif        {
111            for (int i = 0; i < count; i++)
112ExpandedSubBlockStart.gifContractedSubBlock.gif            {
113                if (contents[i].Equals(target))
114                    return i;
115            }

116            return -1;
117        }

118
119        public IBag Union(IBag bag)
120ExpandedSubBlockStart.gifContractedSubBlock.gif        {
121            ArrayBag both = new ArrayBag();
122            for (int index = 0; index < count; index++)
123ExpandedSubBlockStart.gifContractedSubBlock.gif            {
124                both.Add(contents[index]);
125            }

126            foreach (object o in bag)
127ExpandedSubBlockStart.gifContractedSubBlock.gif            {
128                both.Add(o);
129            }

130            return both;
131        }

132
133        public bool Contains(object target)
134ExpandedSubBlockStart.gifContractedSubBlock.gif        {
135            if (Find(target) != -1)
136ExpandedSubBlockStart.gifContractedSubBlock.gif            {
137                return true;
138            }

139            else
140ExpandedSubBlockStart.gifContractedSubBlock.gif            {
141                return false;
142            }

143        }

144
145
146        public IEnumerator GetEnumerator()
147ExpandedSubBlockStart.gifContractedSubBlock.gif        {
148            for (int index = 0; index < count; index++)
149ExpandedSubBlockStart.gifContractedSubBlock.gif            {
150                yield return contents[index];
151            }

152        }

153
154        #endregion

155
156        public override string ToString()
157ExpandedSubBlockStart.gifContractedSubBlock.gif        {
158            StringBuilder sb = new StringBuilder();
159            for (int i = 0; i < count; i++)
160ExpandedSubBlockStart.gifContractedSubBlock.gif            {
161                sb.Append(contents[i] + " ");
162            }

163            return sb.ToString();
164        }

165
166    }

167 }
168
169 //集合的链表实现
170   public   class  LinkNode
171 ExpandedBlockStart.gifContractedBlock.gif     {
172        object element;
173
174        public object Element
175ExpandedSubBlockStart.gifContractedSubBlock.gif        {
176ExpandedSubBlockStart.gifContractedSubBlock.gif            get return element; }
177ExpandedSubBlockStart.gifContractedSubBlock.gif            set { element = value; }
178        }

179        LinkNode next;
180
181        public LinkNode Next
182ExpandedSubBlockStart.gifContractedSubBlock.gif        {
183ExpandedSubBlockStart.gifContractedSubBlock.gif            get return next; }
184ExpandedSubBlockStart.gifContractedSubBlock.gif            set { next = value; }
185        }

186        public LinkNode(object element)
187ExpandedSubBlockStart.gifContractedSubBlock.gif        {
188            this.element = element;
189            next = null;
190        }

191        public LinkNode()
192ExpandedSubBlockStart.gifContractedSubBlock.gif        {
193        }

194
195    }

196      public   class  LinkedBag:IBag
197 ExpandedBlockStart.gifContractedBlock.gif     {
198ContractedSubBlock.gifExpandedSubBlockStart.gif        IBag 成员#region IBag 成员
199
200        LinkNode contents;
201        int count;
202        Random r = new Random();
203
204        public void Add(object element)
205ExpandedSubBlockStart.gifContractedSubBlock.gif        {
206            LinkNode node = new LinkNode(element);
207            node.Next = contents;
208            contents = node;
209            count++;
210        }

211
212        public void AddAll(IBag bag)
213ExpandedSubBlockStart.gifContractedSubBlock.gif        {
214            foreach (object o in bag)
215ExpandedSubBlockStart.gifContractedSubBlock.gif            {
216                Add(o);
217            }

218        }

219
220        public int Size
221ExpandedSubBlockStart.gifContractedSubBlock.gif        {
222ExpandedSubBlockStart.gifContractedSubBlock.gif            get return count; }
223        }

224
225        public bool IsEmpty()
226ExpandedSubBlockStart.gifContractedSubBlock.gif        {
227            return (count==0);
228        }

229
230        public object RemoveRandom()
231ExpandedSubBlockStart.gifContractedSubBlock.gif        {
232            object result = null;
233            LinkNode previous, current;
234            if (IsEmpty())
235ExpandedSubBlockStart.gifContractedSubBlock.gif            {
236                throw new Exception("collection is empty!");
237            }

238            int choice = r.Next(count);
239            if (choice == 0)
240ExpandedSubBlockStart.gifContractedSubBlock.gif            {
241                result = contents.Element;
242                contents = contents.Next;
243            }

244            else
245ExpandedSubBlockStart.gifContractedSubBlock.gif            {
246                previous = contents;
247                for (int i = 1; i < choice; i++)
248ExpandedSubBlockStart.gifContractedSubBlock.gif                {
249                    previous = previous.Next;
250                }

251                current = previous.Next;
252                result = current.Element;
253                previous.Next = current.Next;
254            }

255            count--;
256            return result;
257        }

258
259        public object Remove(object target)
260ExpandedSubBlockStart.gifContractedSubBlock.gif        {
261            bool found = false;
262            LinkNode previous, current;
263            object result = null;
264            if (IsEmpty())
265ExpandedSubBlockStart.gifContractedSubBlock.gif            {
266                throw new Exception("collection is empty!");
267            }

268            if (contents.Element.Equals(target))
269ExpandedSubBlockStart.gifContractedSubBlock.gif            {
270                result = contents.Element;
271                contents = contents.Next;
272            }

273            else
274ExpandedSubBlockStart.gifContractedSubBlock.gif            {
275                previous = contents;
276                current = previous.Next;
277                for(int i=1;i<count&&!found;i++)
278ExpandedSubBlockStart.gifContractedSubBlock.gif                {
279                    if (current.Element.Equals(target))
280ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
281                        found = true;
282                    }

283                    previous = current;
284                    current = previous.Next;
285                }

286                  if (!found)
287ExpandedSubBlockStart.gifContractedSubBlock.gif                {
288                    throw new Exception("Elements not found!");
289                }

290                result = current.Next;
291                previous.Next = current.Next;
292            }

293            count--;
294            return result;
295        }

296
297        public IBag Union(IBag bag)
298ExpandedSubBlockStart.gifContractedSubBlock.gif        {
299            LinkedBag both = new LinkedBag();
300            foreach (LinkNode node in this)
301ExpandedSubBlockStart.gifContractedSubBlock.gif            {
302                both.Add(node.Element);
303            }

304            foreach (LinkNode node in bag)
305ExpandedSubBlockStart.gifContractedSubBlock.gif            {
306                both.Add(node.Element);
307            }

308            return both;
309        }

310
311        public bool Contains(object target)
312ExpandedSubBlockStart.gifContractedSubBlock.gif        {
313            bool found = false;
314            LinkNode current = contents;
315            while (current != null && !found)
316ExpandedSubBlockStart.gifContractedSubBlock.gif            {
317                if (current.Element.Equals(target))
318ExpandedSubBlockStart.gifContractedSubBlock.gif                {
319                    found = true;
320                }

321                current = current.Next;
322            }

323            return found;
324        }

325
326        #endregion

327
328ContractedSubBlock.gifExpandedSubBlockStart.gif        IEnumerable 成员#region IEnumerable 成员
329
330        public System.Collections.IEnumerator GetEnumerator()
331ExpandedSubBlockStart.gifContractedSubBlock.gif        {
332            LinkNode current = contents;
333            while (current != null)
334ExpandedSubBlockStart.gifContractedSubBlock.gif            {
335                yield return current;
336                current = current.Next;
337            }

338        }

339
340        #endregion

341
342        public override string ToString()
343ExpandedSubBlockStart.gifContractedSubBlock.gif        {
344            StringBuilder sb = new StringBuilder();
345            foreach (LinkNode node in this)
346                sb.Append(node.Element.ToString() + " ");
347            return sb.ToString();
348        }

349    }

转载于:https://www.cnblogs.com/healer_zll/archive/2008/11/22/1338809.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值