线性表顺序表示的C#实现(参考数据结构(C语言版))
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace CA_HomeWork_16


{7
enum returnValue8

{9
TRUE = 1,10
FALSE = 0,11
OK = 1,12
ERROR = 0,13
INFEASIBLE = -1,14
OVERFLOW = -215
}16

17
public class Sqlist18

{19
public ElemType[] elem;20
public int length;21
public int listsize;22
}23

24
public class ElemType25

{26
public int data;27
}28

29
public class Program30

{31
//private const int LIST_INIT_SIZE = 100;32

33
//主函数34
static void Main(string[] args)35

{36
Sqlist La = new Sqlist();37
Sqlist Lb = new Sqlist();38
Sqlist Lc = new Sqlist();39
Initlist(ref La);40
ShowList(La);41
Initlist(ref Lb);42
ShowList(Lb);43
MergeList(La, Lb, ref Lc);44
ShowList(Lc);45
return;46
}47

48
//初始化L49
private static returnValue Initlist(ref Sqlist L)50

{51
int i, n;52
Console.Write("pleast input the list's Elem Number: ");53
n = Convert.ToInt32(Console.ReadLine());54
L.elem = new ElemType[n];55
for (i = 0; i < n; i++)56

{57
L.elem[i] = new ElemType();58
Console.Write("please input {0} Elem:", i + 1);59
L.elem[i].data = int.Parse(Console.ReadLine());60
}61
L.length = L.elem.Length;62
//L.listsize = LIST_INIT_SIZE;63
return (returnValue.OK);64
}65

66
//输出L67
public static void ShowList(Sqlist L)68

{69
int i;70
Console.Write("This list is: ");71
for (i = 0; i < L.length; i++)72

{73
Console.Write("{0} ", L.elem[i].data);74
}75
Console.Write("\n");76
return;77
}78

79
//取L长度80
public static int ListLength(Sqlist L)81

{82
return (L.length);83
}84

85
//用e返回在L中的第i个值86
private static returnValue GetElem(Sqlist L, int i, out int e)87

{88
e = 0;89
if (i < 1 || i > L.length) return (returnValue.ERROR);90
e = L.elem[i - 1].data;91
return (returnValue.OK);92
}93

94
//"L"增长"len"95
private static void LengthenList(ref Sqlist L, int len)96

{97
int[] La = new int[L.length + len];98
for (int i = 0; i < L.length; i++)99

{ 100
La[i] = L.elem[i].data;101
}102

103
L.elem = new ElemType[L.length + len];104

105
int j = 0;106
foreach (int l in La)107

{108
L.elem[j] = new ElemType();109
L.elem[j++].data = l;110
}111
L.length += len;112
}113

114
//在"L"中第"i"个位置之前插入新的数据元素"e","L"的长度加1的函数115
private static returnValue ListInsert(ref Sqlist L, int i, int e)116

{117
if (i < 1 || i > L.length + 1) return (returnValue.ERROR);118
LengthenList(ref L, 1);119
i -= 1;120
for (int j = 0; j < L.length; j++)121

{122
if (i == j)123

{124
for (int k = L.length - 1; k > i; k--)125

{126
L.elem[k].data = L.elem[k - 1].data;127
}128
L.elem[i].data = e;129
}130
}131
return (returnValue.OK);132
}133

134
//归并La和Lb为Lc135
private static void MergeList(Sqlist La, Sqlist Lb, ref Sqlist Lc)136

{137
int i, j, k, La_len, Lb_len;138
int ai, bj;139
i = 1;140
j = 1;141
k = 0;142
//Lc = new Sqlist();143
La_len = ListLength(La);144
Lb_len = ListLength(Lb);145
while ((i <= La_len) && (j <= Lb_len))146

{147
GetElem(La, i, out ai);148
GetElem(Lb, j, out bj);149
if (ai <= bj)150

{151
ListInsert(ref Lc, ++k, ai);152
++i;153
}154
else155

{156
ListInsert(ref Lc, ++k, bj);157
++j;158
}159
}160
while (i <= La_len)161

{162
GetElem(La, i++, out ai);163
ListInsert(ref Lc, ++k, ai);164
}165
while (j <= Lb_len)166

{167
GetElem(Lb, j++, out bj);168
ListInsert(ref Lc, ++k, bj);169
}170
return;171
}172

173
}174
}175

有许多不足之处,希望见者指评,谢谢!

浙公网安备 33010602011771号