C help

Discussion in 'Mac Programming' started by MacDonaldsd, Mar 1, 2007.

  1. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    Basically this is part of a big piece of coursework but I cant get my head round one bit

    I have these declaration

    typedef struct{
    int name;
    int at;
    int nut;
    char status;
    struct process *next;
    }process;

    process pro[30];

    process* queueLines[30];

    When a process satisfies a condition it is moved onto queue zero defined as queLines[0],
    I want queLines[0] to point to the first process that is added and then that process to point to the next one, and this will continue.

    Are the declarations correct for a start ?
     
  2. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #2
    Hi

    The struct definition looks okay but why have you got the array

    Code:
    process* queueLines[30];
    
    Do you need to keep track of up to 30 lists? If you are only going to have one list then all you need is

    Code:
    process* queueLines ;

    b e n
     
  3. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #3
    To make life easier, you might want to define a head and tail pointer.

    process *qHead, *qTail ;

    You now just need to figure out how to maintain the queue.

    The condition here is first insert then how to move the pointers for every other insert. Since your always inserting at tail, your point changes are pretty straight forward.

    You don't need an array of 30 pointers (as MacDonaldsd pointed out) - actually the head and tail replace it all together (you could replace qHead with queueLines if you really like the name).

    The next node in the list is pointed to by the next pointer in the structure.
     
  4. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #4
    I do need multiple lists, basically when a condition is met it is moved into queue zero, then when another condition it is moved along to the next queue and so on.
     
  5. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    Ah okay. Can I suggest then that you create another stuct for the queues. It would make sense since in essence you are managing queues as well as processes.

    Something like this perhaps:-

    Code:
    typedef struct
    {
       process* head_ ;
       process* tail_ ;    // as pilotError suggested
    } queue ;
    
    queue* queue_lines[ 30 ] ;
    
    You could then have functions to manage the queues, ie to add/delete queues, and functions to manage processes, ie add/delete a process from a queue.


    b e n
     
  6. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #6
    wouldn't it be

    queue que_lines[30];

    rather than

    queue* queue_lines[ 30 ] ;

    as each struct contains the pointers ?
     
  7. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #7
    Yes you are absolutely right, my mistake!

    For what it's worth I think a better way than using an array of queue elements would be to make the queues a linked list just like process. Something like this:-

    Code:
    typedef struct
    {
       queue* next_ ; 
       process* head_ ;
       process* tail_ ;    // as pilotError suggested
    } queue ;
    
    
    queue* queue_head_ ;
    queue* queue_tail_ ;
    This will make adding/inserting and deleting queues easier.

    By the way a standard practise is to use 0L as the value of head_, tail_, head_ etc to indicate the end of a list.

    b e n
     
  8. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #8
    Thanks I will try and implement that tonight.

    The only reason I am not using a linked list for the the queue is it easier for me to get my head around :D

    30 is OTT as it is there is only going to be around 10 queues and I don't have to do anything like searching or deleting so there is no added benefit of using linked lists.
     
  9. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #9
    Well, Any real world implementation of a Stack or Queue is more or less a linked list.

    Its good real-world practice, much better to wrap your head around it now.
     
  10. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #10
    xcode gives me a warning for the following

    pro[counter2].next=&pro[counter1];

    where counter2 and counter1 are both ints.

    says "warning assignment from incompatible pointer types."

    Can't see the problem myself ?
     
  11. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #11
    try

    Code:
    typedef struct process
    {
       int name;
       int at;
       int nut;
       char status;
       struct process *next;
    } process;
    b e n
     
  12. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #12
    Yep that works:D
     

Share This Page