数据结构第一次作业...
顺序表的实现( C and C++):
C语言版:
1
#include
<
stdio.h
>
2 #include < malloc.h >
3 #include < stdlib.h >
4
5 #define LIST_INIT_SIZE 10
6 #define LISTINCREMENT 2
7 #define OVERFLOW -2
8 #define OK 1
9 #define ERROR 0
10
11 typedef int Status;
12 typedef int ElemType;
13
14 typedef struct {
15 int * elem;
16 int length;
17 int listsize;
18 }SqList;
19
20 Status InitList_Sq(SqList * L); // 初始化顺序表
21 Status ListInsert_Sq(SqList * L, int i,ElemType e); // 顺序表中插入元素
22 Status MergeList_Sq(SqList La,SqList Lb,SqList * Lc); // 顺序表的合并
23 Status ListPrint_Sq(SqList * L); // 顺序表元素的输出
24
25 int main()
26 {
27 SqList La,Lb,Lc;
28 int i = 1 ;
29 ElemType num = 0 ;
30 InitList_Sq( & La);
31 InitList_Sq( & Lb);
32
33 printf( " Please input the number(end by -1):\n " );
34 while ( 1 )
35 {
36 scanf( " %d " , & num);
37 if (num == - 1 )
38 break ;
39 ListInsert_Sq( & La,i,num);
40 i ++ ;
41 }
42 ListPrint_Sq( & La);
43
44 i = 1 ;
45 printf( " Please input the number(end by -1):\n " );
46 while ( true )
47 {
48 scanf( " %d " , & num);
49 if (num == - 1 )
50 break ;
51 ListInsert_Sq( & Lb,i,num);
52 i ++ ;
53 }
54 ListPrint_Sq( & Lb);
55
56 MergeList_Sq(La,Lb, & Lc); // 顺序表合并的算法函数
57
58 printf( " MergeList OK\n " );
59 ListPrint_Sq( & Lc);
60 }
61
62 Status MergeList_Sq(SqList La,SqList Lb,SqList * Lc)
63 {
64 ElemType * pa = La.elem, * pb = Lb.elem, * pc;
65 ElemType * pa_last, * pb_last;
66 Lc -> listsize = Lc -> length = La.length + Lb.length;
67 pc = Lc -> elem = (ElemType * )malloc(Lc -> listsize * sizeof (ElemType));
68 if ( ! Lc -> elem)
69 exit(OVERFLOW);
70 pa_last = La.elem + La.length - 1 ;
71 pb_last = Lb.elem + Lb.length - 1 ;
72 while (pa <= pa_last && pb <= pb_last)
73 * pc ++ = ( * pa <=* pb ? * pa ++ : * pb ++ ); // return a value,but not a good style;(<C++ Programming Style> Tom Cargill )
74 /* {
75 if(*pa<=*pb)
76 *pc++ = *pa++;
77 else
78 *pc++ = *pb++;
79 } */
80 while (pa <= pa_last) * pc ++ = * pa ++ ;
81 while (pb <= pb_last) * pc ++ = * pb ++ ;
82
83 return OK;
84 }
85
86
87
88
89
90 Status InitList_Sq(SqList * L)
91 {
92 L -> elem = (ElemType * )malloc(LIST_INIT_SIZE * sizeof (ElemType));
93 if ( ! L -> elem)
94 exit(OVERFLOW);
95 L -> length = 0 ;
96 L -> listsize = LIST_INIT_SIZE;
97 return OK;
98 }
99
100 Status ListInsert_Sq(SqList * L, int i,ElemType e)
101 {
102 ElemType * newbase, * p, * q;
103 if (i < 1 || i > L -> length + 1 )
104 return ERROR;
105 if (L -> length >= L -> listsize)
106 {
107 newbase = (ElemType * )realloc(L -> elem,(L -> listsize + LISTINCREMENT) * sizeof (ElemType));
108 if ( ! newbase)
109 exit(OVERFLOW);
110 L -> elem = newbase;
111 L -> listsize += LISTINCREMENT;
112 }
113 q = & (L -> elem[i - 1 ]);
114 for (p =& (L -> elem[L -> length - 1 ]);p >= q; -- p)
115 {
116 * (p + 1 ) = * p;
117 }
118 * q = e;
119 ++ L -> length;
120
121 return OK;
122 }
123
124 Status ListPrint_Sq(SqList * L)
125 {
126 int i = 0 ;
127 ElemType num = 0 ;
128 for (i = 0 ;i < L -> length;i ++ )
129 {
130 num = L -> elem[i];
131 printf( " No.%d\t%d\n " ,i,num);
132 }
133
134 return OK;
135 }
2 #include < malloc.h >
3 #include < stdlib.h >
4
5 #define LIST_INIT_SIZE 10
6 #define LISTINCREMENT 2
7 #define OVERFLOW -2
8 #define OK 1
9 #define ERROR 0
10
11 typedef int Status;
12 typedef int ElemType;
13
14 typedef struct {
15 int * elem;
16 int length;
17 int listsize;
18 }SqList;
19
20 Status InitList_Sq(SqList * L); // 初始化顺序表
21 Status ListInsert_Sq(SqList * L, int i,ElemType e); // 顺序表中插入元素
22 Status MergeList_Sq(SqList La,SqList Lb,SqList * Lc); // 顺序表的合并
23 Status ListPrint_Sq(SqList * L); // 顺序表元素的输出
24
25 int main()
26 {
27 SqList La,Lb,Lc;
28 int i = 1 ;
29 ElemType num = 0 ;
30 InitList_Sq( & La);
31 InitList_Sq( & Lb);
32
33 printf( " Please input the number(end by -1):\n " );
34 while ( 1 )
35 {
36 scanf( " %d " , & num);
37 if (num == - 1 )
38 break ;
39 ListInsert_Sq( & La,i,num);
40 i ++ ;
41 }
42 ListPrint_Sq( & La);
43
44 i = 1 ;
45 printf( " Please input the number(end by -1):\n " );
46 while ( true )
47 {
48 scanf( " %d " , & num);
49 if (num == - 1 )
50 break ;
51 ListInsert_Sq( & Lb,i,num);
52 i ++ ;
53 }
54 ListPrint_Sq( & Lb);
55
56 MergeList_Sq(La,Lb, & Lc); // 顺序表合并的算法函数
57
58 printf( " MergeList OK\n " );
59 ListPrint_Sq( & Lc);
60 }
61
62 Status MergeList_Sq(SqList La,SqList Lb,SqList * Lc)
63 {
64 ElemType * pa = La.elem, * pb = Lb.elem, * pc;
65 ElemType * pa_last, * pb_last;
66 Lc -> listsize = Lc -> length = La.length + Lb.length;
67 pc = Lc -> elem = (ElemType * )malloc(Lc -> listsize * sizeof (ElemType));
68 if ( ! Lc -> elem)
69 exit(OVERFLOW);
70 pa_last = La.elem + La.length - 1 ;
71 pb_last = Lb.elem + Lb.length - 1 ;
72 while (pa <= pa_last && pb <= pb_last)
73 * pc ++ = ( * pa <=* pb ? * pa ++ : * pb ++ ); // return a value,but not a good style;(<C++ Programming Style> Tom Cargill )
74 /* {
75 if(*pa<=*pb)
76 *pc++ = *pa++;
77 else
78 *pc++ = *pb++;
79 } */
80 while (pa <= pa_last) * pc ++ = * pa ++ ;
81 while (pb <= pb_last) * pc ++ = * pb ++ ;
82
83 return OK;
84 }
85
86
87
88
89
90 Status InitList_Sq(SqList * L)
91 {
92 L -> elem = (ElemType * )malloc(LIST_INIT_SIZE * sizeof (ElemType));
93 if ( ! L -> elem)
94 exit(OVERFLOW);
95 L -> length = 0 ;
96 L -> listsize = LIST_INIT_SIZE;
97 return OK;
98 }
99
100 Status ListInsert_Sq(SqList * L, int i,ElemType e)
101 {
102 ElemType * newbase, * p, * q;
103 if (i < 1 || i > L -> length + 1 )
104 return ERROR;
105 if (L -> length >= L -> listsize)
106 {
107 newbase = (ElemType * )realloc(L -> elem,(L -> listsize + LISTINCREMENT) * sizeof (ElemType));
108 if ( ! newbase)
109 exit(OVERFLOW);
110 L -> elem = newbase;
111 L -> listsize += LISTINCREMENT;
112 }
113 q = & (L -> elem[i - 1 ]);
114 for (p =& (L -> elem[L -> length - 1 ]);p >= q; -- p)
115 {
116 * (p + 1 ) = * p;
117 }
118 * q = e;
119 ++ L -> length;
120
121 return OK;
122 }
123
124 Status ListPrint_Sq(SqList * L)
125 {
126 int i = 0 ;
127 ElemType num = 0 ;
128 for (i = 0 ;i < L -> length;i ++ )
129 {
130 num = L -> elem[i];
131 printf( " No.%d\t%d\n " ,i,num);
132 }
133
134 return OK;
135 }
貌似我不该公开这个....随意了.....
C++描述:
1
/*
status.h 预定义头文件
*/
2 #pragma once
3 #include < iostream >
4 #include < malloc.h >
5
6 #define TRUE 1
7 #define FALSE 0
8 #define OK 1
9 #define ERROR 0
10 #define OVERFLOW -2
11 #define INFEASIBLE -1
12 #define LISTINCREMENT 2
13 #define LIST_INIT_SIZE 10
14
15
16 typedef int Status;
17 typedef int ElemType;
18
19 struct SqList{
20 ElemType * elem;
21 int length;
22 int listsize;
23 };
24 ----------------------------------------------
25 /* SqList.h CSqList类的声明文件 */
26 #pragma once
27 #include " Status.h "
28
29 class CSqList
30 {
31 public :
32
33 CSqList( void );
34 ~ CSqList( void );
35 public :
36 // 顺序线性表结构体
37 SqList L;
38 public :
39 // 含参数的构造函数
40 CSqList( int length, int listsize);
41 Status ListInsert( int i,ElemType e);
42 // 合并两个非递减线性表
43 Status ListUnion(CSqList & La, CSqList & Lb);
44 // 输出顺序表里的元素
45 Status ListPrint( void );
46 private :
47 // 用e返回L中的第i个元素的值
48 Status GetElem( int i, ElemType & e);
49 };
50 /* SqList.cpp CSqList类的定义文件 */
51 #pragma once
52 #include " Status.h "
53 #include " SqList.h "
54 #include < iostream >
55 using namespace std;
56
57 CSqList::CSqList( void )
58 {
59 this -> L.elem = (ElemType * )malloc(LIST_INIT_SIZE * sizeof (ElemType));
60 this -> L.length = 0 ;
61 this -> L.listsize = LIST_INIT_SIZE;
62 }
63
64 CSqList:: ~ CSqList( void )
65 {
66 free( this -> L.elem);
67 }
68
69 CSqList::CSqList( int length, int listsize)
70 {
71 this -> L.elem = (ElemType * )malloc(listsize * sizeof (ElemType));
72 this -> L.length = length;
73 this -> L.listsize = listsize;
74 }
75
76 Status CSqList::ListInsert( int i,ElemType e)
77 {
78 if (i < 1 || i > L.length + 1 )
79 exit(OVERFLOW);
80 if (L.length >= L.listsize)
81 {
82 ElemType * newbase = (ElemType * )realloc(L.elem,(L.listsize + LISTINCREMENT * sizeof (ElemType)));
83 if ( ! newbase)
84 exit(OVERFLOW);
85 L.elem = newbase;
86 L.listsize += LISTINCREMENT;
87 }
88 ElemType * q = & (L.elem[i - 1 ]);
89 for (ElemType * p = & (L.elem[L.length - 1 ]);p >= q; -- p)
90 * (p + 1 ) = * p;
91 * q = e;
92 L.length ++ ;
93
94 return OK;
95 }
96
97 // 合并两个非递减线性表
98 Status CSqList::ListUnion(CSqList & La, CSqList & Lb)
99 {
100 ElemType * pa = La.L.elem, * pb = Lb.L.elem, * pc = L.elem;
101 ElemType * pa_last = La.L.elem + La.L.length - 1 ;
102 ElemType * pb_last = Lb.L.elem + Lb.L.length - 1 ;
103
104 while (pa <= pa_last && pb <= pb_last)
105 {
106 if ( * pa <= * pb)
107 * pc ++ = * pa ++ ;
108 else
109 * pc ++ = * pb ++ ;
110 }
111 while (pa <= pa_last)
112 * pc ++ = * pa ++ ;
113 while (pb <= pb_last)
114 * pc ++ = * pb ++ ;
115
116 return OK;
117 }
118
119 // 输出顺序表里的元素
120 Status CSqList::ListPrint( void )
121 {
122 ElemType e;
123 for ( int i = 0 ;i < L.length;i ++ )
124 {
125 GetElem(i,e);
126 cout << " NO. " << i << " " << e << endl;
127 }
128
129 return OK;
130 }
131
132 Status CSqList::GetElem( int i, ElemType & e)
133 {
134 e = L.elem[i];
135
136 return OK;
137 }
138 /*
139 ///
140 Main.cpp
141 main函数所在文件
142 NewSketcher
143 08.09.27
144 XP SP1 + VS2008 下编译通过.
145
146 */
147 #pragma once
148 #include < iostream >
149 #include " SqList.h "
150 #include " Status.h "
151 using namespace std;
152
153 int main()
154 {
155 CSqList La,Lb;
156 cout << " Please input the number(end by -1): " << endl;
157 int i = 1 ;
158 while ( true )
159 {
160 ElemType num = 0 ;
161 // int i = 1;
162 cin >> num;
163 if (num == - 1 )
164 break ;
165 La.ListInsert(i,num);
166 i ++ ;
167 }
168 La.ListPrint();
169
170
171 i = 1 ;
172 cout << " Please input the number(end by -1): " << endl;
173 while ( true )
174 {
175 ElemType num = 0 ;
176 // int i = 1;
177 cin >> num;
178 if (num == - 1 )
179 break ;
180 Lb.ListInsert(i,num);
181 i ++ ;
182 }
183 Lb.ListPrint();
184 cout << endl;
185
186 int length = La.L.length + Lb.L.length;
187 int listsize = length;
188 CSqList Lc(length,listsize);
189 Lc.ListUnion(La,Lb); // 顺序表合并的算法都在这个函数里
190
191 Lc.ListPrint();
192
193 return 0 ;
194 }
195
196
2 #pragma once
3 #include < iostream >
4 #include < malloc.h >
5
6 #define TRUE 1
7 #define FALSE 0
8 #define OK 1
9 #define ERROR 0
10 #define OVERFLOW -2
11 #define INFEASIBLE -1
12 #define LISTINCREMENT 2
13 #define LIST_INIT_SIZE 10
14
15
16 typedef int Status;
17 typedef int ElemType;
18
19 struct SqList{
20 ElemType * elem;
21 int length;
22 int listsize;
23 };
24 ----------------------------------------------
25 /* SqList.h CSqList类的声明文件 */
26 #pragma once
27 #include " Status.h "
28
29 class CSqList
30 {
31 public :
32
33 CSqList( void );
34 ~ CSqList( void );
35 public :
36 // 顺序线性表结构体
37 SqList L;
38 public :
39 // 含参数的构造函数
40 CSqList( int length, int listsize);
41 Status ListInsert( int i,ElemType e);
42 // 合并两个非递减线性表
43 Status ListUnion(CSqList & La, CSqList & Lb);
44 // 输出顺序表里的元素
45 Status ListPrint( void );
46 private :
47 // 用e返回L中的第i个元素的值
48 Status GetElem( int i, ElemType & e);
49 };
50 /* SqList.cpp CSqList类的定义文件 */
51 #pragma once
52 #include " Status.h "
53 #include " SqList.h "
54 #include < iostream >
55 using namespace std;
56
57 CSqList::CSqList( void )
58 {
59 this -> L.elem = (ElemType * )malloc(LIST_INIT_SIZE * sizeof (ElemType));
60 this -> L.length = 0 ;
61 this -> L.listsize = LIST_INIT_SIZE;
62 }
63
64 CSqList:: ~ CSqList( void )
65 {
66 free( this -> L.elem);
67 }
68
69 CSqList::CSqList( int length, int listsize)
70 {
71 this -> L.elem = (ElemType * )malloc(listsize * sizeof (ElemType));
72 this -> L.length = length;
73 this -> L.listsize = listsize;
74 }
75
76 Status CSqList::ListInsert( int i,ElemType e)
77 {
78 if (i < 1 || i > L.length + 1 )
79 exit(OVERFLOW);
80 if (L.length >= L.listsize)
81 {
82 ElemType * newbase = (ElemType * )realloc(L.elem,(L.listsize + LISTINCREMENT * sizeof (ElemType)));
83 if ( ! newbase)
84 exit(OVERFLOW);
85 L.elem = newbase;
86 L.listsize += LISTINCREMENT;
87 }
88 ElemType * q = & (L.elem[i - 1 ]);
89 for (ElemType * p = & (L.elem[L.length - 1 ]);p >= q; -- p)
90 * (p + 1 ) = * p;
91 * q = e;
92 L.length ++ ;
93
94 return OK;
95 }
96
97 // 合并两个非递减线性表
98 Status CSqList::ListUnion(CSqList & La, CSqList & Lb)
99 {
100 ElemType * pa = La.L.elem, * pb = Lb.L.elem, * pc = L.elem;
101 ElemType * pa_last = La.L.elem + La.L.length - 1 ;
102 ElemType * pb_last = Lb.L.elem + Lb.L.length - 1 ;
103
104 while (pa <= pa_last && pb <= pb_last)
105 {
106 if ( * pa <= * pb)
107 * pc ++ = * pa ++ ;
108 else
109 * pc ++ = * pb ++ ;
110 }
111 while (pa <= pa_last)
112 * pc ++ = * pa ++ ;
113 while (pb <= pb_last)
114 * pc ++ = * pb ++ ;
115
116 return OK;
117 }
118
119 // 输出顺序表里的元素
120 Status CSqList::ListPrint( void )
121 {
122 ElemType e;
123 for ( int i = 0 ;i < L.length;i ++ )
124 {
125 GetElem(i,e);
126 cout << " NO. " << i << " " << e << endl;
127 }
128
129 return OK;
130 }
131
132 Status CSqList::GetElem( int i, ElemType & e)
133 {
134 e = L.elem[i];
135
136 return OK;
137 }
138 /*
139 ///
140 Main.cpp
141 main函数所在文件
142 NewSketcher
143 08.09.27
144 XP SP1 + VS2008 下编译通过.
145
146 */
147 #pragma once
148 #include < iostream >
149 #include " SqList.h "
150 #include " Status.h "
151 using namespace std;
152
153 int main()
154 {
155 CSqList La,Lb;
156 cout << " Please input the number(end by -1): " << endl;
157 int i = 1 ;
158 while ( true )
159 {
160 ElemType num = 0 ;
161 // int i = 1;
162 cin >> num;
163 if (num == - 1 )
164 break ;
165 La.ListInsert(i,num);
166 i ++ ;
167 }
168 La.ListPrint();
169
170
171 i = 1 ;
172 cout << " Please input the number(end by -1): " << endl;
173 while ( true )
174 {
175 ElemType num = 0 ;
176 // int i = 1;
177 cin >> num;
178 if (num == - 1 )
179 break ;
180 Lb.ListInsert(i,num);
181 i ++ ;
182 }
183 Lb.ListPrint();
184 cout << endl;
185
186 int length = La.L.length + Lb.L.length;
187 int listsize = length;
188 CSqList Lc(length,listsize);
189 Lc.ListUnion(La,Lb); // 顺序表合并的算法都在这个函数里
190
191 Lc.ListPrint();
192
193 return 0 ;
194 }
195
196
故意不加析构函数...故意不用属性...其实还是有粗糙的地方,封装就有些不科学....
------------by NewKetcher
Time: 08.10.05 22:55