nie-ii-year

lab stuff from undergrad second year.
git clone http://git.hanabi.in/repos/nie-ii-year.git
Log | Files | Refs | LICENSE

tq.cpp (1551B)


      1 #include <iostream>
      2 #define MAX 5
      3 
      4 int read = -1;
      5 int write = -1;
      6 
      7 template <class T>
      8 class queue{
      9   T data[MAX];
     10 public:
     11   void inQueue(){
     12     T incoming;
     13     if(write == MAX - 1){
     14       std::cout << "Queue overflow\n";
     15       return;
     16     }
     17     if(read == -1)
     18       ++read;
     19     std::cout << "Enter element to be entered: ";
     20     std::cin >> incoming;
     21     write = (write + 1);
     22     data[write] = incoming;
     23   };
     24   void deQueue(){
     25     if(read == -1 || read > write)
     26       std::cout << "Queue underflow.\n";
     27     else{
     28       std::cout << data[read] << " has been removed.\n";
     29       read++;
     30     }
     31   };
     32   void displayQueue(){
     33     if(write == -1 || read > write)
     34       std::cout <<"Queue is empty.\n";
     35     else{
     36       for(int i = read; i <= write; i++)
     37         std::cout << data[i] << " ";
     38       std::cout << "\n";
     39     }
     40   };
     41 };
     42 
     43 int main() {
     44     int choice;
     45     queue <int> q;
     46     do{
     47       std::cout << "Enter choice: ";
     48       std::cin >> choice;
     49       switch (choice) {
     50         case 1: q.inQueue();
     51                 q.displayQueue();
     52                 break;
     53         case 2: q.deQueue();
     54                 q.displayQueue();
     55                 break;
     56         default: choice = 0;
     57       }
     58     }while(choice);
     59     queue <float> Q;
     60     do{
     61       std::cout << "Enter choice: ";
     62       std::cin >> choice;
     63       switch (choice) {
     64         case 1: Q.inQueue();
     65                 Q.displayQueue();
     66                 break;
     67         case 2: Q.deQueue();
     68                 Q.displayQueue();
     69                 break;
     70         default: choice = 0;
     71       }
     72     }while(choice);
     73   return 0;
     74 }