1
2 #include < stdio.h >
3 #include < stdlib.h >
4 #include < malloc.h >
5
6 #define OK 1
7 #define ERROR 0
8 #define TRUE 1
9 #define FALSE 0
10
11 #define ElemType int
12 #define MAXSIZE 100 /*此处的宏定义常量表示线性表可能达到的最大长度*/
13 typedef struct
14 {
15 ElemType elem[MAXSIZE]; /* 线性表占用的数组空间 */
16 int last; /* 记录线性表中最后一个元素在数组elem[ ]中的位置(下标值),空表置为-1 */
17 }SeqList;
18
19 void merge(SeqList * LA, SeqList * LB, SeqList * LC)
20 {
21 int i,j,k;
22 i = 0 ;j = 0 ;k = 0 ;
23 while (i <= LA -> last && j <= LB -> last)
24 if (LA -> elem[i] <= LB -> elem[j])
25 {
26 LC -> elem[k] = LA -> elem[i];
27 i ++ ;
28 k ++ ;
29 }
30 else
31 {
32 LC -> elem[k] = LB -> elem[j];
33 j ++ ;
34 k ++ ;
35 }
36 while (i <= LA -> last) /* 当表LA有剩余元素时,则将表LA余下的元素赋给表LC */
37 {
38 LC -> elem[k] = LA -> elem[i];
39 i ++ ;
40 k ++ ;
41 }
42 while (j <= LB -> last) /* 当表LB有剩余元素时,则将表LB余下的元素赋给表LC */
43 {
44 LC -> elem[k] = LB -> elem[j];
45 j ++ ;
46 k ++ ;
47 }
48 LC -> last = LA -> last + LB -> last + 1 ; /* LC的长度等于两个栈的长度+1 */
49 }
50
51
52 void main()
53 {
54 SeqList * la, * lb, * lc;
55 int r;
56 int i;
57
58 la = (SeqList * )malloc( sizeof (SeqList));
59 printf( " please input the stack A 's length: " );
60 scanf( " %d " , & r);
61 la -> last = r - 1 ;
62 printf( " please enter the numbers of stack A:\n " );
63 for (i = 0 ; i <= la -> last; i ++ )
64 {
65 scanf( " %d " , & la -> elem[i]);
66 }
67
68
69 lb = (SeqList * )malloc( sizeof (SeqList));
70 printf( " please input the stack B 's length: " );
71 scanf( " %d " , & r);
72 lb -> last = r - 1 ;
73 printf( " please enter the numbers of stack B:\n " );
74 for (i = 0 ; i <= lb -> last; i ++ )
75 {
76 scanf( " %d " , & lb -> elem[i]);
77 }
78
79 lc = (SeqList * )malloc( sizeof (SeqList));
80 merge(la,lb,lc);
81 printf( " Join the two stacks is:\n " );
82 for (i = 0 ; i <= lc -> last; i ++ )
83 {
84 printf( " %d " ,lc -> elem[i]);
85 }
86 }
87
2 #include < stdio.h >
3 #include < stdlib.h >
4 #include < malloc.h >
5
6 #define OK 1
7 #define ERROR 0
8 #define TRUE 1
9 #define FALSE 0
10
11 #define ElemType int
12 #define MAXSIZE 100 /*此处的宏定义常量表示线性表可能达到的最大长度*/
13 typedef struct
14 {
15 ElemType elem[MAXSIZE]; /* 线性表占用的数组空间 */
16 int last; /* 记录线性表中最后一个元素在数组elem[ ]中的位置(下标值),空表置为-1 */
17 }SeqList;
18
19 void merge(SeqList * LA, SeqList * LB, SeqList * LC)
20 {
21 int i,j,k;
22 i = 0 ;j = 0 ;k = 0 ;
23 while (i <= LA -> last && j <= LB -> last)
24 if (LA -> elem[i] <= LB -> elem[j])
25 {
26 LC -> elem[k] = LA -> elem[i];
27 i ++ ;
28 k ++ ;
29 }
30 else
31 {
32 LC -> elem[k] = LB -> elem[j];
33 j ++ ;
34 k ++ ;
35 }
36 while (i <= LA -> last) /* 当表LA有剩余元素时,则将表LA余下的元素赋给表LC */
37 {
38 LC -> elem[k] = LA -> elem[i];
39 i ++ ;
40 k ++ ;
41 }
42 while (j <= LB -> last) /* 当表LB有剩余元素时,则将表LB余下的元素赋给表LC */
43 {
44 LC -> elem[k] = LB -> elem[j];
45 j ++ ;
46 k ++ ;
47 }
48 LC -> last = LA -> last + LB -> last + 1 ; /* LC的长度等于两个栈的长度+1 */
49 }
50
51
52 void main()
53 {
54 SeqList * la, * lb, * lc;
55 int r;
56 int i;
57
58 la = (SeqList * )malloc( sizeof (SeqList));
59 printf( " please input the stack A 's length: " );
60 scanf( " %d " , & r);
61 la -> last = r - 1 ;
62 printf( " please enter the numbers of stack A:\n " );
63 for (i = 0 ; i <= la -> last; i ++ )
64 {
65 scanf( " %d " , & la -> elem[i]);
66 }
67
68
69 lb = (SeqList * )malloc( sizeof (SeqList));
70 printf( " please input the stack B 's length: " );
71 scanf( " %d " , & r);
72 lb -> last = r - 1 ;
73 printf( " please enter the numbers of stack B:\n " );
74 for (i = 0 ; i <= lb -> last; i ++ )
75 {
76 scanf( " %d " , & lb -> elem[i]);
77 }
78
79 lc = (SeqList * )malloc( sizeof (SeqList));
80 merge(la,lb,lc);
81 printf( " Join the two stacks is:\n " );
82 for (i = 0 ; i <= lc -> last; i ++ )
83 {
84 printf( " %d " ,lc -> elem[i]);
85 }
86 }
87