Advertisement
szmelu

stos wskaz

Mar 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct stos
  4. {
  5. int wartosc;
  6. stos *next;
  7. };
  8. bool isempty(stos *top)
  9. {
  10. return top==NULL;
  11. }
  12. bool push(stos *&top, int val)
  13. {
  14. stos *nowy = new stos;
  15. nowy->next=top;
  16. nowy->wartosc=val;
  17. top=nowy;
  18. return 1;
  19. }
  20. bool pop(stos *&top, int &wart)
  21. {
  22. if(isempty(top))
  23. return 0;
  24. stos *temp = new stos;
  25. temp=top;
  26. wart=temp->wartosc;
  27. top=temp->next;
  28. delete temp;
  29. return 1;
  30. }
  31. void showtop(stos *top)
  32. {
  33. if(isempty(top))
  34. cout<<"Pusty"<<endl;
  35. else
  36. cout<<"top: "<<top->wartosc<<endl;
  37. }
  38.  
  39.  
  40.  
  41. int main()
  42. {
  43. stos *top = NULL;
  44. int val;
  45. push(top, 1);
  46. showtop(top);
  47. push(top, 2);
  48. showtop(top);
  49. push(top, 3);
  50. showtop(top);
  51. push(top, 4);
  52. showtop(top);
  53. push(top, 5);
  54. showtop(top);
  55. push(top, 6);
  56. showtop(top);
  57. push(top, 7);
  58. showtop(top);
  59. push(top, 8);
  60. showtop(top);
  61. pop(top, val);
  62. cout<<val<<endl;
  63. showtop(top);
  64. pop(top, val);
  65. cout<<val<<endl;
  66. showtop(top);
  67. pop(top, val);
  68. cout<<val<<endl;
  69. showtop(top);
  70. pop(top, val);
  71. cout<<val<<endl;
  72. showtop(top);
  73. pop(top, val);
  74. cout<<val<<endl;
  75. showtop(top);
  76. system("pause");
  77.  
  78.  
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement