指针链表这货,让我焦作了一整天。终于弄好了。 有链表的创建,清空,从末尾添加。等功能。
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
typedef struct _DEV_LIST{
-
-
int fd;
-
unsigned char UUID[8];
-
struct _DEV_LIST* Next;
-
-
}DEV_LIST,*pDEV_LIST;
-
-
-
-
DEV_LIST *CreatNode(void){
-
-
DEV_LIST *pNode;
-
-
pNode = (DEV_LIST *)malloc(sizeof(DEV_LIST));
-
if(pNode == NULL)printf("内存分配失败\n");
-
pNode->fd = 0;
-
memset(pNode->UUID,0,sizeof(pNode->UUID));
-
pNode->Next = NULL;
-
-
return pNode;
-
}
-
-
-
-
-
void PrintfList(DEV_LIST* pHead){
-
int i;
-
if(NULL == pHead) //链表为空
-
{
-
printf("PrintList函数执行,链表为空\n");
-
}
-
else
-
{
-
while(NULL != pHead)
-
{
-
printf("fd=%d ",pHead->fd);
-
for(i=0;i<8;i++)
-
printf("UUID%d =%d \n",i,pHead->UUID[i]);
-
pHead = pHead->Next;
-
}
-
printf("\n");
-
}
-
}
-
-
int Add_Node(DEV_LIST** pHead)
-
{
-
DEV_LIST *pTemp;
-
if(*pHead == NULL) {
-
*pHead = CreatNode();
-
return 0;
-
}
-
pTemp = *pHead;
-
while(pTemp->Next != NULL){
-
pTemp = pTemp->Next;
-
}
-
pTemp->Next = CreatNode();
-
return 0;
-
}
-
-
int Add_Node_End(DEV_LIST** pHead, DEV_LIST** pNode)
-
{
-
DEV_LIST *pTemp;
-
if(*pHead == NULL) {
-
*pHead = CreatNode();
-
(*pHead)->fd = (*pNode)->fd;
-
memcpy((*pHead)->UUID ,(*pNode)->UUID,sizeof((*pHead)->UUID ));
-
-
printf("List null\n");
-
return 0;
-
}
-
pTemp = *pHead;
-
while(pTemp->Next != NULL){
-
pTemp = pTemp->Next;
-
}
-
pTemp->Next = *pNode;
-
-
return 0;
-
}
-
-
-
int ClearList(DEV_LIST** pHead){
-
DEV_LIST* pNext;
-
-
if (*pHead == NULL) {
-
return 0;
-
}
-
while((*pHead) != NULL){
-
pNext = (*pHead)->Next;
-
free(*pHead);
-
*pHead = pNext;
-
}
-
return 0;
-
}
-
-
-
int GetDevNumber(DEV_LIST* pHead)
-
{
-
int num=0;
-
while(pHead) {
-
num ++;
-
pHead = pHead->Next ;
-
}
-
return num;
-
}
-
-
-
-
int main()
-
{
-
DEV_LIST *pHead;
-
DEV_LIST *pNode;
-
-
pHead = NULL;
-
pNode = NULL;
-
printf("------------------------------\n");
-
pNode = CreatNode();
-
pNode->fd = 1;
-
memset(pNode->UUID,1,sizeof(pNode->UUID));
-
Add_Node_End(&pHead,&pNode);
-
-
pNode = CreatNode();
-
pNode->fd = 2;
-
memset(pNode->UUID,2,sizeof(pNode->UUID));
-
Add_Node_End(&pHead,&pNode);
-
-
PrintfList(pHead);
-
-
printf("GetDevNumber = %d\n",GetDevNumber(pHead));
-
-
printf("---------------clear---------------\n");
-
ClearList(&pHead);
-
PrintfList(pHead);
-
-
}
阅读(1158) | 评论(0) | 转发(0) |