Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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 ?
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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.
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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

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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Something like this perhaps:-

Code:
typedef struct
{
   process* head_ ;
   process* tail_ ;    // as pilotError suggested
} queue ;

queue* queue_lines[ 30 ] ;

wouldn't it be

queue que_lines[30];

rather than

queue* queue_lines[ 30 ] ;

as each struct contains the pointers ?
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
no added benefit of using linked lists

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.
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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 ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.