//Author: Frank R. Mendez
//Title: Object Oriented Queue
//Description: This is program uses double linked list to store data in the queue its functions are enqueu,dequeue and display queue.
//Hope it can help you in you future projects. Good vibes to all programmers!
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

class Queue {
    struct Node* head,* tail;

public:

    Queue() {
        head = tail = NULL;
    }
    void enQueue();
    void deQueue();
    void displayQueue();
    void menu();
    int elem;
    int choice;
};

void Queue::enQueue() {
    cout << "Enter your element to be inserted the queue:" << endl;
    cin >> elem;
    Node* pointer = new Node;
    pointer -> data = elem;
    pointer -> next = NULL;
    if(head == NULL) {
        head = pointer;
    }
    else
        tail -> next = pointer;
        tail = pointer;
        cout << "Element has been inserted in the queue!" << endl;

}

void Queue::deQueue() {
    if(head == NULL){
        cout << "Queue is empty!" << endl;
    }
    Node* temp = head;
    head = head -> next;
    delete temp;
}

void Queue::displayQueue() {
    Node* pointer1 = head;
    if(head == NULL) {
        cout << "Queue is empty!" << endl;
    }
    else
    cout << "Elements of your QUEUE!" << endl;
    while (pointer1 != NULL) {
        cout << pointer1 -> data << endl;
        pointer1 = pointer1 -> next;
    }
    cout << "End" << endl;

}

void Queue::menu() {
    while(1)
   {
    cout<<"==============================================================="<<"\n";
    cout<<"              MENU - QUEUE(FIFO - First In First Out)                                 "<<"\n";
    cout<<"==============================================================="<<"\n";
    cout<<"     1. Queue"<<"\n";
    cout<<"     2. Dequeue"<<"\n";
    cout<<"     3. Display Queue"<<"\n";
    cout<<"     4. Exit"<<"\n";
    cout<<"==============================================================="<<"\n";
    cout << endl;
    cout<<"==============================================================="<<"\n";
    cout << endl;
    cout << endl;
      cout<<"\nEnter your choice: ";
      cin>>choice;
      switch(choice)
      {
       case 1:
      enQueue();
      break;
       case 2:
      deQueue();
      break;
       case 3:
      displayQueue();
      break;
       case 4:
      break;
       default:
      cout<<"Please enter correct choice(1-4)!!";

      break;
       }
   }
}

int main () {
    Queue frank;
    frank.menu();


}

Recommended Answers

All 2 Replies

Firstly, this is more of a code snippet than a discussion thread, no? Secondly the methodology you used is flawed. Instead of getting input inside the class functions you should move it outside. Instead of enqueue() you should use enqueue(int val) so that you can add values to the queue however you want. This is very important because lets say for example you have to get your values from a file. To use your code you would have to redirect cin to that file, and probably cout to a file too to keep messages from popping up. However if you use proper modularization this is not an issue. Always try to create classes that act as generally as possible so that they can be reused repeatedly. For example a queue should ONLY be a queue, not a queue and an input parser! As such I would suggest the following improvements:

A) Use standard names. For queues typically the functions are called enter and leave (enter adds an element to the queue, leave removes one and returns it).
B) Allow for input to the enter function so that all it does is store things.
C) If you are comfortable with them, this is the perfect chance to use a template parameter so that instead of being an integer queue, your queue class is just a queue.
D) Remove the using namespace std; line, it means that anybody using your code also has to use the std namespace, which could cause naming issues. Put that line at the top of any functions that use it so that only those functions can see it.
E) Remove the temporary variables from the public space of the class, they should really be initialized inside the functions that use them. If they need to be shared amongst class functions then they should be private. They definately should not be public.

If you need help implementing any of these changes I would be happy to provide it. :)

Frank, from what I (and others) have observed, you are obviously trying to help newbie C++ programmers with common patterns. Unfortunately, as folks such as decepticon and labdabeta have indicated, you may be causing more problems than you are curing.

I applaud your efforts, but I encourage you to get more experience before you share your "enlightenment" with the rest of the world... :-)

FWIW, I have been designing and developing major system software with C++ since 1992, and today write complex data collection and analytical software in C++ as senior systems engineer for a tier-one mobile phone company. I do know a bit about the subject of C++ design and development. I have also taught the subject at the senior and graduate level, though not since the early 2000's.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.