Link List implementation of Queues Urgent!

Suzanne's Avatar, Join Date: Feb 2010
Newbie Member
I have to create a time-simulated queue. I have done a rough code but the program hangs aft a certain point, can't figure out why, plus m not sure how to show the job allocation for people using the service. The program hangs because of a do while loop in the first part of the program, I tried with while also, no use.

Program 1:
Code:
#include <iostream> 
#include <iomanip> 
using namespace std; 
#include "Queue.h" 
 
struct Machine{ 
 bool active; 
 int transaction; 
}; 
 
int main(){ 
    const int MIN_SIM_TIME = 0,MAX_SIM_TIME = 10000,MIN_SERV_TIME = 3,MAX_SERV_TIME = 16, 
    MIN_NUM_SERV = 0,MAX_NUM_SERV = 20,MIN_ARRIV_TIME = 0, MAX_ARRIV_TIME = 100; 
     
       char prog_run = 'Y';  
    int SIM_TIME,SERV_TIME,NUMS_SERV,ARRIV_TIME; 
    int index,mach_Time,cust,wait_Time;  
     
     index = 0;mach_Time = 0; 
     cust = 0;wait_Time = 0; 
      
     Queue ATM_Q; 
     do{ 
           cout << "\n------------------------------------------"; 
           cout << "\n- Welcome to the ATM Simulation Program -" ; 
           cout << "\n------------------------------------------"; 
      
           cout << "\n\nPlease input the Queue Simulation Time: "; 
           cin >> SIM_TIME; 
      
     while (SIM_TIME <= MIN_SIM_TIME || SIM_TIME > MAX_SIM_TIME)  
     { 
           cout << "\nInvalid input. Please re-enter: "; 
           cin >> SIM_TIME; 
     } 
     cout << "\nAverage Service Time: "; 
     cin >> SERV_TIME; 
      
     while (SERV_TIME <= MIN_SERV_TIME || SERV_TIME > MAX_SERV_TIME)  
     { 
           cout << "\nInvalid input. Please re-enter: "; 
           cin >> SERV_TIME; 
     } 
     cout << "\nAverage Number of Servers: "; 
     cin >> NUMS_SERV; 
 
     while (NUMS_SERV <= MIN_NUM_SERV || NUMS_SERV > MAX_NUM_SERV)  
     { 
           cout << "\nInvalid input. Please re-enter: "; 
           cin >> NUMS_SERV; 
     } 
     cout << "\nAverage Time Between Arrivals: "; 
     cin >> ARRIV_TIME; 
 
     while (ARRIV_TIME <= MIN_ARRIV_TIME || ARRIV_TIME > MAX_ARRIV_TIME)  
     { 
           cout << "\nInvalid input. Please re-enter: "; 
           cin >> ARRIV_TIME; 
     } 
      
     Machine * mach = new Machine[NUMS_SERV]; 
      
     for (index = 0; index < NUMS_SERV; index++)  
     {             
         mach[index].active = false;             
         mach[index].transaction = SERV_TIME;         
     } 
      
     while (mach_Time < SIM_TIME)  
     { //Run until simulation time is reached             
       if (mach_Time % ARRIV_TIME == 0)  
       { //Check if a customer should be enqueued                 
       ATM_Q.enqueue();                 
       cust++; 
       }             
      
     } 
      
     for (index = 0; index < NUMS_SERV; index++)  
     {                 
          if (mach[index].active == false && ATM_Q.getSize() != 0)  
          { //Dequeue if a teller is open                     
          ATM_Q.dequeue();                     
          mach[index].active = true;                     
          mach[index].transaction = SERV_TIME;                                         
          }             
     }  
      
     for (index = 0; index < NUMS_SERV; index++)  
     {                 
          if (mach[index].active == true)  
          {                     
                mach[index].transaction--;  //Decrement time spent at teller                 
          }                 
          if (mach[index].transaction == 0)  
          { 
            mach[index].active = false; //Set teller to open if time limit is reached             
          }             
    } 
    wait_Time += ATM_Q.getSize(); //Set wait time to persons left in queue             
    mach_Time++;         
        //Output user input data         
    cout << "\n---------------";              
    cout<< "\n- Data Output -";              
    cout<< "\n---------------\n";          
    cout << setw(31) << left << "Simulation Time: ";         
    cout << SIM_TIME << endl;          
    cout << setw(31) << left << "Average Service Time: ";         
    cout << SERV_TIME << endl;          
    cout << setw(31) << left << "Average Number of Servers: ";         
    cout << NUMS_SERV << endl;          
    cout << setw(31) << left << "Average Time Between Arrivals: ";         
    cout << ARRIV_TIME << endl;         //Output calculated data         
    /*cout << "Average Total Wait Time: ";         
    cout << fixed << setprecision(2);              
    cout<< (float)wait_Time/cust;         
    cout << "\nCustomers in line at end of simulation: ";*/              
    cout<< ATM_Q.getSize() << endl;         //Ask to run again         
    cout << "\nRun the program again? (y/n): ";         
    cin >> prog_run;         
    prog_run = (char)toupper(prog_run);                     
    //Deallocate teller structure array         
       delete []mach;     
  }while(prog_run != 'Y' && prog_run != 'N'); 
   
  system("pause"); 
  return 0; 
}
2nd related program:
Code:
#include <iostream> 
using namespace std; 
#include "Queue.h" 
 
