Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <fstream>
- #include <vector>
- class Queue {
- public:
- Queue(int max_size) : size_(0), max_size_(max_size), head_(0), tail_(0) { queue_.resize(max_size); }
- void push(int x) {
- if (size_ == max_size_) {
- std::cout << "error" << std::endl;
- }
- else {
- queue_[tail_] = x;
- tail_ = (tail_ + 1) % max_size_;
- size_++;
- }
- }
- void pop() {
- if (size_ == 0) {
- std::cout << "None" << std::endl;
- }
- else {
- std::cout << queue_[head_] << std::endl;
- head_ = (head_ + 1) % max_size_;
- size_--;
- }
- }
- void peek() {
- if (size_ == 0) {
- std::cout << "None" << std::endl;
- }
- else {
- std::cout << queue_[head_] << std::endl;
- }
- }
- void size() {
- std::cout << size_ << std::endl;
- }
- private:
- std::vector<int> queue_;
- int size_;
- int max_size_;
- int head_;
- int tail_;
- };
- int main() {
- int count, size, num;
- std::ifstream fin("input.txt");
- fin >> count >> size;
- Queue queue(size);
- std::string command;
- for (int i = 0; i < count; i++) {
- fin >> command;
- if (command == "push") {
- fin >> num;
- queue.push(num);
- }
- if (command == "pop") {
- queue.pop();
- }
- if (command == "peek") {
- queue.peek();
- }
- if (command == "size") {
- queue.size();
- }
- }
- }
Advertisement
Comments
-
- Hello look this its better :
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- class Queue {
- public:
- explicit Queue(int max_size) : size_(0), max_size_(max_size), head_(0), tail_(0), queue_(max_size) {}
- bool push(int x) {
- if (size_ >= max_size_) {
- std::cout << "Queue is full" << std::endl;
- return false;
- }
- queue_[tail_] = x;
- tail_ = (tail_ + 1) % max_size_;
- ++size_;
- return true;
- }
- bool pop() {
- if (empty()) {
- std::cout << "Queue is empty" << std::endl;
- return false;
- }
- std::cout << queue_[head_] << std::endl;
- head_ = (head_ + 1) % max_size_;
- --size_;
- return true;
- }
- void peek() const {
- if (empty()) {
- std::cout << "Queue is empty" << std::endl;
- } else {
- std::cout << queue_[head_] << std::endl;
- }
- }
- void display_size() const {
- std::cout << size_ << std::endl;
- }
- bool empty() const {
- return size_ == 0;
- }
- private:
- std::vector<int> queue_;
- int size_;
- int max_size_;
- int head_;
- int tail_;
- };
- int main() {
- std::ifstream fin("input.txt");
- if (!fin) {
- std::cerr << "Error opening file" << std::endl;
- return 1;
- }
- int count, size;
- fin >> count >> size;
- Queue queue(size);
- std::string command;
- int num;
- while (count-- > 0 && fin >> command) {
- if (command == "push") {
- fin >> num;
- queue.push(num);
- } else if (command == "pop") {
- queue.pop();
- } else if (command == "peek") {
- queue.peek();
- } else if (command == "size") {
- queue.display_size();
- }
- }
- return 0;
- }
- • BETTER POINT :
- • Renamed the size() method to display_size() to avoid confusion with the size_ class member.
- • Added checks to ensure the “input.txt” file opens correctly.
- • Simplified the command reading loop using a while loop.
- • Introduced the empty() method to check if the queue is empty before performing pop or peek.
- • Provided more descriptive error messages for when the queue is full or empty.
- • Made the peek() method const, as it does not modify the state of the object.
- • Added boolean returns for the push and pop methods to indicate the success or failure of the operation.
Add Comment
Please, Sign In to add comment