I have to read packets from a file, and reconstruct the packets into a linked list in order of the block_num.
struct packet {
unsigned short block_num;
unsigned short block_size;
unsigned short crc;
unsigned char *payload;
};
struct list {
struct packet p;
struct list *next;
};
This is my code for reading the packets and adding them into the linked list, but how would I sort them according to the block_num?
FILE *infp;
struct list *head = NULL;
struct list *last = NULL;
while (fread(&p, sizeof(p), 1, infp) != NULL) {
packet *newpacket = malloc(sizeof(p));
struct list *newlist = malloc(sizeof(list));
newlist -> p = newpacket;
newlist -> next = NULL;
if (head == NULL) {
head = last = newlist;
} else {
last -> next = newlist;
last = newlist;
}
}