Like Tree1Likes
  • 1 Post By KCfromNC

Question about Queues

This is a discussion on Question about Queues within the C Programming forums, part of the General Programming Boards category; Hello, I am have a programming assignment where I need to simulate a store's waiting line for check out. I ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Question about Queues

    Hello,
    I am have a programming assignment where I need to simulate a store's waiting line for check out.

    I am a little confused about the struct declaration, can someone please help me make sure i'm on the right track, b4 I dive into working on this program?


    Code:
    struct queue{
    int * element; //an array that will later be allocated using malloc representing each persons position in the line?
    int front; //points to the front of the queue?
    int numElements //the current size of the queue ?
    int queueSize  //this is the max size of the queue?
    
    }
    Last edited by matthayzon89; 10-19-2010 at 10:18 AM.
    Linklists use recursion to store what? ..... floats!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    This seems reasonable.

    The elements array is probably going hold a number representing each customer. The order they are stored will show the position. So if three entries in element are 5,1,3 it means that customer 5 is in front of the line, 1 is next, and 3 is in back.

    front is an index into the elements array showing the first valid entry of the queue. This will also start as 0, but will be moved once you start removing entries from the queue.

    The names of the last two are what you guessed, but it's hard to tell which is which. One is the total size of the elements array and the other is the number of entries currently in the queue. I use size below to refer to the total that you could possibly hold, compared to length which is how many elements are currently in the queue. You can use the names in your struct, just be consistent on which is which - if it makes sense to you that's what matters (comment the code well - it will help).

    The size will be set once, when you allocate element. This has to be done first before you use the queue for anything.

    The length will initially be 0. Each time you add an entry, you'll increase the length. This will let you find the last valid entry of the queue (element[front + length - 1]).

    Each time you take something out of the queue, you return the value element[front] then add 1 to front. Each time you add something, copy it into element[front+length] and add 1 to length.

    The when you get to the end of the queue you wrap around to the front. So the first entry is really element[(front + length - 1) % size] - the % size just wraps around from element[size - 1] back to element[0]. Same with the element you insert into.

    You'll have to make sure that length is less than size when you insert - it's a basic check that the queue is big enough. When removing you'll check to see that length > 0, otherwise the queue is empty.
    Pinkie22 likes this.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thanks for the response....!

    Can someone please help me understand this warning?'

    "[Warning] Struct queue declared inside parameter list"
    Linklists use recursion to store what? ..... floats!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Your actual struct, or it's forward declaration needs to be visible before the function prototype.

    Code:
    // This
    struct queue{
    int * element; //an array that will later be allocated using malloc representing each persons position in the line?
    int front; //points to the front of the queue?
    int numElements //the current size of the queue ?
    int queueSize  //this is the max size of the queue?
    
    };
    
    // Or this
    struct queue;
    
    
    // before this
    void foo ( struct queue *q );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21