1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace List
6 {
7 class UseArrayList:ListInterface
8 {
9 //线性表元素数组
10 private object[] items;
11 //当前线性表的元素个数
12 private int length;
13 //线性表最大长度
14 private const int maxLen = 50;
15
16 /// <summary>
17 /// 用默认值初始化线性表元素数组
18 /// </summary>
19 public UseArrayList()
20 {
21 this.length = 0;
22 this.items = new object[maxLen];
23 }
24
25 /// <summary>
26 /// 用指定的长度初始化线性表元素数组
27 /// </summary>
28 /// <param name="maxLength">要初始化的数组长度</param>
29 public UseArrayList(int maxLength)
30 {
31 this.length = 0;
32 this.items = new object[maxLength];
33 }
34
35 /// <summary>
36 /// 往线性表末尾插入一个元素
37 /// </summary>
38 /// <param name="item">要插入的元素</param>
39 /// <returns>Bool</returns>
40 public bool add(object item)
41 {
42 bool isSuccessful = true;
43 if (!this.isFull())
44 {
45 items[length] = item;
46 length++;
47 }
48 else
49 {
50 isSuccessful = false;
51 }
52 return isSuccessful;
53 }
54
55 /// <summary>
56 /// 判断要要插入元素的位置是否合法
57 /// </summary>
58 /// <param name="pos">要插入元素的位置</param>
59 /// <returns>Bool</returns>
60 private bool isSuit(int pos)
61 {
62 bool issuit = false;
63 if (!this.isFull() && pos >= 1 && pos <= this.length + 1)
64 {
65 issuit = true;
66 }
67 else
68 issuit = false;
69 return issuit;
70 }
71
72 /// <summary>
73 /// 将该位置以下的元素,移向更低一级的位置上去
74 /// </summary>
75 /// <param name="pos">要出入元素的位置</param>
76 /// <returns>Bool</returns>
77 private bool moveItemDown(int pos)
78 {
79 bool isSuccessful = false;
80 try
81 {
82 for (int index = this.length; index >= pos; index--)
83 {
84 items[length] = items[length - 1];
85 }
86 isSuccessful = true;
87 }
88 catch
89 {
90 isSuccessful = false;
91 }
92 return isSuccessful;
93 }
94
95 /// <summary>
96 /// 将该位置以上的元素,移向更高一级的位置上去
97 /// </summary>
98 /// <param name="pos">要移出的元素的位置</param>
99 /// <returns>Bool</returns>
100 private bool moveItemUp(int pos)
101 {
102 bool isSuccessful = false;
103 try
104 {
105 for (int index = pos; index <= this.length; index++)
106 {
107 items[index - 1] = items[index];
108 }
109 isSuccessful = true;
110 }
111 catch
112 {
113 isSuccessful = false;
114 }
115 return isSuccessful;
116 }
117
118 /// <summary>
119 /// 在指定的位置上插入元素
120 /// </summary>
121 /// <param name="pos">要插入元素的位置</param>
122 /// <param name="item">要插入的元素</param>
123 /// <returns>Bool</returns>
124 public bool add(int pos, object item)
125 {
126 bool isSuccessful = false;
127 if (this.isSuit(pos))
128 {
129 if (this.moveItemDown(pos))
130 {
131 items[pos - 1] = item;
132 length++;
133 isSuccessful = true;
134 }
135 else
136 {
137 isSuccessful = false;
138 }
139 }
140 else
141 {
142 isSuccessful = false;
143 }
144 return isSuccessful;
145 }
146
147 /// <summary>
148 /// 在指定的位置处移除元素,并获得该元素的引用
149 /// </summary>
150 /// <param name="pos">要移除元素的位置</param>
151 /// <returns>object</returns>
152 public object remove(int pos)
153 {
154 object item = null;
155 if (isSuit(pos))
156 {
157 item = items[pos - 1];
158 if (this.moveItemUp(pos))
159 {
160 length--;
161 }
162 else
163 {
164 item = null;
165 }
166 }
167 return item;
168 }
169
170 /// <summary>
171 /// 判断线性表是否已满
172 /// </summary>
173 /// <returns>Bool</returns>
174 public bool isFull()
175 {
176 return this.length == this.items.Length;
177 }
178
179 /// <summary>
180 /// 替换指定位置的元素
181 /// </summary>
182 /// <param name="pos">被替换的元素的位置</param>
183 /// <param name="item">要进行替换的元素</param>
184 /// <returns>Bool</returns>
185 public bool replace(int pos, object item)
186 {
187 bool isSuccessful = false;
188 if (isSuit(pos))
189 {
190 items[pos - 1] = item;
191 isSuccessful = true;
192 }
193 else
194 {
195 isSuccessful = false;
196 }
197 return isSuccessful;
198 }
199
200
201 /// <summary>
202 /// 获得指定位置的元素
203 /// </summary>
204 /// <param name="pos">要获得元素的位置</param>
205 /// <returns>object</returns>
206 public object getItem(int pos)
207 {
208 object result = null;
209 if (isSuit(pos))
210 {
211 result = items[pos - 1];
212 }
213 else
214 {
215 result = null;
216 }
217 return result;
218 }
219
220 /// <summary>
221 /// 检查线性表是否包含指定的元素
222 /// </summary>
223 /// <param name="item">要查找的元素</param>
224 /// <returns>Bool</returns>
225 public bool hasItem(object item)
226 {
227 bool isHas = false;
228 for (int i = 0; i < this.length; i++)
229 {
230 if (items[i].Equals(item))
231 {
232 isHas = true;
233 break;
234 }
235 }
236 return isHas;
237 }
238
239 /// <summary>
240 /// 清空线性表
241 /// </summary>
242 public void clear()
243 {
244 this.length = 0;
245 }
246
247 /// <summary>
248 /// 获得线性表的长度
249 /// </summary>
250 /// <returns>线性表的长度</returns>
251 public int getLength()
252 {
253 return this.length;
254 }
255
256 /// <summary>
257 /// 检查线性表是否为空
258 /// </summary>
259 /// <returns>Bool</returns>
260 public bool isEmpty()
261 {
262 return length == 0;
263 }
264 }
265 }
266
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace List
6 {
7 class UseArrayList:ListInterface
8 {
9 //线性表元素数组
10 private object[] items;
11 //当前线性表的元素个数
12 private int length;
13 //线性表最大长度
14 private const int maxLen = 50;
15
16 /// <summary>
17 /// 用默认值初始化线性表元素数组
18 /// </summary>
19 public UseArrayList()
20 {
21 this.length = 0;
22 this.items = new object[maxLen];
23 }
24
25 /// <summary>
26 /// 用指定的长度初始化线性表元素数组
27 /// </summary>
28 /// <param name="maxLength">要初始化的数组长度</param>
29 public UseArrayList(int maxLength)
30 {
31 this.length = 0;
32 this.items = new object[maxLength];
33 }
34
35 /// <summary>
36 /// 往线性表末尾插入一个元素
37 /// </summary>
38 /// <param name="item">要插入的元素</param>
39 /// <returns>Bool</returns>
40 public bool add(object item)
41 {
42 bool isSuccessful = true;
43 if (!this.isFull())
44 {
45 items[length] = item;
46 length++;
47 }
48 else
49 {
50 isSuccessful = false;
51 }
52 return isSuccessful;
53 }
54
55 /// <summary>
56 /// 判断要要插入元素的位置是否合法
57 /// </summary>
58 /// <param name="pos">要插入元素的位置</param>
59 /// <returns>Bool</returns>
60 private bool isSuit(int pos)
61 {
62 bool issuit = false;
63 if (!this.isFull() && pos >= 1 && pos <= this.length + 1)
64 {
65 issuit = true;
66 }
67 else
68 issuit = false;
69 return issuit;
70 }
71
72 /// <summary>
73 /// 将该位置以下的元素,移向更低一级的位置上去
74 /// </summary>
75 /// <param name="pos">要出入元素的位置</param>
76 /// <returns>Bool</returns>
77 private bool moveItemDown(int pos)
78 {
79 bool isSuccessful = false;
80 try
81 {
82 for (int index = this.length; index >= pos; index--)
83 {
84 items[length] = items[length - 1];
85 }
86 isSuccessful = true;
87 }
88 catch
89 {
90 isSuccessful = false;
91 }
92 return isSuccessful;
93 }
94
95 /// <summary>
96 /// 将该位置以上的元素,移向更高一级的位置上去
97 /// </summary>
98 /// <param name="pos">要移出的元素的位置</param>
99 /// <returns>Bool</returns>
100 private bool moveItemUp(int pos)
101 {
102 bool isSuccessful = false;
103 try
104 {
105 for (int index = pos; index <= this.length; index++)
106 {
107 items[index - 1] = items[index];
108 }
109 isSuccessful = true;
110 }
111 catch
112 {
113 isSuccessful = false;
114 }
115 return isSuccessful;
116 }
117
118 /// <summary>
119 /// 在指定的位置上插入元素
120 /// </summary>
121 /// <param name="pos">要插入元素的位置</param>
122 /// <param name="item">要插入的元素</param>
123 /// <returns>Bool</returns>
124 public bool add(int pos, object item)
125 {
126 bool isSuccessful = false;
127 if (this.isSuit(pos))
128 {
129 if (this.moveItemDown(pos))
130 {
131 items[pos - 1] = item;
132 length++;
133 isSuccessful = true;
134 }
135 else
136 {
137 isSuccessful = false;
138 }
139 }
140 else
141 {
142 isSuccessful = false;
143 }
144 return isSuccessful;
145 }
146
147 /// <summary>
148 /// 在指定的位置处移除元素,并获得该元素的引用
149 /// </summary>
150 /// <param name="pos">要移除元素的位置</param>
151 /// <returns>object</returns>
152 public object remove(int pos)
153 {
154 object item = null;
155 if (isSuit(pos))
156 {
157 item = items[pos - 1];
158 if (this.moveItemUp(pos))
159 {
160 length--;
161 }
162 else
163 {
164 item = null;
165 }
166 }
167 return item;
168 }
169
170 /// <summary>
171 /// 判断线性表是否已满
172 /// </summary>
173 /// <returns>Bool</returns>
174 public bool isFull()
175 {
176 return this.length == this.items.Length;
177 }
178
179 /// <summary>
180 /// 替换指定位置的元素
181 /// </summary>
182 /// <param name="pos">被替换的元素的位置</param>
183 /// <param name="item">要进行替换的元素</param>
184 /// <returns>Bool</returns>
185 public bool replace(int pos, object item)
186 {
187 bool isSuccessful = false;
188 if (isSuit(pos))
189 {
190 items[pos - 1] = item;
191 isSuccessful = true;
192 }
193 else
194 {
195 isSuccessful = false;
196 }
197 return isSuccessful;
198 }
199
200
201 /// <summary>
202 /// 获得指定位置的元素
203 /// </summary>
204 /// <param name="pos">要获得元素的位置</param>
205 /// <returns>object</returns>
206 public object getItem(int pos)
207 {
208 object result = null;
209 if (isSuit(pos))
210 {
211 result = items[pos - 1];
212 }
213 else
214 {
215 result = null;
216 }
217 return result;
218 }
219
220 /// <summary>
221 /// 检查线性表是否包含指定的元素
222 /// </summary>
223 /// <param name="item">要查找的元素</param>
224 /// <returns>Bool</returns>
225 public bool hasItem(object item)
226 {
227 bool isHas = false;
228 for (int i = 0; i < this.length; i++)
229 {
230 if (items[i].Equals(item))
231 {
232 isHas = true;
233 break;
234 }
235 }
236 return isHas;
237 }
238
239 /// <summary>
240 /// 清空线性表
241 /// </summary>
242 public void clear()
243 {
244 this.length = 0;
245 }
246
247 /// <summary>
248 /// 获得线性表的长度
249 /// </summary>
250 /// <returns>线性表的长度</returns>
251 public int getLength()
252 {
253 return this.length;
254 }
255
256 /// <summary>
257 /// 检查线性表是否为空
258 /// </summary>
259 /// <returns>Bool</returns>
260 public bool isEmpty()
261 {
262 return length == 0;
263 }
264 }
265 }
266
浙公网安备 33010602011771号