C/how to calculate the size fo packets destined for a particlur node in a queue of packets
Expert: Narendra - 4/20/2004
QuestionI have a queue of packets, in which each packet contains node_id and size.
For packets destined for each node_id I want to calculate the size of the packets for that particular node. How do I calculate this ?
struct packet{
int node_id;
int size;
};
struct queue{
struct packet *front,* rear;
};
For example
In the above queue of packets,I want to calculate the total size of packets destined for node_id=2
How can i do this ?
AnswerSize of each packet is fixed!
I think the question is not yet clear to me....
You have got a queue. Each node of the queue is of type struct packet.
And the value of node_id in each of these nodes can be same or different.
You want to browse thru the queue and get the size of the nodes for which node_id is 2 and then get a total of them.
Is this right!?
You will need to make a small change to your struct queue:
struct queue
{
struct packet data;
struct queue *next;
};
And 2 global variables are needed:
struct queue *front = NULL, *rear = NULL;
This has to be updated as you add/delete elements from the queue.
Now here is a small function to do this:
int getTotalSize(int node_id)
{
int total_size = 0;
struct queue *queue = front;
while (queue != rear)
{
if (queue->data.node_id == node_id)
total_size += queue->data.size;
queue = queue->next;
}
return total_size;
}
Hope this helps....
-ssnkumar