Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225537
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: C/C++

2016-05-17 14:27:58

指针链表这货,让我焦作了一整天。终于弄好了。 有链表的创建,清空,从末尾添加。等功能。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct _DEV_LIST{

  5.     int fd;
  6.     unsigned char UUID[8];
  7.     struct _DEV_LIST* Next;

  8. }DEV_LIST,*pDEV_LIST;



  9. DEV_LIST *CreatNode(void){

  10.     DEV_LIST *pNode;
  11.     
  12.     pNode = (DEV_LIST *)malloc(sizeof(DEV_LIST));
  13.     if(pNode == NULL)printf("内存分配失败\n");
  14.     pNode->fd = 0;
  15.     memset(pNode->UUID,0,sizeof(pNode->UUID));
  16.     pNode->Next = NULL;

  17.     return pNode;
  18. }




  19. void PrintfList(DEV_LIST* pHead){
  20.     int i;
  21.     if(NULL == pHead) //链表为空
  22.     {
  23.         printf("PrintList函数执行,链表为空\n");
  24.     }
  25.     else
  26.     {
  27.         while(NULL != pHead)
  28.         {
  29.             printf("fd=%d ",pHead->fd);
  30.             for(i=0;i<8;i++)
  31.             printf("UUID%d =%d \n",i,pHead->UUID[i]);
  32.             pHead = pHead->Next;
  33.         }
  34.         printf("\n");
  35.     }
  36. }

  37. int Add_Node(DEV_LIST** pHead)
  38. {
  39.     DEV_LIST *pTemp;
  40.     if(*pHead == NULL) {
  41.         *pHead = CreatNode();
  42.         return 0;
  43.     }
  44.     pTemp = *pHead;
  45.     while(pTemp->Next != NULL){
  46.         pTemp = pTemp->Next;
  47.     }
  48.     pTemp->Next = CreatNode();
  49.     return 0;
  50. }

  51. int Add_Node_End(DEV_LIST** pHead, DEV_LIST** pNode)
  52. {
  53.     DEV_LIST *pTemp;
  54.     if(*pHead == NULL) {
  55.         *pHead = CreatNode();
  56.         (*pHead)->fd = (*pNode)->fd;
  57.         memcpy((*pHead)->UUID ,(*pNode)->UUID,sizeof((*pHead)->UUID ));

  58.         printf("List null\n");
  59.         return 0;
  60.     }
  61.     pTemp = *pHead;
  62.     while(pTemp->Next != NULL){
  63.         pTemp = pTemp->Next;
  64.     }
  65.     pTemp->Next = *pNode;

  66.     return 0;
  67. }


  68. int ClearList(DEV_LIST** pHead){
  69.     DEV_LIST* pNext;

  70.     if (*pHead == NULL) {
  71.         return 0;
  72.     }
  73.     while((*pHead) != NULL){
  74.         pNext = (*pHead)->Next;
  75.         free(*pHead);
  76.         *pHead = pNext;
  77.     }
  78.     return 0;
  79. }


  80. int GetDevNumber(DEV_LIST* pHead)
  81. {
  82.     int num=0;
  83.     while(pHead) {
  84.      num ++;
  85.      pHead = pHead->Next ;
  86.      }
  87.     return num;
  88. }



  89. int main()
  90. {
  91.     DEV_LIST *pHead;
  92.     DEV_LIST *pNode;

  93.     pHead = NULL;
  94.     pNode = NULL;
  95.     printf("------------------------------\n");
  96.     pNode = CreatNode();
  97.     pNode->fd = 1;
  98.     memset(pNode->UUID,1,sizeof(pNode->UUID));
  99.     Add_Node_End(&pHead,&pNode);

  100.     pNode = CreatNode();
  101.     pNode->fd = 2;
  102.     memset(pNode->UUID,2,sizeof(pNode->UUID));
  103.     Add_Node_End(&pHead,&pNode);

  104.     PrintfList(pHead);

  105.     printf("GetDevNumber = %d\n",GetDevNumber(pHead));

  106.     printf("---------------clear---------------\n");
  107.     ClearList(&pHead);
  108.     PrintfList(pHead);

  109. }

阅读(1119) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~