Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128751
  • 博文数量: 44
  • 博客积分: 86
  • 博客等级: 民兵
  • 技术积分: 249
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-23 22:50
文章分类
文章存档

2012年(32)

2011年(12)

分类: C/C++

2011-10-10 09:36:31

  1. 不用那么多废话,双向链表的插入、删除、查询等操作,欢迎大家提出修改意见:
  2. doubleList.h

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. typedef struct Node
  7. {
  8.     int data;
  9.     struct Node * next;
  10.     struct Node * prior;
  11. }NODE, *PNODE;
  12.  
  13. PNODE create_list(void); //创建节点
  14. void traverse_list(PNODE pHead); //遍历链表(打印)
  15. int length_list(PNODE pHead); //求链表长度
  16. void sort_list(PNODE pHead); //排序
  17. void insert_list(PNODE pHead, int val); //插入节点
  18. void delete_list(PNODE pHead, int pos); //删除节点
  19. int find_list(PNODE pHead, int val); //查找元素


  20. PNODE create_list(void) //从文件中读取数据链表的初始化值
  21. {
  22.        int i, len;
  23.        int value;
  24.        char str[10];
  25.        FILE *pf;
  26.        PNODE pTail, pNew;
  27.        PNODE pHead = (PNODE)malloc(sizeof(NODE));
  28.        if (NULL == pHead)
  29.        {
  30.                 fprintf(stdout, "Memory molloc erroe !\n");
  31.                 exit(-1);
  32.        }
  33.        pTail = pHead;
  34.        pTail->next = NULL;
  35.        
  36.        pf = fopen("./data.txt", "r");
  37.        /*while(fgets(str, 10, pf) != NULL)
  38.        {
  39.            printf("str = %s \n", str);
  40.        }*/
  41.      len = 0;
  42.        while (1)
  43.        {
  44.              fgets(str, 10, pf);
  45.              if (feof(pf))
  46.              {
  47.                           fprintf(stdout, "file read finish !\n");
  48.                           break;
  49.              }
  50.              sscanf(str, "%d", &value);
  51.              len++; // 记录初始化多少个节点
  52.              
  53.              pNew = (PNODE)malloc(sizeof(NODE));
  54.              if (NULL == pNew)
  55.              {
  56.                       fprintf(stdout, "New Node Memory's malloc error !\n");
  57.                       exit(-1);
  58.              }
  59.              /*创建一个新的节点*/
  60.              pNew->data = value;
  61.              
  62.              pTail->next = pNew;
  63.              pNew->prior = pTail;
  64.              pTail = pNew;
  65.              pTail->next = NULL;
  66.        }
  67.      fprintf(stdout, "Initialize %d node !\n", len);
  68.        return pHead;
  69. }

  70. void traverse_list(PNODE pHead)
  71. {
  72.      if (NULL == pHead)
  73.      {
  74.               fprintf(stdout, "List is NULL !\n");
  75.               exit(-1);
  76.      }
  77.      while(pHead != NULL)
  78.      {
  79.             printf("%d\n", pHead->data);
  80.             pHead = pHead->next;
  81.      }
  82.      return;
  83. }

  84. int length_list(PNODE pHead)
  85. {
  86.     int length = 0;
  87.     PNODE pNode = NULL;
  88.     pNode = pHead;
  89.     if (NULL == pNode)
  90.     {
  91.         fprintf(stdout, "List is NULL !\n");
  92.         exit(-1);
  93.     }
  94.     while(pNode)
  95.     {
  96.         length++;
  97.         pNode = pNode->next;
  98.     }
  99.     return length;
  100. }

  101. void sort_list(PNODE pHead)
  102. {
  103.     int length;
  104.     int i, j;
  105.     int tmp;
  106.     PNODE pnode = NULL;
  107.     PNODE qnode = NULL;
  108.     printf("sort_list ... \n");
  109.         
  110.     if(NULL == pHead)
  111.     {
  112.         fprintf(stdout, "pHead is NULL !\n");
  113.         exit(-1);
  114.     }
  115.     length = length_list(pHead);

  116.     fprintf(stdout, "List Length is %d\n", length);
  117.     
  118.     for(i = 0, pnode = pHead; i < length; i++, pnode = pnode->next)
  119.     {
  120.         for(j = i+1, qnode = pnode->next; j < length; j++, qnode = qnode->next)
  121.         {
  122.             if (pnode->data > qnode->data)
  123.             {
  124.                 tmp = pnode->data;
  125.                 pnode->data = qnode->data;
  126.                 qnode->data = tmp;
  127.             }
  128.         
  129.         }
  130.     }
  131.     return;
  132. }

  133. void insert_list(PNODE pHead, int value)
  134. {
  135.     PNODE pNew;
  136.     PNODE pTail = NULL;

  137.     if (NULL == pHead)
  138.     {
  139.         fprintf(stdout, "pHead is NULL !\n");
  140.         exit(-1);
  141.     }

  142.     pNew = (PNODE)malloc(sizeof(NODE));
  143.     if (NULL == pNew)
  144.     {
  145.         fprintf(stdout, "in_node malloc error !\n");
  146.         exit(-1);
  147.     }
  148.     memset(pNew, 0, sizeof(NODE));
  149.     pNew->data = value;

  150.     pTail = pHead;
  151.     while(pTail->next != NULL)
  152.     {
  153.         pTail = pTail->next;
  154.     }
  155.     pTail->next = pNew;
  156.     pNew->prior = pTail;
  157.     pTail = pNew;
  158.     pTail->next = NULL;
  159.     fprintf(stdout, "Success insert new_node, value is %d\n", pNew->data);
  160.     //traverse_list(pHead);
  161.     return;
  162. }


  163. void delete_list(PNODE pHead, int pos)
  164. {
  165.     int length, i = 1;
  166.     PNODE pDelNode = NULL;    
  167.     PNODE pPreNode = NULL;
  168.     
  169.     fprintf(stdout, "Delete node !\n");
  170.     if (NULL == pHead)
  171.     {
  172.         fprintf(stdout, "pHead is NULL !\n");
  173.         exit(-1);
  174.     }
  175.     length = length_list(pHead);
  176.     if (length < pos)
  177.     {
  178.         fprintf(stdout, "delete's pos is error !\n");
  179.         exit(-1);
  180.     }
  181.     pPreNode = pHead;
  182.     pDelNode = pHead;
  183.     while(pDelNode->next != NULL)
  184.     {
  185.         if(i == pos)
  186.         {
  187.             break;
  188.         }
  189.         i ++;
  190.         pPreNode = pDelNode;
  191.         pDelNode = pDelNode->next;
  192.     }        
  193.     pPreNode->next = pDelNode->next;
  194.     pDelNode->next->prior = pPreNode;
  195.     free(pDelNode);
  196.     return;
  197. }

  198. int find_list(PNODE pHead, int value)
  199. {
  200.     PNODE pFindNode = NULL;
  201.     int pos = 1;
  202.     if (NULL == pHead)
  203.     {
  204.         fprintf(stdout, "List is NULL !\n");
  205.         exit(-1);
  206.     }
  207.     pFindNode = pHead;
  208.     while(pFindNode->next != NULL)
  209.     {
  210.         if (pFindNode->data == value)
  211.         {
  212.             return pos;
  213.         }
  214.         pFindNode = pFindNode->next;
  215.         pos++;
  216.     }
  217. }



  218. 测试文件doubleList.c

  219. #include "../include/doubleList.h"

  220. int main()
  221. {
  222.     int length;
  223.     PNODE pHead = NULL;
  224.     pHead = create_list();
  225.     traverse_list(pHead);    
  226.     length = length_list(pHead);
  227.     fprintf(stdout, "Total %d nodes !\n", length);
  228.     insert_list(pHead, 88);
  229.     sort_list(pHead);
  230.     traverse_list(pHead);
  231.     fprintf(stdout, "Total %d nodes !\n", length);
  232.     delete_list(pHead, 4);
  233.     traverse_list(pHead);
  234.     fprintf(stdout, "Total %d nodes !\n", length);
  235.     
  236.     fprintf(stdout, "FindPos is %d \n", find_list(pHead, 0));
  237.     return 0;    
  238. }

  239. 其中文件中用到的data.txt数据文件只是一个简单的文件,里面包含了我初始化的一些数据项。数据格式(附件包含有程序和数据): doubleList.rar  
  240. 11
  241. 22
  242. 33
  243. 4
  244. 45
  245. 34
运行环境和命令:
Linux
gcc -g -o doubleList doublist.c
阅读(1150) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~