irapilguy

Untitled

Nov 10th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 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 res = 0;
  25. void pop() {
  26.     if (first == NULL) return ;
  27.      res = first->data;
  28.     first = first->prev;
  29. }
  30. int front() {
  31.     if (first != NULL) return first->data;
  32.     return 0;
  33. }
  34. int countt = 1;
  35. int size(Queue *t) {
  36.     if (t == NULL) return 0;
  37.     if (t ->prev != NULL) {
  38.         countt++;
  39.         size(t->prev);
  40.     }
  41.     return countt;
  42. }
  43. void clear(Queue *t) {
  44.     if (t != NULL) {
  45.         clear(t->prev);
  46.         pop();
  47.     }
  48. }
  49. int compare(char *str1, char *str2) {
  50.     int i = 0;
  51.     while (str1[i] != '\0' || str2[i] != '\0') {
  52.         if (str1[i] == str2[i]) {
  53.             i++;
  54.         }
  55.         else {
  56.             return 0;
  57.         }
  58.     }
  59.     return 1;
  60.  
  61. }
  62. void show(Queue *t) {
  63.     if (t != NULL) {
  64.         cout << t->data;
  65.         show(t->prev);
  66.     }
  67. }
  68. int main() {
  69.     char command[10] = { 0 };
  70.     int value;
  71.     while (compare(command, "exit") != 1) {
  72.         cin >> command;
  73.         if (compare(command, "push") == 1) {
  74.             cin >> value;
  75.             push(value);
  76.             cout << "ok" << "\n";
  77.         }
  78.         if (compare(command, "pop") == 1) {
  79.             if (first == NULL) cout << "error"<<"\n";
  80.             else {
  81.                 pop();
  82.                 cout << res << "\n";
  83.             }
  84.         }
  85.         if (compare(command, "front") == 1) {
  86.             if (first == NULL) cout << "error" << "\n";
  87.             else cout << front() << "\n";
  88.         }
  89.         if (compare(command, "size") == 1) {
  90.              cout << size(first) << "\n";
  91.             countt = 1;
  92.         }
  93.         if (compare(command, "exit") == 1) {
  94.             cout << "bye";
  95.         }
  96.         if (compare(command, "clear") == 1) {
  97.             clear(first);
  98.             cout << "ok" << "\n";
  99.         }
  100.     }
  101.     return 0;
  102. }
Add Comment
Please, Sign In to add comment