Queue::Queue() { 
    mySize = 0; 
    first = NULL; 
    last = NULL;     
} 
 
void Queue::enqueue() { 
    NodePointer nPtr = new Node(1); 
    NodePointer predPtr = first; 
     
    if (first == NULL) { //Insert if queue is empty 
        nPtr->next = first; 
        first = nPtr; 
    } else { //Insert into the queue at the end 
        while (predPtr->next) { 
            predPtr = predPtr->next; 
        } 
        nPtr->next = predPtr->next; 
    } 
    mySize++; 
    last = nPtr; //Set last to new pointer 
} 
 
void Queue::dequeue() { 
    if (first) { 
        NodePointer dPtr = first; 
        first = first->next; //Set first to the second node in the <strong class="highlight">list</strong> 
        delete dPtr; //Delete the node that has been dequeued 
    } 
    mySize--; 
} 
 
ElementType Queue::front() { 
    if (first) { 
        NodePointer ptr = first; 
        return ptr->data; 
    } 
} 
 
int Queue::getSize() { 
    return mySize; 
} 
 
Queue::~Queue() { 
    if (first) { 
        //Deallocate all nodes in queue 
        NodePointer current = first; 
        NodePointer temp = first; 
         
        temp = current->next; 
        delete current; 
        current = temp; 
    } 
}
3rd related program:
Code:
#ifndef QUEUE 
#define QUEUE 
 
#include <iostream> 
using namespace std;  
 
typedef int ElementType; 
 
class Queue { 
     
    public: 
         
        //Default constructor 
        Queue(); 
         
        //Add to the back of the queue 
        void enqueue(); 
         
        //Remove from the front of the queue 
        void dequeue(); 
     
        //Returns the front of the queue 
        ElementType front(); 
         
        //Return size of the queue 
        int getSize(); 
         
        //Destructor 
        ~Queue(); 
         
    private: 
         
        class Node { 
         
            public: 
             
                ElementType data; 
                Node *next;                  
 
                Node(ElementType i) { // Node constructor 
                    data = i; 
                    next = NULL; 
                } 
 
        }; //--- end of Node class 
         
        typedef Node *NodePointer; 
         
        Node *first; 
        Node *last; 
        int mySize; 
         
}; //--- end of Queue class 
 
#endif

Last edited by shabbir; 18Jul2010 at 18:29.. Reason: Code blocks
0
Poonamol's Avatar, Join Date: Mar 2010
Go4Expert Member
Please go through the below link.

http://lispmachine.wordpress.com/2009/05/13/queue-in-c/