Thread: peoblem with queues

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    peoblem with queues

    i have a structure with


    Code:
    typedef struct queue_s 
    {
       struct queue_s *q_next;
       struct queue_s *q_prev;
    } queue_t;
    
    
    queue_t devices={&devices,&devices};
    now i am confused , what value will be assigned to the two members of the object devices.

    please help ....

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Aren't you missing a data element in your structure?

    q_next will be the link to the following element in the queue, q_prev to the preceding element:
    Code:
              ----------       ----------       ----------
              | data   |       | data   |       | data   |
    NULL<-----| q_prev |<------| q_prev |<------| q_prev |
              | q_next |------>| q_next |------>| q_next |------>NULL
              ----------       ----------       ----------
    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Quote Originally Posted by AndiPersti View Post
    Aren't you missing a data element in your structure?

    q_next will be the link to the following element in the queue, q_prev to the preceding element:
    Code:
              ----------       ----------       ----------
              | data   |       | data   |       | data   |
    NULL<-----| q_prev |<------| q_prev |<------| q_prev |
              | q_next |------>| q_next |------>| q_next |------>NULL
              ----------       ----------       ----------
    Bye, Andreas


    no i don't need a data element in the structure, i know that prev will point to previous element and next to the next element...just wanted to know what will be values of q_next and q_prev in the condition mentioned in the problem...]

    thanks in advance!!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    They will point to the structure just constructed i would say

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    i mean...what values will they contain...NULL values??

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by rinky05 View Post
    no i don't need a data element in the structure, i know that prev will point to previous element and next to the next element...just wanted to know what will be values of q_next and q_prev in the condition mentioned in the problem...]

    thanks in advance!!
    'devices' is a struct of type queue_t, you assign it's own address to the two members *q_next and *q_prev. I guess this is the inital state of your queue, where it's self referential.

    Try adding this after the line that confuses you:

    Code:
    printf("%p %p %p\n", &devices, devices.q_next, devices.q_prev);
    This doesn't mean that you won't need a data element in the struct however, although it's possible to create without one it's pointless since you can not add anything to your queue.
    Last edited by Subsonics; 08-21-2012 at 07:01 AM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by rinky05 View Post
    i mean...what values will they contain...NULL values??
    No.Here is the story.The struct is created,memory is allocated,so the struct exsists.Then the pointers that you have as members of the struct are set to point to this same struct.The struct has been created,so it not NULL.I would suggest you to add that printf subsonics said

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rinky05 View Post
    now i am confused , what value will be assigned to the two members of the object devices.
    "devices" will be a struct containing two pointers that both point to the "devices" struct, i.e. itself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with Queues
    By Hikaru90 in forum C Programming
    Replies: 5
    Last Post: 03-14-2010, 08:55 AM
  2. slight code peoblem
    By Ciaran789 in forum C Programming
    Replies: 8
    Last Post: 11-21-2009, 11:17 AM
  3. queues
    By rebelfirst in forum C++ Programming
    Replies: 9
    Last Post: 12-02-2007, 05:33 AM
  4. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 02:08 AM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 05:19 PM