PDA

View Full Version : C help




MacDonaldsd
Mar 1, 2007, 02:53 PM
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
Mar 1, 2007, 04:19 PM
Hi

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


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

process* queueLines ;


b e n

pilotError
Mar 1, 2007, 09:00 PM
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
Mar 2, 2007, 08:16 AM
Hi

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


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

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
Mar 2, 2007, 09:39 AM
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:-

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
Mar 2, 2007, 09:46 AM
Something like this perhaps:-

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
Mar 2, 2007, 10:41 AM
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:-


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
Mar 2, 2007, 12:07 PM
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
Mar 2, 2007, 02:06 PM
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
Mar 2, 2007, 04:12 PM
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 ?

lazydog
Mar 2, 2007, 04:39 PM
try

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

b e n

MacDonaldsd
Mar 2, 2007, 04:50 PM
try

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

b e n

Yep that works:D