Thread: noob help on queue class

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    noob help on queue class

    hey pple:

    i am new so this may sound silly.

    i have the following struct:
    Code:
    struct item{
    string msg;
    string sender;
    string receiver;
    };
    QUESTION:
    1.how do you implement a simple queue.
    2. how do i implement a linear/cicular queue class for the items. so that i can store the items (msg,sender,reciever) in a queue and read (dequeue) the items.

    NB i have searched the web and i keep getting <templates> which i dont need.

    thanks in advance

  2. #2
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    Me thinks there is one already implemented in the STL.

    Check out QUEUE class here

    Of course you could always implement your own queue if you know how to program a linked list.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    QUESTION:
    1.how do you implement a simple queue.
    You can use an array and store the index position of the first and last items in the array in some separate variables. When the array is full, you don't allow any more items to be stored in the queue, and instead you return a message stating the queue is full.

    2. how do i implement a linear/cicular queue class for the items. so that i can store the items (msg,sender,reciever) in a queue and read (dequeue) the items.
    A circular queue just means the start of the queque isn't necessarily at index postion 0 in the array. Also, when you store items in the array instead of returning a "queue is full" message when you get to the end of the array, you wrap back around to the begining of the array. You just have to keep track of the index position of the start of the array, the index position of the end of the array, and the size of the array, so you know when to wrap around.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    how to store a store the structure

    thanx pple,

    got the queue up and running. now to the next question:

    i used an array to store the items (used integers to keep it simple). i am now having problems storing the structure (Item) in the array. Q: how do i store a structure in a queue?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Keep track of where the first available index is. Maybe you have a variable that's keeping track of how many elements are in the table. You could use that and the variable determining which element is start of the "circular" queue to determine where the first available index is. Then in your push() or add() function you would do something like this:
    Code:
    void push(typeNameToPush t)
    {
      embeddedArrayName[firstAvailableIndex] = t;
    }
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question about a use a friend class
    By dorenthe in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 12:38 PM
  2. Need help with FIFO Queue using Singly Linked Lists
    By astou in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 02:36 PM
  3. ADT queue
    By ^xor in forum C Programming
    Replies: 4
    Last Post: 06-15-2005, 05:43 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM