c#数据结构---顺序表
数据表实现代码:
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Collections;
5
6 namespace List
7 {
8 public class SeqList<T>
9 {
10 private int maxsize;//容量
11 private T[] data;//值
12 private int last;//末指针位置
13
14 //索引器
15 public T this[int index]
16 {
17 get { return data[index]; }
18 set { data[index] = value; }
19 }
20
21 public int Last
22 {
23 get { return last; }
24 }
25
26 public int Maxsize
27 {
28 get { return maxsize; }
29 set { maxsize = value; }
30 }
31
32 //构造函数
33 public SeqList(int size)
34 {
35 data = new T[size];
36 maxsize = size;
37 last = -1;
38 }
39
40 //求顺序列表的长度
41 public int GetLength()
42 {
43 return last + 1;
44 }
45
46 //清空列表
47 public void Clear()
48 {
49 last = -1;
50 }
51
52 //判断是否为空
53 public bool IsEmpty()
54 {
55 if (last==-1)
56 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63 }
64
65 //判断顺序表是否为满
66 public bool IsFull()
67 {
68 if (last==maxsize-1)
69 {
70 return true;
71 }
72 else
73 {
74 return false;
75 }
76 }
77
78 //在顺序表的末尾添加新元素
79 public void Append(T item)
80 {
81 if (IsFull())
82 {
83 Console.Write("List is Full!");
84 return;
85 }
86 data[++last] = item;
87 }
88
89 //在顺序表中的第i个位置添加新的元素
90 public void Insert(T item, int i)
91 {
92 if (IsFull())
93 {
94 Console.Write("List is Full!");
95 return;
96 }
97 if(i<1 || i>last+2)
98 {
99 Console.Write("Position is error!~");
100 return;
101 }
102 if (i==last+2) //在末尾添加新元素
103 {
104 data[last + 1] = item;
105 }
106 else
107 {
108 for (int j = last; j >= i - 1;--j )
109 {
110 data[j + 1] = data[j];//数据往后推移
111 }
112 data[i - 1] = item;
113 }
114 ++last;
115 }
116
117 //删除书序表的第i个数据
118 public T Delete(int i)
119 {
120 T tmp = default(T);
121 if (IsEmpty())
122 {
123 Console.Write("List is empty!");
124 return tmp;
125 }
126 if (i<1 || i>last+1)
127 {
128 Console.Write("Position is error!");
129 return tmp;
130 }
131 if (i==last+1)//删除末尾的数据
132 {
133 tmp = data[last--];
134 Console.WriteLine("Delete Successful! the value is {0}", tmp);
135 }
136 else
137 {
138 tmp = data[i - 1];
139 for (int j = i; j <= last;++j )
140 {
141 data[j]=data[j+1];
142 }
143 Console.WriteLine("Delete Successful! the value is {0}",tmp);
144 }
145 --last;
146 return tmp;
147 }
148
149 //获取书序表第i个数据元素
150 public T GetElemen(int i)
151 {
152 if (IsEmpty() || (i<1) || (i>last+1))
153 {
154 Console.Write("List is empty or Position is error!");
155 return default(T);
156 }
157 return data[i-1];
158 }
159
160 //在顺序表查找值为value的元素
161 public int Locate(T value)
162 {
163 if (IsEmpty())
164 {
165 Console.Write("List is empty!");
166 return -1;
167 }
168
169 int i = 0;
170 for (i = 0; i <= last;++i )
171 {
172 if (value.Equals(data[i]))
173 {
174 break;
175 }
176 }
177
178 if (i>last)
179 {
180 return -1;
181 }
182 return i;
183 }
184
185 //遍历输出所有元素
186 public void Output()
187 {
188 for (int i = 1; i <=GetLength(); i++)
189 {
190 Console.Write(GetElemen(i) + " ");
191 }
192 }
193
194 //元素倒置
195 public void ReverSeqList(SeqList<int> L)
196 {
197 int tmp = 0;
198 int len = L.GetLength();
199 for (int i =1; i <= len / 2; ++i)
200 {
201 tmp = L[i];
202 L[i] = L[len - i];
203 L[len - i] = tmp;
204 }
205 }
206 }
207 }
208
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Collections;
5
6 namespace List
7 {
8 public class SeqList<T>
9 {
10 private int maxsize;//容量
11 private T[] data;//值
12 private int last;//末指针位置
13
14 //索引器
15 public T this[int index]
16 {
17 get { return data[index]; }
18 set { data[index] = value; }
19 }
20
21 public int Last
22 {
23 get { return last; }
24 }
25
26 public int Maxsize
27 {
28 get { return maxsize; }
29 set { maxsize = value; }
30 }
31
32 //构造函数
33 public SeqList(int size)
34 {
35 data = new T[size];
36 maxsize = size;
37 last = -1;
38 }
39
40 //求顺序列表的长度
41 public int GetLength()
42 {
43 return last + 1;
44 }
45
46 //清空列表
47 public void Clear()
48 {
49 last = -1;
50 }
51
52 //判断是否为空
53 public bool IsEmpty()
54 {
55 if (last==-1)
56 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63 }
64
65 //判断顺序表是否为满
66 public bool IsFull()
67 {
68 if (last==maxsize-1)
69 {
70 return true;
71 }
72 else
73 {
74 return false;
75 }
76 }
77
78 //在顺序表的末尾添加新元素
79 public void Append(T item)
80 {
81 if (IsFull())
82 {
83 Console.Write("List is Full!");
84 return;
85 }
86 data[++last] = item;
87 }
88
89 //在顺序表中的第i个位置添加新的元素
90 public void Insert(T item, int i)
91 {
92 if (IsFull())
93 {
94 Console.Write("List is Full!");
95 return;
96 }
97 if(i<1 || i>last+2)
98 {
99 Console.Write("Position is error!~");
100 return;
101 }
102 if (i==last+2) //在末尾添加新元素
103 {
104 data[last + 1] = item;
105 }
106 else
107 {
108 for (int j = last; j >= i - 1;--j )
109 {
110 data[j + 1] = data[j];//数据往后推移
111 }
112 data[i - 1] = item;
113 }
114 ++last;
115 }
116
117 //删除书序表的第i个数据
118 public T Delete(int i)
119 {
120 T tmp = default(T);
121 if (IsEmpty())
122 {
123 Console.Write("List is empty!");
124 return tmp;
125 }
126 if (i<1 || i>last+1)
127 {
128 Console.Write("Position is error!");
129 return tmp;
130 }
131 if (i==last+1)//删除末尾的数据
132 {
133 tmp = data[last--];
134 Console.WriteLine("Delete Successful! the value is {0}", tmp);
135 }
136 else
137 {
138 tmp = data[i - 1];
139 for (int j = i; j <= last;++j )
140 {
141 data[j]=data[j+1];
142 }
143 Console.WriteLine("Delete Successful! the value is {0}",tmp);
144 }
145 --last;
146 return tmp;
147 }
148
149 //获取书序表第i个数据元素
150 public T GetElemen(int i)
151 {
152 if (IsEmpty() || (i<1) || (i>last+1))
153 {
154 Console.Write("List is empty or Position is error!");
155 return default(T);
156 }
157 return data[i-1];
158 }
159
160 //在顺序表查找值为value的元素
161 public int Locate(T value)
162 {
163 if (IsEmpty())
164 {
165 Console.Write("List is empty!");
166 return -1;
167 }
168
169 int i = 0;
170 for (i = 0; i <= last;++i )
171 {
172 if (value.Equals(data[i]))
173 {
174 break;
175 }
176 }
177
178 if (i>last)
179 {
180 return -1;
181 }
182 return i;
183 }
184
185 //遍历输出所有元素
186 public void Output()
187 {
188 for (int i = 1; i <=GetLength(); i++)
189 {
190 Console.Write(GetElemen(i) + " ");
191 }
192 }
193
194 //元素倒置
195 public void ReverSeqList(SeqList<int> L)
196 {
197 int tmp = 0;
198 int len = L.GetLength();
199 for (int i =1; i <= len / 2; ++i)
200 {
201 tmp = L[i];
202 L[i] = L[len - i];
203 L[len - i] = tmp;
204 }
205 }
206 }
207 }
208
测试代码:
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5
6 namespace List
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 SeqList<string> List=new SeqList<string>(4);
13 List.Append("hello");
14 List.Append("World");
15 List.Append("!");
16
17 string string2 = List.GetElemen(0);
18 Console.WriteLine(string2);
19 int length = List.GetLength();
20 Console.WriteLine("List 的长度为{0}个元素",length);
21
22 bool IsEmpty = List.IsEmpty();
23 Console.WriteLine("List 为空吗?{0}", IsEmpty);
24
25 bool IsFull = List.IsFull();
26 Console.WriteLine("List 为满吗?{0}", IsFull);
27
28 List.Insert("tao", 1);
29 string InserString=List.GetElemen(1);
30 Console.WriteLine("The Insert Element is {0}", InserString);
31
32 IsFull = List.IsFull();
33 Console.WriteLine("List 为满吗?{0}", IsFull);
34
35 // List.Delete(4);
36
37 int locate = List.Locate("World");
38 Console.WriteLine("The location is {0}", locate);
39
40 List.Output();
41 Console.WriteLine();
42
43 ////////////////////////////////////////////////
44 SeqList<int> NumberList = new SeqList<int>(8);
45 NumberList.Append(11);
46 NumberList.Append(23);
47 NumberList.Append(36);
48 NumberList.Append(45);
49 NumberList.Append(80);
50 NumberList.Append(60);
51 NumberList.Append(40);
52 NumberList.Append(6);
53
54 NumberList.Output();
55 Console.WriteLine();
56 NumberList.ReverSeqList(NumberList);
57
58 NumberList.Output();
59 }
60 }
61 }
62
2 using System.Collections.Generic;
3 using System.Text;
4
5
6 namespace List
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 SeqList<string> List=new SeqList<string>(4);
13 List.Append("hello");
14 List.Append("World");
15 List.Append("!");
16
17 string string2 = List.GetElemen(0);
18 Console.WriteLine(string2);
19 int length = List.GetLength();
20 Console.WriteLine("List 的长度为{0}个元素",length);
21
22 bool IsEmpty = List.IsEmpty();
23 Console.WriteLine("List 为空吗?{0}", IsEmpty);
24
25 bool IsFull = List.IsFull();
26 Console.WriteLine("List 为满吗?{0}", IsFull);
27
28 List.Insert("tao", 1);
29 string InserString=List.GetElemen(1);
30 Console.WriteLine("The Insert Element is {0}", InserString);
31
32 IsFull = List.IsFull();
33 Console.WriteLine("List 为满吗?{0}", IsFull);
34
35 // List.Delete(4);
36
37 int locate = List.Locate("World");
38 Console.WriteLine("The location is {0}", locate);
39
40 List.Output();
41 Console.WriteLine();
42
43 ////////////////////////////////////////////////
44 SeqList<int> NumberList = new SeqList<int>(8);
45 NumberList.Append(11);
46 NumberList.Append(23);
47 NumberList.Append(36);
48 NumberList.Append(45);
49 NumberList.Append(80);
50 NumberList.Append(60);
51 NumberList.Append(40);
52 NumberList.Append(6);
53
54 NumberList.Output();
55 Console.WriteLine();
56 NumberList.ReverSeqList(NumberList);
57
58 NumberList.Output();
59 }
60 }
61 }
62

浙公网安备 33010602011771号