irapilguy

Untitled

Nov 10th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4. struct Queue {
  5.     int data;
  6.     Queue *prev;
  7.     Queue(int x) {
  8.         data = x;
  9.         prev = NULL;
  10.     }
  11. };
  12. Queue *first = NULL;
  13. Queue *last = NULL;
  14. void push(int x) {
  15.     Queue *t = new Queue(x);
  16.     if (first == NULL) {
  17.         first = t;
  18.         last = t;
  19.         return;
  20.     }
  21.     last->prev = t;
  22.     last = t;
  23. }
  24. int pop() {
  25.     if (first == NULL) return 0;
  26.     int res = first->data;
  27.     first = first->prev;
  28.     return res;
  29. }
  30. int front() {
  31.     if (first != NULL) return first->data;
  32.     return 0;
  33. }
  34. int res = 0;
  35. int size(Queue *t) {
  36.     if (t != NULL) {
  37.         res++;
  38.         size(t->prev);
  39.     }
  40.     if (t == NULL) return 0;
  41.     return res;
  42. }
  43. void clear(Queue *t) {
  44.     if (t != NULL) {
  45.         clear(t->prev);
  46.         pop();
  47.  
  48.     }
  49. }
  50. int compare(char *str1, char *str2) {
  51.     int i = 0;
  52.     while (str1[i] != '\0' || str2[i] != '\0') {
  53.         if (str1[i] == str2[i]) {
  54.             i++;
  55.         }
  56.         else {
  57.             return 0;
  58.         }
  59.     }
  60.     return 1;
  61.  
  62. }
  63. int main() {
  64.     char command[10] = { 0 };
  65.     int i = 0;
  66.     int value;
  67.     while (compare(command, "exit") != 1) {
  68.         cin >> command;
  69.         if (compare(command, "push") == 1) {
  70.             cin >> value;
  71.             push(value);
  72.             cout << "ok" << "\n";
  73.         }
  74.         if (compare(command, "pop") == 1) {
  75.             if (pop() == 0) cout << "error"<<"\n";
  76.             else cout << pop() << "\n";
  77.         }
  78.         if (compare(command, "front") == 1) {
  79.             if (front() == 0) cout << "error" << "\n";
  80.             else cout << front() << "\n";
  81.         }
  82.         if (compare(command, "size") == 1) {
  83.             cout << size(first) << "\n";
  84.             res = 1;
  85.         }
  86.         if (compare(command, "exit") == 1) {
  87.             cout << "bye";
  88.         }
  89.         if (compare(command, "clear") == 1) {
  90.             clear(first);
  91.             cout << "ok" << "\n";
  92.         }
  93.     }
  94.     return 0;
  95. }
Add Comment
Please, Sign In to add comment