# 双向链表

typedef struct LinkNode{
	int data;
	struct LinkNode *next;
	struct LinkNode *prev;
} Node;

typedef struct DoubleLink{
	Node *pHead;
	Node *pTail;
} DLink;

void addLinkNode(DLink *link, int data, int type)
{

	Node *newNode = (Node *) malloc(sizeof(Node));
	if(link == NULL) {
		return;
	}
	newNode->data = data;
	newNode->next = NULL;
	newNode->prev = NULL;
	if(link->pHead == NULL || link->pTail == NULL)
	{
		link->pHead = newNode;
		link->pTail = newNode;
	}
	else if(type)
	{
		link->pHead->prev = newNode;
		newNode->next = link->pHead;
		link->pHead = newNode;
	}else
	{
		link->pTail->next = newNode;
		newNode->prev=link->pTail;
		link->pTail=newNode;
	}
	
	
}

void showLink(DLink *link, int type)
{
	Node *tmp;
	if(link == NULL) return;
	if(type)
	{		
		tmp = link->pHead;
		while(tmp != NULL)
		{
			printf("Node Data = %d\n",tmp->data);
			tmp = tmp->next;
		}
	}else
	{
		tmp = link->pTail;
		while(tmp != NULL)
		{
			printf("Node Data = %d\n",tmp->data);
			tmp = tmp->prev;
		}
	}
}

void delLinkNode(DLink *link, int data)
{
	Node *tmp = link->pHead;
	if(link == NULL || link->pHead == NULL || link->pTail == NULL)
	{
		return;
	}
	while(tmp != NULL)
	{
		if(tmp->data == data){break;}
		tmp=tmp->next;
	}
	if(tmp == link->pHead)
	{
		tmp->next->prev = NULL;
		link->pHead = tmp->next;
		free(tmp);
	}else if(tmp == link->pTail)
	{
		tmp->prev->next = NULL;
		link->pTail = tmp->prev;
		free(tmp);
	}else
	{
		tmp->next->prev = tmp->prev;
		tmp->prev->next = tmp->next;
		free(tmp);
	}
}

# 大小端判断

void IsBigEndian()//原理:联合体union的存放顺序是所有成员都从低地址开始存放,而且所有成员共享存储空间
{
 union temp
 {
  short int a;
  char b;
 }temp;
 temp.a = 0x1234;
 if( temp.b == 0x12 )//低字节存的是数据的高字节数据
 {
  printf("da");
 }
 else
 {
  printf("xiao");
 }
}

# ip地址分割

	struct IpData{
		char ip_addr[30];
		char ip_mask[30];
	}data[2];
	char array[]={"nameserver 192.168.100.251/24 nameserver 9.9.8.8/10"};
	char *p=array;
	char *head=array;
	int i=0,index=0,j=0,k=0,start=0,end=0,flag=1;
	while(*p)
	{
		if(flag&&(*p>='0'||p<='9'))
		{
			start = i;
			flag = 0;
		}
		if(*p=='/')
		{
			end = i;
			for(j=start,k=0;j<end;j++,k++)
			{
				data[index].ip_addr[k]=*(head+j);
			}
			data[index].ip_addr[k]='\0';
			data[index].ip_mask[0]=*(head+i+1);
			data[index].ip_mask[1]=*(head+i+2);
			data[index].ip_mask[2]='\0';
			p+=2;
			i+=2;
			index++;
			flag=1;
		}
		i++;
		p++;
	}
	for(i=0;i<index;i++)
	{
		printf("ip_addr is %s, and mask is %s\n",data[i].ip_addr,data[i].ip_mask);
	}

# 普通链表

Node *addLink(Node *head, int data){
	Node *newNode = (Node *) malloc(sizeof(Node));
	Node *tmp;
	tmp = head;
	newNode->data = data;
	newNode->next = NULL;
	if(head==NULL)
	{
		head = newNode;
		return head;
	}
	while(tmp->next!= NULL)
	{
		tmp=tmp->next;
	}
	tmp->next = newNode;
	return head;
}
void printLink(Node *head)
{
	Node *tmp = head;
	while(tmp!=NULL)
	{
		printf("Node data is %d\n",tmp->data);
		tmp = tmp ->next;
	}
}
Node * delLink(Node *head, int data)
{
	Node *p2,*p1;
	p1 = head;
	while(p1->data != data && p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if(p1->data == data)
	{
		if(p1==head)
		{
			head = p1->next;
		}
		else {
			p2->next=p1->next;
			free(p1);
		}
	}else {
		printf("can not find data in link!");
	}
	return head;
}