C语言单链表

C语言单链表,第1张

C语言单链表

1.arp_link.c

#include 
#include 
#include 
#include 
#include "arp_link.h"

ARP_link *arp_head=NULL;

void free_arp_link(ARP_link *head){
	ARP_link *pb = head;
	while(head){
		pb = head->next;
		free(head);
		head = pb;
	}
}


void printf_arp_link(ARP_link *head){
	ARP_link *pb = head;
	while(pb){
		printf("IP:%d.%d.%d.%d---", pb->ip[0],pb->ip[1],pb->ip[2],pb->ip[3]);
		printf("MAC:%02x:%02x:%02x:%02x:%02x:%02xn",
				pb->mac[0],pb->mac[1],pb->mac[2],pb->mac[3],pb->mac[4],pb->mac[5]);
		pb = pb->next;
	}
}


ARP_link *find_arp_from_ip(ARP_link *head, unsigned char *ip){
	ARP_link *pb = head;
	//printf("查找的ip包**********************:%dn",ntohs(*(unsigned short *)ip));//ntohs(*(unsigned short *)
	while(pb){
		if(memcmp(pb->ip,ip,4)==0){
			//printf("查到ip包**********************:%dn",ntohs(*(unsigned short *)(pb->ip)));
			break;
		}
		//printf("查不到n");
		pb = pb->next;
	}
	return pb;
}


ARP_link *inner_arp_link(ARP_link *head,ARP_link* p){
	ARP_link *pb = find_arp_from_ip(head, p->ip);//查找是否有该记录
	if(pb==NULL){//未查找到,插入链表,直接插入表头方便
		p->next = head;
		head = p;
		//printf("插入链表成功n");
	}else{//查找到,修改链表
		memcpy(pb->mac,p->mac,6);//拷贝mac地址
	}
	return head;
}

2.arp_link.h

#ifndef ARP_link_H
#define ARP_link_H

typedef struct arp_link{
	unsigned char ip[4];//ip地址四个字节
	unsigned char mac[6];//mac地址六个字节
	struct arp_link *next;
}ARP_link;

extern ARP_link *arp_head;

extern ARP_link *inner_arp_link(ARP_link *arp_head,ARP_link* p);


extern void printf_arp_link(ARP_link *arp_head);


extern ARP_link *find_arp_from_ip(ARP_link *arp_head, unsigned char *ip);

extern void free_arp_link(ARP_link *arp_head);
#endif

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3971723.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存