Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct ele {
  4. int value;
  5. ele *succ;
  6. ele(int k) : value(k){
  7. succ=NULL;
  8. }
  9. ele(int k, ele* s) : value(k), succ(s){}
  10. };
  11. struct Stack {
  12. ele *testa;
  13. Stack(){
  14. testa=NULL;
  15. }
  16. };
  17. bool OEmpty(Stack* S){
  18. if(S->testa)
  19. return false;
  20. return true;
  21. }
  22. void OPop(Stack *S){
  23. if(!OEmpty(S)){
  24. ele *tmp=S->testa->succ;
  25. delete S->testa;
  26. S->testa=tmp;
  27. }
  28. }
  29. void OPush(Stack *S, ele *x){
  30. if(!OEmpty(S)){
  31. if(S->testa->value>x->value){
  32. OPop(S);
  33. OPush(S,x);
  34. }
  35. else{
  36. x->succ=S->testa;
  37. S->testa=x;
  38. }
  39. }
  40. else{
  41. //se la pila è vuota inserisco la testa
  42. x->succ=NULL;//e il suo succ deve essere NULL
  43. S->testa=x;
  44. }
  45. }
  46.  
  47. void stampa(Stack *S){
  48. if(!OEmpty(S)){
  49. ele* p=S->testa;
  50. while(p!=NULL){
  51. cout<<p->value<<"|";
  52. p=p->succ;
  53. }
  54. }
  55. else{
  56. cout<<"La pila e' vuota"<<endl;
  57. }
  58. cout<<endl;
  59. }
  60. int main(){
  61. Stack *S=new Stack();
  62. cout<<OEmpty(S)<<endl;//stampa 1 perchè è vuoto
  63. stampa(S);
  64. S->testa=new ele(-1);
  65. cout<<OEmpty(S)<<endl;//stampa 0 perchè non è vuoto
  66. stampa(S);
  67. OPop(S);
  68. cout<<OEmpty(S)<<endl;//stampa 1 perchè è vuoto
  69. stampa(S);
  70. OPush(S,new ele(-1));
  71. OPush(S,new ele(1));
  72. OPush(S,new ele(4));
  73. OPush(S,new ele(5));
  74. stampa(S);
  75. OPush(S,new ele(3));
  76. stampa(S);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement