Thread: Why is my program no dequeueing?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Why is my program no dequeueing?

    Why is my code not dequeueing here:

    Code:
    if(arrive != i)
    {
    Queue.Enqueue(customer);
    //i = arrive;
    }
    
    else if(arrive == i)
    {
        Queue.Dequeue();
        service = rand()%100;
        Outfile<<service<<endl;
    }
    
    
    //******************************************************************//
    //Program Queue client file                                         //
    //                               //
    //  //
    //   //
    //                                                               //
    //                                                                  //
    //                                                                  //
    //******************************************************************//
    
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<cstdlib>
    #include"Queue.h"
    #include"QueueImpl.cpp"
    
    const int loopcounter = 200;
    using namespace std;
    
    
    //int CalculateTime(int service);
    
    
    int main()
    {
        IntQueue Queue;
             
        ifstream Infile;
        ofstream Outfile;
        Infile.open("inputQ.txt");
        Outfile.open("OUTPUTQ.TXT");
        int y = rand();
        int x = rand();
        int minutes = 720;
        int arrive, service, currentperson_time, nextperson, NextCustomer;
        int arrivaltimenewcust, servicetimeNew, oldValue;
        int MaxCust= 3;
        int i = 0;
        int customer = 0;
    int CustomerNumber=0;
    int runningtime;
    int time=0;
    
    
    y = rand();
    x = rand();
    Infile>>customer;
    
    while(i < loopcounter )
    {
        i++;
    //Infile>>customer;
    
    CustomerNumber = customer;
    srand(customer);
    arrive = rand()%100;  //First cutsomer arrival time
    service = rand()%100;     //First customer service time
    
    if(arrive != i)
    {
    Queue.Enqueue(customer);
    //i = arrive;
    }
    
    else if(arrive == i)
    {
        Queue.Dequeue();
        service = rand()%100;
        Outfile<<service<<endl;
    }
    Outfile<<"Customer "<<customer<<endl;
    Outfile<<"Customer "<<customer<<" arrival time "<<arrive<<endl;
    Outfile<<"Customer "<<customer<<" service time "<<service<<endl;
    customer++;
    }
    Infile>>customer;
    
    return 0;
    }
    //Implementation
    #include<stddef.h>
    
    typedef NodeType* NodePtr;
    
    struct NodeType
    {
        int data;
        NodePtr link;
    };
    ////////////////////////////////////////////////////////////////////
    void CopyList(NodePtr, NodePtr&, NodePtr&);
    IntQueue::IntQueue()
    {
        front = rear = NULL;
    }
    
    ////////////////////////////////////////////////////////////////////
    IntQueue::~IntQueue()
    {
        NodePtr tempPtr;
    
        while(front != NULL)
        {
             tempPtr = front;
             front = tempPtr->link;
             delete tempPtr;
        }
    }
    /////////////////////////////////////////////////////////////////////
    IntQueue::IntQueue(const IntQueue& otherQ)
    {
        if(otherQ.front == NULL)
             front = rear = NULL;
        else
             CopyList(otherQ.front, front, rear);
    }
    //////////////////////////////////////////////////////////////////////
    void IntQueue::Enqueue(int newItem)
    {
        NodePtr newPtr = new NodeType;
        newPtr->data = newItem;
        newPtr->link = NULL;
        if(front == NULL)
             front = rear = newPtr;
        else{
             rear->link = newPtr;
             rear = newPtr;
        }
    }
    //////////////////////////////////////////////////////////////////////
    int IntQueue::Front() const
    {
        return front->data;
    }
    //////////////////////////////////////////////////////////////////////
    void IntQueue::Dequeue()
    {
        NodePtr tempPtr = front;
        front = tempPtr->link;
        delete tempPtr;
        if(front == NULL)
             rear = NULL;
    }
    //////////////////////////////////////////////////////////////////////
    void CopyList(NodePtr currPtr, NodePtr& cloneFront, NodePtr& cloneRear)
    {
        NodePtr newPtr = NULL; {
        CopyList(currPtr->link, cloneFront, cloneRear);
        newPtr = new NodeType;
        newPtr->data = currPtr->data;
        newPtr->link = cloneFront;
        if(currPtr->link == NULL)
             cloneRear = newPtr;
    }
    cloneFront = newPtr;
    }
    //header
    struct NodeType;
    
    class IntQueue 
    {
    
    public:
        IntQueue();
             IntQueue(const IntQueue& otherQ);
        void Enqueue(int newItem);
        int Front() const;
        void Dequeue();
        ~IntQueue();
    private:
        NodeType* front;
        NodeType* rear;
    };

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Note for administrator: what are doing smilies inside code tags?

    You should check whether the queue is empty before dequeueing, or do checking in dequeue() function.
    Last edited by aerian; 09-19-2003 at 06:43 AM.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Hmm.. I was too hungry to think about the code last time.

    Use indentation, so others (and also you) can easily determine where the while loop ends.

    The if part of else..if (else if(arrive == i)) is redundant, it's always true.

    You can try to debug it with lesser amount of unused variables (x, y) and random values. In my opinion, first you should debug the queue, when it is working fine, you can try it in other application and debugging of that application will be easier.

    So enqueue some numbers, look at their values (function like printQueue() will help a lot), then dequeue them and watch...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM