帮忙解释下这个对IP地址排序的链表程序
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define SIZE 269
typedef unsigned char BYTE ;
typedef struct _IP {
BYTE seg[4];
struct _IP *next;
}IP;
int main()
{
FILE *fp1,*fp2;
int i, found,s,j;
char c[100];
IP *pip, *ptem, *pfind, *judge;
BYTE tem_ip[4];
pip = (IP *)malloc(sizeof(IP));
pip->next = pip;
if((fp1=fopen("ip1","r"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
if((fp2=fopen("route","w"))==NULL)
{
printf("cannot open outfile\n");
exit(0);
}
for(s=1;s<=SIZE;s++)
{
fscanf(fp1,"%s",&c);
if(feof(fp1)) {fclose(fp1);exit(0);}
sscanf(c,"%d.%d.%d.%d",&tem_ip[0], &tem_ip[1], &tem_ip[2], &tem_ip[3]);
if (!(tem_ip[0] || tem_ip[1] || tem_ip[2] || tem_ip[3]))
break;
ptem = (IP *)malloc(sizeof(IP));
for (i = 0; i < 4; i++)
ptem->seg[i] = tem_ip[i];
found = 0;
pfind = pip;
while (pfind->next != pip) {
for (i = 0; i < 4; i++) {
if (ptem->seg[i] < pfind->next->seg[i]) {
found = 1;
break;
} else if (ptem->seg[i] > pfind->next->seg[i]) {
break;
}
}
if (found)
break;
pfind = pfind->next;
}
ptem->next = pfind->next;
pfind->next = ptem;
}
printf("\nSorted ip address:\n");
ptem = pip->next;
while (ptem != pip) {
输出};
主要是排序的那部分,输出的我懂,请各位大侠帮帮忙!
[ 本帖最后由 yangrui 于 2010-4-16 16:38 编辑 ]