Closed Thread
Results 1 to 7 of 7

Thread: Help with FIFO QUEUE

  1. #1
    ghost305 is offline Newbie
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Help with FIFO QUEUE

    Description

    Create a Queue class that implements a queue abstraction. A queue is a FIFO list (First In First Out queue). A simple example is waiting in line, where the first person in the line is the first served. New arrivals are added to the back of the line, the next person served is (removed) from the front of the line.

    The Queue class needs to implement the following operations:

    * adding to the queue at one end (the tail)
    * removing from the queue at the other end (the head)
    * printing all items the queue (from head to tail)
    * erasing all items in the queue (leaving the queue empty).
    * destructor to empty the queue before it's destroyed (to release all memory)

    Additions and removals always occur at the opposite ends of the queue.

    You should create the following methods in your Queue class to implement the above operationsm

    Your Queue implementation uses a companion QueueItem class to represent each element in the list. A QueueItem contains character string as the data value, a unique (among all QueueItems in a Queue) integer node identifier, and a pointer to the next QueueItem in the list. The following is the definition for the QueueItem class.

    class QueueItem {
    public:
    QueueItem(char *pData, int id); // ctor
    void setNext(QueueItem *pItem);
    QueueItem* getNext();
    int getId();
    const char* getData();
    private:
    char mData[30]; // or, use a char* if you want to dynamically alloc memory
    int mNodeID;
    QueueItem * mpNext;
    };

    The QueueItem member functions are very basic, just setting or getting data members of the class. All the linked list manipulation is done by the Queue class member functions.

    The Queue class member functions manipulate the linked list of QueueItem's, creating and destroying QueueItem objects as needed using the C++ new and delete operators. The Queue class member data includes a pointer to the head and and pointer to the tail of the linked list of QueueItems, and an integer node counter used to provide a unique node ID for every newly created QueueItem (incremented each time a new QueueItem is added, and passed as a parameter to the QueueItem constructor. It is never decremented).

    The following is a partial example of the Queue class; you will need to fill in the remaining methods.

    class Queue {
    public:
    Queue(); // ctor inits a new Queue
    ~Queue(); // dtor erases any remaining QueueItems
    void addItem(char *pData);
    void removeItem();
    ...
    private:
    QueueItem *mpHead; // always points to first QueueItem in the list
    QueueItem *mpTail; // always points to the last QueueItem in the list
    int mNodeCounter; // always increasing for a unique id to assign to each new QueueItem
    };

    The Queue class member functions should not have access to the private members of QueueItem objects. They call the public member functions of QueueItem.

    As an example, the outline of the Queue::addItem() member function is shown below. It must add a new QueueItem at the tail of the Queue, and update the mpTail pointer to point to it. The first item in the Queue is both the head and the tail of the list.:


    void Queue::addItem(char *pData)
    {
    // dynamically create and init a new QueueItem object
    QueueItem *pQI = new QueueItem(pData, ++mNodeCounter);

    if (0 == mpHead) // chk for empty queue
    mpHead = mpTail = pQI;
    else
    {
    // link new item onto tail of list using mpTail pointer
    ...
    }
    }

    The removeItem() method removes the head QueueItem from the queue, and should release the memory using the C++ delete operator. It updates mpHead to point at the following item (if any) as the new head. If the list becomes empty, both mpHead and mpTail must be set to null (0). It does not change the value of mNodeCounter (which is always incremented when a new item is added). If called on an empty Queue, it does nothing.

    The erase() method removes all the items in the queue and should release the memory. To implement, you could loop calling removeItem() until the queue is empty.

    The Queue destructor should ensure that all items are removed from the queue. The easiest way is to call the erase() method from the destructor.
    The user code (main) never see's QueueItem objects, since they are used only for implementation inside of class Queue. main() has only the Queue object to work with. For example, the following code would create a queue with three elements, and then print it out:



    -------------------------------------------------------------------------------
    my Program:

    //QueueItem.h
    //Declaration of class QueueItem


    #ifndef QUEUEITEM_H
    #define QUEUEITEM_H



    class QueueItem
    {
    public:
    QueueItem(char *pData, int id); // constructor
    void setNext(QueueItem *pItem); //set pointer to next Item
    QueueItem* getNext(); //get pointer to next Item
    int getId(); //get Id
    const char* getData(); //get data member

    private:
    char mData[30]; // or, use a char* if you want to dynamically alloc memory
    int mNodeID;
    QueueItem *mpNext; // pointer to another object of same type
    };//end class QueueItem

    #endif
    ------
    //Queue.h
    //Declaration of class Queue

    //prevent multiple inclusions of header file
    #ifndef QUEUE_H
    #define QUEUE_H

    #include "QueueItem.h" // include definitions of class QueueItem from QueueItem.h

    class Queue
    {
    public:
    Queue(); // constructor
    ~Queue(); // destructor
    void addItem(char *pData);
    void removeItem();
    void eraseList();
    void printList();

    private:
    QueueItem *mpHead; // always points to first QueueItem in the list
    QueueItem *mpTail; // always points to the last QueueItem in the list
    int mNodeCounter; // always increasing for a unique id to assign to each new QueueItem
    };//end class Queue
    #endif
    -----------
    //member function definitions for class QueueItem

    #include <iostream> // allows program to output data
    using namespace std;

    #include <string>

    #include "QueueItem.h" // include definition of class QueueItem from QueueItem.h

    //QueueItem constructor
    QueueItem::QueueItem(char *pData, int id)
    {
    pData = mData;
    id = mNodeID;
    mpNext = NULL;

    }// end constructor

    void QueueItem::setNext(QueueItem *pItem)
    {
    mpNext = pItem;
    }// end setNext function


    QueueItem* QueueItem::getNext()
    {
    return mpNext;
    }//end getNext function

    int QueueItem::getId()
    {
    return mNodeID;
    }//end getId function

    const char* QueueItem::getData()
    {
    return mData;
    }//end getData function
    --------------
    //member function definitions for class Date
    #include <iostream> //allows program to output data on screen
    using std::cout;
    using std::endl;
    using namespace std;

    #include <iomanip>
    using std::setfill;
    using std::setw;

    #include <string>
    using std::string;
    using std::strcpy;

    #include "Queue.h" //include definition of class Queue from Queue.h



    //constructor
    Queue::Queue()
    {
    mpHead = NULL;
    mpTail = NULL;
    mNodeCounter = 0;
    }

    void Queue::addItem(char *pData)
    {
    //create and initialize new QueueItem dynamically
    QueueItem *pQI = new QueueItem(pData, ++mNodeCounter);

    if ( mpHead == 0 ){ //check for empty queue
    mpHead = mpTail = pQI;}

    else{
    // link new item onto tail of list using mpTail pointer

    mpTail->setNext(pQI);
    mpTail = pQI;

    }

    }//end addItem function

    //function removeItem
    void Queue::removeItem()
    {
    if(mpHead == NULL){
    mpHead = mpTail;}
    else{
    delete mpHead;
    mpHead = mpHead->getNext();}



    }//end removeItem function

    //Erase List
    void Queue::eraseList()
    {
    while (mpHead != 0)
    removeItem();
    mpHead = mpTail = 0;
    }//end eraseList function

    //prints contents of queue
    void Queue:rintList()
    {
    while (mpHead != 0){
    cout << mpHead->getData() << endl;
    mpHead = mpHead->getNext();
    cout << mNodeCounter << endl;}
    }//end printList function

    //destructor
    Queue::~Queue()
    {
    eraseList();
    }
    ---------------------------------------
    What is wrong with my program??? can somebody help me. It only prints 00000000 zeros
    |

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Help with FIFO QUEUE

    There is nothing wrong with your program. It is doing exactly as you programmed it to do, print only zeros. Do you have a specific question about a particular line of code?

  4. #3
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Help with FIFO QUEUE

    I don't see anyplace where you assign mData a value.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    ghost305 is offline Newbie
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Re: Help with FIFO QUEUE

    Quote Originally Posted by WingedPanther View Post
    I don't see anyplace where you assign mData a value.
    I changed it, i assigned mData like this in the QueueItem constructor:
    strncpy(mData, pData, 30);
    ---------------------------------------------------
    howw do i fix Printlist() and removeItem()??

  6. #5
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Help with FIFO QUEUE

    Can you give the current code, input, and output?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    ghost305 is offline Newbie
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Re: Help with FIFO QUEUE

    Code:
    //QueueItem.h
    //Declaration of class QueueItem
    
    
    #ifndef QUEUEITEM_H
    #define QUEUEITEM_H
    
    
    
    class QueueItem
    {
    public:
    QueueItem(char *pData, int id); // constructor
    void setNext(QueueItem *pItem); //set pointer to next Item
    QueueItem* getNext(); //get pointer to next Item
    int getId(); //get Id
    const char* getData(); //get data member
    
    private:
    char mData[30]; // or, use a char* if you want to dynamically alloc memory
    int mNodeID;
    QueueItem *mpNext; // pointer to another object of same type
    };//end class QueueItem
    
    #endif
    ------
    //Queue.h
    //Declaration of class Queue
    
    //prevent multiple inclusions of header file
    #ifndef QUEUE_H
    #define QUEUE_H
    
    #include "QueueItem.h" // include definitions of class QueueItem from QueueItem.h
    
    class Queue
    {
    public:
    Queue(); // constructor
    ~Queue(); // destructor
    void addItem(char *pData);
    void removeItem();
    void eraseList();
    void printList();
    
    private:
    QueueItem *mpHead; // always points to first QueueItem in the list
    QueueItem *mpTail; // always points to the last QueueItem in the list
    int mNodeCounter; // always increasing for a unique id to assign to each new QueueItem
    };//end class Queue
    #endif
    -----------
    //member function definitions for class QueueItem
    
    #include <iostream> // allows program to output data
    using namespace std;
    
    #include <string>
    
    #include "QueueItem.h" // include definition of class QueueItem from QueueItem.h
    
    //QueueItem constructor
    QueueItem::QueueItem(char *pData, int id)
    {
    pData = mData;
    id = mNodeID;
    mpNext = NULL;
    
    }// end constructor
    
    void QueueItem::setNext(QueueItem *pItem)
    {
    mpNext = pItem;
    }// end setNext function
    
    
    QueueItem* QueueItem::getNext()
    {
    return mpNext;
    }//end getNext function
    
    int QueueItem::getId()
    {
    return mNodeID;
    }//end getId function
    
    const char* QueueItem::getData()
    {
    return mData;
    }//end getData function
    --------------
    //member function definitions for class Date
    #include <iostream> //allows program to output data on screen
    using std::cout;
    using std::endl;
    using namespace std;
    
    #include <iomanip>
    using std::setfill;
    using std::setw;
    
    #include <string>
    using std::string;
    using std::strcpy;
    
    #include "Queue.h" //include definition of class Queue from Queue.h
    
    
    
    //constructor
    Queue::Queue()
    {
    mpHead = NULL;
    mpTail = NULL;
    mNodeCounter = 0;
    }
    
    void Queue::addItem(char *pData)
    {
    //create and initialize new QueueItem dynamically
    QueueItem *pQI = new QueueItem(pData, ++mNodeCounter);
    
    if ( mpHead == 0 ){ //check for empty queue
    mpHead = mpTail = pQI;}
    
    else{
    // link new item onto tail of list using mpTail pointer
    
    mpTail->setNext(pQI);
    mpTail = pQI;
    
    }
    
    }//end addItem function
    
    //function removeItem
    void Queue::removeItem()
    {
    	
    	if (mpHead == NULL)
    	return;
    	else
    	{
    	mpHead = temp;
    	mpHead = mpHead->getNext();
    	delete temp;}
    
    }//end removeItem function
    
    //Erase List
    void Queue::eraseList()
    {
    while (mpHead != 0)
    removeItem();
    mpHead = mpTail = 0;
    }//end eraseList function
    
    //prints contents of queue
    void Queue:printList()
    {
    
    	QueueItem* temp;
    	temp = mpHead;
    	cout << mpHead->getData() << endl;
    	cout << mNodeCounter <<endl;
    	temp = mpHead->getNext();
    }//end printList function
    
    //destructor
    Queue::~Queue()
    {
    eraseList();
    ------------------------------------------
    
    int main()
    {
    	Queue numbers;
    
    	numbers.addItem("one");
    	numbers.addItem("two");
    	numbers.addItem("three");
    	numbers.addItem("four");
    	numbers.printList();
    	numbers.removeItem();
    	numbers.removeItem();
    	numbers.addItem("five");
    	numbers.addItem("six");
    	numbers.addItem("seven");
    	numbers.addItem("eight");
    	numbers.printList();
    	numbers.removeItem();
    	numbers.removeItem();
    	numbers.removeItem();
    	numbers.removeItem();
    	numbers.printList();
    	numbers.eraseList();
    	numbers.addItem("nine");
    	numbers.addItem("ten");
    	numbers.addItem("eleven");
    	numbers.printList();
    	numbers.eraseList();
    	numbers.printList();
    
    	return 0;
    }
    I keep getting errors such as uninitialized local variable "temp". nothing else seems to work. OUTPUT prints "1 one" and after that there's an error.

  8. #7
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Help with FIFO QUEUE

    I'll go for the most obvious error:
    Code:
    void Queue:printList()
    {
    	QueueItem* temp;
    	temp = mpHead; //mpHead may be NULL!
    	cout << mpHead->getData() << endl; //If mpHead is NULL, this will fail!
    	cout << mNodeCounter <<endl;
    	temp = mpHead->getNext();//this doesn't do anything to print the next item
    }//end printList function
    Instead, try the following:
    Code:
    void Queue:printList()
    {
    	QueueItem* temp;
    	temp = mpHead;
            while (temp != NULL)
            {
            	cout << temp->getData() << endl;
    	        temp = temp->getNext();
            }
    	cout << mNodeCounter <<endl;
    }//end printList function
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Generic Queue
    By Nyfer in forum Java Tutorials
    Replies: 12
    Last Post: 10-16-2011, 03:45 AM
  2. FIFO memory buffer
    By eagle123 in forum C# Programming
    Replies: 1
    Last Post: 05-17-2010, 05:44 AM
  3. FIFO For Batch Processing?
    By TcM in forum Programming Theory
    Replies: 2
    Last Post: 07-30-2008, 10:22 AM
  4. queue
    By lmc059 in forum Java Help
    Replies: 4
    Last Post: 12-16-2007, 08:51 AM
  5. How to implement LIFO and FIFO method
    By anantabd in forum Visual Basic Programming
    Replies: 2
    Last Post: 10-11-2007, 04:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts