Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. struct Node{
  7. Node* minKey;
  8. Node* next;
  9. int key;
  10. };
  11.  
  12. bool empty(Node* top){
  13. if (top == NULL){
  14. return true;
  15. }
  16. return false;
  17. }
  18.  
  19. int size(Node* top){
  20. int res = 0;
  21. while(top != NULL){
  22. ++res;
  23. top = top -> next;
  24. }
  25. return res;
  26. }
  27.  
  28. bool isLess(Node* x, Node* y){
  29. if ((x -> key) <= (y -> key)){
  30. return true;
  31. }
  32. return false;
  33. }
  34.  
  35. void pop(Node* &top){
  36. if (empty(top)){
  37. cout << "error" << '\n';
  38. return ;
  39. }
  40. else {
  41. cout << (top -> key) << '\n';
  42. Node* temp = top;
  43. top = top -> next;
  44. delete temp;
  45. return;
  46. }
  47. }
  48.  
  49. void push(Node *&top, int key){
  50. Node* newNode = new Node;
  51. newNode -> key = key;
  52. newNode -> next = top;
  53. if (top == NULL){
  54. newNode -> minKey = newNode;
  55. }
  56. else if (isLess(newNode, top -> minKey)){
  57. newNode -> minKey = newNode;
  58. }
  59. else {
  60. newNode -> minKey = top -> minKey;
  61. }
  62. top = newNode;
  63. cout << "ok" << '\n';
  64. }
  65.  
  66. void back(Node* top){
  67. if (empty(top)){
  68. cout << "error" << '\n';
  69. return;
  70. }
  71. cout << (top -> key) << '\n';
  72. }
  73.  
  74. void clear(Node* &top){
  75. Node* temp;
  76. while(top != NULL){
  77. temp = top;
  78. top = top -> next;
  79. delete temp;
  80. }
  81. cout << "ok" << '\n';
  82. }
  83.  
  84. void min(Node* top){
  85. if (empty(top)){
  86. cout << "error" << '\n';
  87. return ;
  88. }
  89. cout << (top -> minKey) -> key << '\n';
  90. }
  91.  
  92. int main(){
  93. int n;
  94. cin >> n;
  95. Node* top;
  96. top = NULL;
  97. char temp[10];
  98. for (int i = 0; i < n; ++i){
  99. char command[10] = "";
  100. cin >> temp;
  101. strcat(command, temp);
  102. if (!strcmp(command, "push")){
  103. int key;
  104. cin >> key;
  105. push(top, key);
  106. }
  107. else if(!strcmp(command, "pop")){
  108. pop(top);
  109. }
  110. else if(!strcmp(command, "size")) {
  111. cout << size(top) << '\n';
  112. }
  113. else if(!strcmp(command, "back")) {
  114. back(top);
  115. }
  116. else if(!strcmp(command, "min")) {
  117. min(top);
  118. }
  119. else if(!strcmp(command, "clear")){
  120. clear(top);
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement