I'm inserting in a list of struct some details that each fourplet represents a packet with source, destination, generation time and a rollnumber? Here is my display function and in the main() I just call her: display(); in the end of the main code. How can I display only the first 10 packets (fourplets) from the list, that I inserted before with the insert function below?
struct Packet{
int rollnumber;
int src;
int dest;
double gentime;
struct Packet *next;
}* head;
void display(){
struct Packet * temp = head;
while(temp!=NULL){
printf("Roll Number: %d\n",temp->rollnumber);
printf("src: %d\n", temp->src);
printf("dest: %d\n", temp->dest);
printf("gentime: %0.1f\n\n", temp->gentime);
temp = temp->next;
}
}
void insert(int rollnumber, int src, int dest, double gentime){
struct Packet * packet = (struct Packet *) malloc(sizeof(struct Packet));
packet->rollnumber = rollnumber;
packet->src=src;
packet->dest=dest;
packet->gentime = gentime;
packet->next = NULL;
if(head==NULL){
head = packet;
}
else{
packet->next = head;
head = packet;
}
}
Write the function display
for example the following way
void display( size_t n )
{
if ( n == 0 ) n = ( size_t )-1;
for ( struct Packet * temp = head; n-- && temp != NULL; temp = temp->next )
{
printf("Roll Number: %d\n",temp->rollnumber);
printf("src: %d\n", temp->src);
printf("dest: %d\n", temp->dest);
printf("gentime: %0.1f\n\n", temp->gentime);
}
}
And call the function for example like
display( 10 );
When the function is called with the argument equal to 0
then the function outputs the whole list.