Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- struct Queue {
- int data;
- Queue *prev;
- Queue(int x) {
- data = x;
- prev = NULL;
- }
- };
- Queue *first = NULL;
- Queue *last = NULL;
- void push(int x) {
- Queue *t = new Queue(x);
- if (first == NULL) {
- first = t;
- last = t;
- return;
- }
- last->prev = t;
- last = t;
- }
- int pop() {
- if (first == NULL) return 0;
- int res = first->data;
- first = first->prev;
- return res;
- }
- int front() {
- if (first != NULL) return first->data;
- return 0;
- }
- int res = 0;
- int size(Queue *t) {
- if (t != NULL) {
- res++;
- size(t->prev);
- }
- if (t == NULL) return 0;
- return res;
- }
- void clear(Queue *t) {
- if (t != NULL) {
- clear(t->prev);
- pop();
- }
- }
- int compare(char *str1, char *str2) {
- int i = 0;
- while (str1[i] != '\0' || str2[i] != '\0') {
- if (str1[i] == str2[i]) {
- i++;
- }
- else {
- return 0;
- }
- }
- return 1;
- }
- int main() {
- char command[10] = { 0 };
- int i = 0;
- int value;
- while (compare(command, "exit") != 1) {
- cin >> command;
- if (compare(command, "push") == 1) {
- cin >> value;
- push(value);
- cout << "ok" << "\n";
- }
- if (compare(command, "pop") == 1) {
- if (pop() == 0) cout << "error"<<"\n";
- else cout << pop() << "\n";
- }
- if (compare(command, "front") == 1) {
- if (front() == 0) cout << "error" << "\n";
- else cout << front() << "\n";
- }
- if (compare(command, "size") == 1) {
- cout << size(first) << "\n";
- res = 1;
- }
- if (compare(command, "exit") == 1) {
- cout << "bye";
- }
- if (compare(command, "clear") == 1) {
- clear(first);
- cout << "ok" << "\n";
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment