You are here:

C/how to calculate the size fo packets destined for a particlur node in a queue of packets

Advertisement


Question
I 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 ?

Answer
Size 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.