loading...

CS301 Data structures Assignment 3 Solution Fall 2012

Consider the scenario that the customers of a bank are depositing their utility bills in the banks standing in a row outside.

The bank serves its customer on first come first serve strategy, in order to automate this scenario the Queue data structure is well suitable to use, therefore,

Write a C++ program which store customer names and their ages in the Queue, while the Queue must implement as a linked listed internally, it should be clear that the first node (Customer) in the Linked list will be the front of the Queue while the last node (Customer) in the linked list will be the Back (rear) of the Queue, for example,

The program should implement the following operations of Queue data structure,

  1. Add_Customer() : add a new customer in the queue
  2. Remove_Customer() : Remove or serve a customer from the Queue
  3. Get_FrontCustomer() : Just show the Customer name that will serve(remove) next, or means customer at the front.
  4. Display_Q() : Show all Customer names and ages currently present in the Queue
  5. GetQsize() : Show the number of customer currently present in the Queue.

Lectures Covered:  This assignment covers Lecture # 01-10

Deadline: Your assignment must be uploaded/submitted at or before. 19/11/2012
Solution:

#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
using namespace std;
class Queue{
    private:
        string item[MAX_SIZE];
        int rear;
        int front;
    public:
        Queue();
        void enqueue(string);
        string dequeue();
        int size();
        void display();
        bool isEmpty();
        bool isFull();
};
Queue::Queue(){
    rear = -1;
    front = 0;
}
void Queue::enqueue(string data){
        item[++rear] = data;
}
string Queue::dequeue(){
        return item[front++];
}
void Queue::display(){
    if(!this->isEmpty()){
        for(int i=front; i<=rear; i++)
            coutitem[i]endl;
    }else{
        cout"Queue Underflow"endl;
    }
}
int Queue::size(){
    return (rear - front + 1);
}
bool Queue::isEmpty(){
    if(front>rear){
        return true;
    }else{
        return false;
    }
}
bool Queue::isFull(){
    if(this->size()>=MAX_SIZE){
        return true;
    }else{
        return false;
    }
}
int main(){
    Queue queue;
    int choice;
    string data;
    while(1){
        cout"n1.for Add Customer in Queuen2.for Remove Customer from Queuen3.for Queue Sizen4.for Display all elementn5.for Quit";
        cout"nEnter your choice: ";
        cin>>choice;
        switch(choice){
            case 1:
            if(!queue.isFull()){
                    cout"n Enter name: ";
                    cin>>data;
                    queue.enqueue(data);
            }else{
                cout"Queue is Full"endl;
            }
            break;
            case 2:
            if(!queue.isEmpty()){
                cout"The data dequeued is :"queue.dequeue();
            }else{
                cout"Queue is Emtpy"endl;
            }
                break;
            case 3:
                cout"Size of Queue is "queue.size();
                break;
            case 4:
                queue.display();
                break;
            case 5:
                exit(0);
                break;
        }
    }
}

 

DOWNLOAD SOLUTION HERE
loading...