Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- struct Node{
- Node* minKey;
- Node* next;
- int key;
- };
- bool empty(Node* top){
- if (top == NULL){
- return true;
- }
- return false;
- }
- int size(Node* top){
- int res = 0;
- while(top != NULL){
- ++res;
- top = top -> next;
- }
- return res;
- }
- bool isLess(Node* x, Node* y){
- if ((x -> key) <= (y -> key)){
- return true;
- }
- return false;
- }
- void pop(Node* &top){
- if (empty(top)){
- cout << "error" << '\n';
- return ;
- }
- else {
- cout << (top -> key) << '\n';
- Node* temp = top;
- top = top -> next;
- delete temp;
- return;
- }
- }
- void push(Node *&top, int key){
- Node* newNode = new Node;
- newNode -> key = key;
- newNode -> next = top;
- if (top == NULL){
- newNode -> minKey = newNode;
- }
- else if (isLess(newNode, top -> minKey)){
- newNode -> minKey = newNode;
- }
- else {
- newNode -> minKey = top -> minKey;
- }
- top = newNode;
- cout << "ok" << '\n';
- }
- void back(Node* top){
- if (empty(top)){
- cout << "error" << '\n';
- return;
- }
- cout << (top -> key) << '\n';
- }
- void clear(Node* &top){
- Node* temp;
- while(top != NULL){
- temp = top;
- top = top -> next;
- delete temp;
- }
- cout << "ok" << '\n';
- }
- void min(Node* top){
- if (empty(top)){
- cout << "error" << '\n';
- return ;
- }
- cout << (top -> minKey) -> key << '\n';
- }
- int main(){
- int n;
- cin >> n;
- Node* top;
- top = NULL;
- char temp[10];
- for (int i = 0; i < n; ++i){
- char command[10] = "";
- cin >> temp;
- strcat(command, temp);
- if (!strcmp(command, "push")){
- int key;
- cin >> key;
- push(top, key);
- }
- else if(!strcmp(command, "pop")){
- pop(top);
- }
- else if(!strcmp(command, "size")) {
- cout << size(top) << '\n';
- }
- else if(!strcmp(command, "back")) {
- back(top);
- }
- else if(!strcmp(command, "min")) {
- min(top);
- }
- else if(!strcmp(command, "clear")){
- clear(top);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement