数据结构使用教程 代码
数据结构和算法实践
1 //2.4-2,3,5
2 //get max value in linked list
3 ElemType MaxValue(LNode * head)
4 {
5 if(head==NULL)
6 {
7 cerr<<"Linked list is empty!"<<endl;
8 exit(1);
9 }
10 ElemType max=head->data;
11 LNode* p=head->next;//用temp指针p指向链表
12 while(p!=NULL)
13 {
14 if(p->data>max)
15 max=p->data;
16 p=p->next; //指向下一个指针
17 }
18 return max;
19 }
20
21 //
22 int Count(LNode* head,ElemType x)
23 {
24 int n=0;
25 while(head!=NULL)
26 {
27 if(head->data==x)
28 n++;
29 head=head->next;
30 }
31 return n;
32 }
33
34 LNode* Merge(LNode* &p1,LNode* &p2)
35 {
36 LNode a; //便于处理,创建结果有序表头附加节点,
37 LNode* p=&a;//p指向结果链表的表尾节点,初始指向表头附加节点
38 p->next=NULL;
39 while(p1!=NULL&&p2!=NULL)
40 {
41 if(p1->data<=p2->data)
42 {
43 p->next=p1;
44 p1=p1->next;
45 }else{
46 p->next=p2;
47 p2=p2->next;
48 }
49 p=p->next;
50 }
51
52 if(p1!=NULL) p->next=p1;
53 if(p2!=NULL) p->next=p2;
54
55 p1=p2=NULL;
56 return a.next;
57 }
58
59 template<class ElemType>
60 class Stack{
61 ElemType *stack;
62 int top;
63 int MaxSize;
64 public:
65 Stack();
66 Stack(Stack& s);
67 Stack& operator=(Stack& s);
68 void Push(ElemType item);
69 ElemType Pop();
70 ElemType Peek();
71 bool EmptyStack();
72 ~Stack();
73 };
74
75 template<class ElemType>
76 ElemType Stack<ElemType>::Pop()
77 {
78 ///write something
79 }
80
81 template<class ElemType>
82 ElemType Stack<ElemType>::Peek()
83 {
84 ///write something
85 }
86
87 template<class ElemType>
88 void Stack<ElemType>::Push(ElemType item)
89 {
90 ///write something
91 }
92
93 template<class ElemType>
94 bool Stack<ElemType>::EmptyStack()
95 {
96 return top==-1;
97 }
98 //4.3 -2,3
99 //后缀表达式的求值定义 ,操作数栈为引用参数
100 double Compute(Stack<double> &S,char* str)
101 {
102 double x,y;// 保存浮点数
103 int i=0;
104 while(str[i]){
105 if(str[i]=='')
106 {
107 i++;continue;
108 }
109 switch(str[i]){
110 case '+':
111 x=S.Pop()+S.Pop();
112 i++;
113 break;
114 case '-':
115 x=S.Pop();
116 x=S.Pop()-x;
117 i++;
118 break;
119 case '*':
120 x=S.Pop()*S.Pop();
121 i++;
122 break;
123 case '/':
124 x=S.Pop();
125 if(x!=0.0) x=S.Pop()/x;
126 else
127 {
128 cerr<<"Divide by zero!"<<endl;
129 exit(1);
130 }
131 i++;
132 break;
133 default:
134 x=0;
135 while(str[i]>=48&&str[i]<=57){
136 x=x*10+str[i]-48;i++;
137 }
138 if(str[i]=='.'){
139 i++;
140 y=0;
141 double j=10.0;
142 while(str[i]>=48&&str[i]<=57){
143 y=y+(str[i]-48)/j;
144 i++;j*=10;
145 }
146 x+=y;
147 }
148 }
149 S.Push(x);
150 }
151 if(S.EmptyStack())
152 {
153 cerr<<"Stack is empty!"<<endl;
154 exit(1);
155 }
156 x=S.Pop();
157 if(S.EmptyStack()) return x;
158 else
159 {
160 cerr<<"expression error!"<<endl;
161 exit(1);
162 }
163 }
164
165 //返回运算符op的优先级数值
166 int Precedence(char op)
167 {
168 switch(op)
169 {
170 case '+':
171 case '-':
172 return 1;
173 case '*':
174 case '/':
175 return 2;
176 case '(':
177 case '@':
178 default:
179 return 0;
180 }
181 }
182
183 //中缀表达式转换成后缀表达式 运算符栈为引用参数
184 void Change(Stack<char>& R,char* s1,char* s2)
185 {
186 R.Push('@');
187 int i=0,j=0;
188 char ch=s1[i];
189 while(ch!='\0')
190 {
191 if(ch=='') ch=s1[++i];
192 else if(ch=='('){
193 R.Push(ch);ch=s1[++i];
194 }
195 else if(ch==')'){
196 while(R.Peek()!='(') {s2[j++]=R.Pop();}
197 R.Pop(); // 删除站顶的左括号
198 ch=s1[++i];
199 }
200 else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
201
202 char w=R.Peek();
203 while(Precedence(w)>=Precedence(ch))
204 {
205 s2[j++]=w;R.Pop();
206 w=R.Peek();
207 }
208 R.Push(ch);
209 ch=s1[++i];
210 } else{
211 if((ch<'0'||ch>'9')&&ch!='.'){
212 cout<<"error"<<endl;
213 exit(1);
214 }
215 while((ch<'0'||ch>'9')&&ch!='.'){
216 s2[j++]=ch;
217 ch=s1[++i];
218 }
219 s2[j++]='';
220 }
221
222 ch=R.Pop();
223 while(ch!='@'){
224 if(ch=='(')
225 {
226 cerr<<"expression error"<<endl;exit(1);
227 }else{
228 s2[j++]=ch;
229 ch=R.Pop();
230 }
231 }
232 s2[j++]='\0';
233 }
234 }
235
236 //利用辗转相除和递归的方法求两个整数的最大公约数
237 int f1(int a,int b)
238 {
239 //cout<<"a="<<a<<","<<"b="<<b<<endl;
240 if(a%b==0) return b;
241 else return f1(b,a%b);
242 }
243
244 //Fibonacci 数列递归和非递归算法
245 int Fib(int n)
246 {
247 if(n==1||n==2) return n-1; //终止递归条件
248 else return Fib(n-1)+Fib(n-2);
249 }
250 //时间复杂度O(2*n) 空间复杂度O(n)
251
252 int Fib2(int n)
253 {
254 int a,b,c;
255 a=0;b=1;
256 if(n==1||n==2) return n-1;
257 else
258 {
259 for(int i=3;i<=n;i++)
260 {
261 c=a+b;
262 a=b;
263 b=c;
264 }
265 return c;
266 }
267 }
268 //时间复杂度O(n) 空间复杂度O(1)
269
270 //编写汉诺塔问题的非递归算法
271 void Hanoi1(int n,int a,int b,int c)
272 {
273 struct items{
274 int n,a,b,c;
275 };
276 items s[10]; //used for sequence stack
277 int top=-1;
278 items temp;
279 while(top!=-1||n>=1)
280 {
281 while(1){
282 if(n==1) {cout<<a<<"=>"<<c<<endl;n=0;break;}
283 temp.n=n;temp.a=a;temp.b=b;temp.c=c;
284 top++;s[top]=temp; //enter stack
285 cout<<"n1="<<n<<endl;
286 n=n-1;int d=b;b=c;c=d;//a's value no change
287 cout<<"n2="<<n<<endl;
288 }
289 if(top!=-1){
290 temp=s[top];top--;
291 n=temp.n;a=temp.a;b=temp.b;c=temp.c;
292 cout<<a<<"->"<<c<<endl;
293 n=n-1;int d=a;a=b;b=d;//c's value no change
294 }
295 }
296 }
297
298 struct BTreeNode{
299 ElemType data;
300 BTreeNode* left;
301 BTreeNode* right;
302 };
303
304 // binary tree node count
305 int BTreeNodeCount(BTreeNode* BT)
306 {
307 if(BT==NULL) return 0;
308 else
309 return BTreeNodeCount(BT->left)+BTreeNodeCount(BT->right)+1;
310 }
311
312 // binary tree leaf count
313 int BTreeLeafCount(BTreeNode* BT)
314 {
315 if(BT==NULL) return 0;
316 else if(BT->left==NULL&&BT->right==NULL) return 1;
317 else return BTreeLeafCount(BT->left)+BTreeLeafCount(BT->right);
318 }