简单算法实现

  • content
    {:toc}

复数抽象数据类型的定义及操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
#define ERROR 0
typedef float status;

typedef struct //抽象数据类型的存储结构
{
float Realpart;
float Imagepart;
}Complex;

void Create(Complex &C,float x,float y) //构建复数
{
C.Realpart=x;
C.Imagepart=y;
}

status GetReal(Complex C) //取实部
{
return C.Realpart;
}

status GetImag(Complex C) //取虚部
{
return C.Imagepart;
}

Complex Add(Complex C1,Complex C2) //复数相加
{
Complex sum;
sum.Realpart=C1.Realpart+C2.Realpart;
sum.Imagepart=C1.Imagepart+C2.Imagepart;
return sum;
}

Complex Sub(Complex C1,Complex C2) //复数相减
{
Complex dif;
dif.Realpart=C1.Realpart-C2.Realpart;
dif.Imagepart=C1.Imagepart-C2.Imagepart;
return dif;
}

void main()
{
Complex C1,C2,C3,C4;
cout<<"请输入第一个复数!"<<"\n";
cin>>C1.Realpart;
cin>>C1.Imagepart;
cout<<"请输入第二个复数!"<<"\n";
cin>>C2.Realpart;
cin>>C2.Imagepart;
cout<<"C1实部为:"<<GetReal(C1)<<"C1虚部为:"<<GetImag(C1)<<"\n";
C3=Add(C1,C2);
C4=Sub(C1,C2);
cout<<"和为:"<<C3.Realpart<<"+"<<C3.Imagepart<<"i"<<"\n";
cout<<"差为:"<<C4.Realpart<<"+"<<C4.Imagepart<<"i";
}

两个有序表的合并(顺序表)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
#define ERROR 0
#define MAXSIZE 100
#define OK 1
typedef int status;
typedef int Elemtype;

typedef struct //抽象数据类型的存储结构
{
Elemtype *elem;
int length;
}SqList;

status initlist(SqList &L)
{
L.elem=new Elemtype[MAXSIZE];
if(!L.elem) return ERROR;
L.length=0;
return OK;
}

SqList createlist(int n)
{
SqList L;
initlist(L);
for(int i=0;i<n;i++)
{
cin>>L.elem[i];
++L.length;
}
return L;
}

void merge(SqList L1,SqList L2,SqList &L)
{
int *p,*p1,*p2,*p1_last,*p2_last;
initlist(L);
L.length=L1.length+L2.length;
p=L.elem;p1=L1.elem;p2=L2.elem;
p1_last=L1.elem+L1.length-1;
p2_last=L2.elem+L2.length-1;
while((p1<=p1_last)&&(p2<=p2_last))
{
if(*p1<=*p2) *p++=*p1++;
else *p++=*p2++;
}
while(p1<=p1_last) *p++=*p1++;
while(p2<=p2_last) *p++=*p2++;

}
void main()
{
SqList L1,L2,L;
initlist(L1);
initlist(L2);
initlist(L);
cout<<"请输入4个数字!"<<"\n";
L1=createlist(4);
cout<<"请输入3个数字!"<<"\n";
L2=createlist(3);
merge(L1,L2,L);
for(int j=0;j<L.length;j++)
{
cout<<L.elem[j];
}
}

有序表的合并(单链表)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
using namespace std;
#define ERROR 0
#define MAXSIZE 100
#define OK 1
typedef int status;
typedef int Elemtype;

typedef struct LNode
{
Elemtype data;
struct LNode *next;
}LNode,*Linklist;

status init(Linklist &L) //链表初始化
{
L=new LNode; //为链表开辟内存空间
L->next=NULL;
return OK;
}

void create_L(Linklist &L,int n) //后插法
{
LNode *p,*r;
L=new LNode; //开辟内存空间
L->next=NULL; //创建一个链表
r=L;
for(int i=0;i<n;i++)
{
p=new LNode; //创建结点
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}

void MergeList_L(Linklist La,Linklist Lb,Linklist &Lc){ //合并有序链表
LNode *pa,*pb,*pc,*a;
init(Lc); //******这是必须的!!!
pa=La->next; pb=Lb->next;
pc=Lc=La; //用La的头结点作为Lc的头结点
while(pa && pb){
if(pa->data<=pb->data){ pc->next=pa;pc=pa;pa=pa->next;}
else{pc->next=pb; pc=pb; pb=pb->next;}
pc->next=pa?pa:pb; //插入剩余段
//delete a; //释放Lb的头结点
}
}

void main()
{
LNode *p,*L1,*L2,*L,*p1,*p2;
cout<<"请输入3个数字"<<"\n";
create_L(L1,3);
cout<<"创建成功!"<<"第一个序列是:";
p1=L1->next;
while(p1)
{
cout<<p1->data;
p1=p1->next;
}
cout<<"请输入4个数字"<<"\n";
create_L(L2,4);
cout<<"创建成功!"<<"第二个序列是:";
p2=L2->next;
while(p2)
{
cout<<p2->data;
p2=p2->next;
}
MergeList_L(L1,L2,L);
p=L->next;
cout<<"输出为"<<"";
while(p)
{
cout<<p->data;
p=p->next;
}
}
----------------------------- 我是有底线 ~..~ ------------------------------