Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct ele {
- int value;
- ele *succ;
- ele(int k) : value(k){
- succ=NULL;
- }
- ele(int k, ele* s) : value(k), succ(s){}
- };
- struct Stack {
- ele *testa;
- Stack(){
- testa=NULL;
- }
- };
- bool OEmpty(Stack* S){
- if(S->testa)
- return false;
- return true;
- }
- void OPop(Stack *S){
- if(!OEmpty(S)){
- ele *tmp=S->testa->succ;
- delete S->testa;
- S->testa=tmp;
- }
- }
- void OPush(Stack *S, ele *x){
- if(!OEmpty(S)){
- if(S->testa->value>x->value){
- OPop(S);
- OPush(S,x);
- }
- else{
- x->succ=S->testa;
- S->testa=x;
- }
- }
- else{
- //se la pila è vuota inserisco la testa
- x->succ=NULL;//e il suo succ deve essere NULL
- S->testa=x;
- }
- }
- void stampa(Stack *S){
- if(!OEmpty(S)){
- ele* p=S->testa;
- while(p!=NULL){
- cout<<p->value<<"|";
- p=p->succ;
- }
- }
- else{
- cout<<"La pila e' vuota"<<endl;
- }
- cout<<endl;
- }
- int main(){
- Stack *S=new Stack();
- cout<<OEmpty(S)<<endl;//stampa 1 perchè è vuoto
- stampa(S);
- S->testa=new ele(-1);
- cout<<OEmpty(S)<<endl;//stampa 0 perchè non è vuoto
- stampa(S);
- OPop(S);
- cout<<OEmpty(S)<<endl;//stampa 1 perchè è vuoto
- stampa(S);
- OPush(S,new ele(-1));
- OPush(S,new ele(1));
- OPush(S,new ele(4));
- OPush(S,new ele(5));
- stampa(S);
- OPush(S,new ele(3));
- stampa(S);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement