2014年5月4日星期日
上次将数据结构Bag呈现了一下,后面又学了一个和Bag类似的结构Sequence。
1) 不同于Bag的随意,Sequence有一个current属性指向当前要操作的位置,如果current属性不可用的话,将current属性和used属性置成一样;
2) 插入是,分为insert()方法和attach()方法。Insert()方法是将元素插入到current索引的前一个位置,当current属性不可用时,插入到第一个元素;attach()方法是将元素插入到current索引的后一个位置,当current属性不可用时,插入到最后一个元素;
3) 不管是insert()和attach()插入,都涉及到元素的移动。需要使用到shiftItemsAfterward()方法和shiftItemsForeward()方法,将元素向后移动和向前移动;
4) 删除也是删除当前current指向的元素,若current不可用,则删除最后一个元素
5) travel()遍历的时候,使用内部迭代。主要依靠四个成员方法
start():将current属性指向第一个元素
isItem():判断current属性指向的是否为合法的元素
advance():将current属性向后移动,
current():取得current属性指向的元素
1. 数据结构Sequence的基本实现,Sequence.cs文件如下:
// FILE://DataStructure_Sequence.cs // CLASS IMPLMENT: class Sequence, insert item in the firstone, and // delete from the last one // // CONSTRUCTORS for class Sequence // Sequence(): // Postcondition:default constructor, generate an instance of the sequence class // and the capacityof the sequence class is 30 // // Sequence(sequence_size size): // Postcondition:generate an instance of the sequence class, and the capacity is // decided by theparameter users provide // // MODIFICATION MEMBER METHODS for class Sequence // void shiftItemsForeward(int indexFrom): // Postcondition:shift all items forehead one by one from the given index, the first item willlose // // void shiftItemsAfterward(int indexFrom): // Precondition: thesequence has the enough room // Postcondition:shift all items afterward one by one from the given index // // bool insert(sequence_type member): // Precondition: it'snot beyond the capacity of the sequence // Postcondition:insert the item before the current index. if no current index (the currentindex equals the used), // insert into thefirst // // bool attach(sequence_type member): // Precondition: it'snot beyond the capacity of the sequence // Postcondition:insert the item after the current index. if on current index, // insert into theend // // bool removeCurrent(): // Postcondition:remove the item the current point to // // int search(sequence_type target): // Postcondition: iffind return the index, otherwise NOT_FOUND(equal -1) // // CONST MEMBER METHOD for sequence class (not const now) // void travel(): // Postcondition:travel the sequence // // int count(sequence_type target): // Postcondition: returnhow many times the target has been found, if // not found, return0 // // uint getCapacity(): // Postcondition:return the capacity of the sequence // // int getUsed(): // Postcondition:return room has been used // // sequence_type getByIndex(int index): // Postcondition:return the item by the index user provides // // ArrayList getAllFoundIndex(): // Postcondition:return all the indexs have been found // during the searchoperation or count operation // // void sort(): // Postcondition:sort all the items in the sequence // // void start(): // Postcondition:make the current point to the first one // // sequence_type current(): // Precondition: if isItem() is true; // Postcondition:return the item the current point to // // advance(): // Postcondition:shift the current afterwards // // bool isItem(): // Postcondition: ifthe place that the current point to is a available item using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; // Alias the data type with my own type using sequence_size = System.UInt32; using sequence_type
数据结构与GDI+的碰撞(C# VS 2008)之Sequence
最新推荐文章于 2024-10-10 09:13:57 发布