![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
实现部分
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace CA_HomeWork_1
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
enum returnValue
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
TRUE = 1,
10
FALSE = 0,
11
OK = 1,
12
ERROR = 0,
13
INFEASIBLE = -1,
14
OVERFLOW = -2
15
}
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public class Sqlist
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
public ElemType[] elem;
20
public int length;
21
public int listsize;
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
public class ElemType
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
public int data;
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
public class Program
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
//private const int LIST_INIT_SIZE = 100;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
//主函数
34
static void Main(string[] args)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
//初始化L
49
private static returnValue Initlist(ref Sqlist L)
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
//输出L
67
public static void ShowList(Sqlist L)
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
int i;
70
Console.Write("This list is: ");
71
for (i = 0; i < L.length; i++)
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
Console.Write("{0} ", L.elem[i].data);
74
}
75
Console.Write("\n");
76
return;
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
//取L长度
80
public static int ListLength(Sqlist L)
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
return (L.length);
83
}
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
//用e返回在L中的第i个值
86
private static returnValue GetElem(Sqlist L, int i, out int e)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
//"L"增长"len"
95
private static void LengthenList(ref Sqlist L, int len)
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
int[] La = new int[L.length + len];
98
for (int i = 0; i < L.length; i++)
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
La[i] = L.elem[i].data;
101
}
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
L.elem = new ElemType[L.length + len];
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
int j = 0;
106
foreach (int l in La)
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
L.elem[j] = new ElemType();
109
L.elem[j++].data = l;
110
}
111
L.length += len;
112
}
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
//在"L"中第"i"个位置之前插入新的数据元素"e","L"的长度加1的函数
115
private static returnValue ListInsert(ref Sqlist L, int i, int e)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
if (i == j)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
for (int k = L.length - 1; k > i; k--)
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
134
//归并La和Lb为Lc
135
private static void MergeList(Sqlist La, Sqlist Lb, ref Sqlist Lc)
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
GetElem(La, i, out ai);
148
GetElem(Lb, j, out bj);
149
if (ai <= bj)
150![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
151
ListInsert(ref Lc, ++k, ai);
152
++i;
153
}
154
else
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
ListInsert(ref Lc, ++k, bj);
157
++j;
158
}
159
}
160
while (i <= La_len)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
GetElem(La, i++, out ai);
163
ListInsert(ref Lc, ++k, ai);
164
}
165
while (j <= Lb_len)
166![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
167
GetElem(Lb, j++, out bj);
168
ListInsert(ref Lc, ++k, bj);
169
}
170
return;
171
}
172![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
173
}
174
}
175![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
有许多不足之处,希望见者指评,谢谢!
posted @
2008-05-13 20:47
悦峯
阅读(
572)
评论()
编辑
收藏
举报