1, in the data structure of the book is mostly pseudo-code, did not give a complete program, in this small series to everyone a complete simple into the stack operation.
2, the realization of the function is : to observe the two numbers in the stack and out of the stack sequence.
3. Program and Code:
#include <iostream>
#include <stdlib.h>
using namespace Std;
struct stack{
int* Base;
int top;
int stack_size;
};
int Inistack (Stack &s,int size) {
S.base= (int*) malloc (size *sizeof (int));
if (S.base==null) return 0;
S.top=0;
S.stack_size=size;
return 1;
}
int Push (Stack &s,int e) {
S.base[s.top]=e;
s.top++;
return 1;
}
int Pop (Stack &s) {
if (s.base[s.top]==0) return 0;
s.top--;
return s.base[s.top];
}
int exchange (int x,int y) {
Stack s;
Inistack (s,3);
Push (S,X);
Push (S,y);
X=pop (s);
Y=pop (s);
cout<< "x=" <<x<< "y=" <<y<<endl;
return 1;
}
int main () {
int x, y;
cout<< "Please enter the value of x:" <<endl;
cin>>x;
cout<< "Please enter the value of y:" <<endl;
cin>>y;
Exchange (x, y);
return 0;
}
Operation Result:
Please enter the value of x:
45
Please enter a value for y:
56
X=56y=45
Please press any key to continue ...
This article is from the "jinleijie_123" blog, make sure to keep this source http://3998540.blog.51cto.com/3988540/1696534
C + + implementation of two elements in the stack and the order of the stack