Thread: Please Help with Queues

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

    Please Help with Queues

    How do I make several queues? Do I make just one struct and then do this in main:

    A = NewQueue();
    B = NewQueue();
    or is it:
    struct queue Q1;
    struct queue Q2;

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You do not have to redefine the struct no, but you do have to declare individual instances.

    If "A" has not previously been declared, this is illegal in C:
    Code:
    A = NewQueue();
    B = NewQueue();
    What datatype are a A and B?

    Probably you want to do something like this:
    Code:
    struct queue Q1 = NewQueue();
    struct queue Q2 = NewQueue();
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    ok, but now it says invalid initializer. Are they supposed to be pointers and am I supposed to allocate space?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Hikaru90 View Post
    ok, but now it says invalid initializer. Are they supposed to be pointers and am I supposed to allocate space?
    That depends on NewQueue() -- if NewQueue() returns a pointer then...use a pointer!

    You need to allocate space in NewQueue. I was presuming you've already written it and got it working, since you are discussing multiple queues. If not, how about getting one real queue to work first?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    This creates a new and space for it right?
    struct queue *A = (struct queue*)malloc(sizeof(struct queue));

    By the way, these are my structs:
    Code:
    // Stores node of the linked list.
    struct node {
        int data;
        struct node* next;
    };
    
    // Stores queue.
    struct queue {
        struct node* front;
        struct node* rear;
    };

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Hikaru90 View Post
    This creates a new and space for it right?
    struct queue *A = (struct queue*)malloc(sizeof(struct queue));
    Yep. You do not have to cast malloc in C tho, only in C++. Remember to free() everything you malloc() at some point.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2009, 07:54 PM
  2. queues
    By rebelfirst in forum C++ Programming
    Replies: 9
    Last Post: 12-02-2007, 05:33 AM
  3. Concatenating two static queues?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 10-18-2004, 11:19 PM
  4. queues
    By jamesb in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 08:57 PM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 05:19 PM