Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1212044
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 14:59:07


点击(此处)折叠或打开

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

  4. #define NAMELEN 32
  5. #define    ADDRLEN 128

  6. /*双向链表的虚拟实现*/
  7. typedef struct dunode
  8. {
  9.     char name[NAMELEN];
  10.     char addr[ADDRLEN];
  11.     char sex;
  12.     float score;
  13.     int id;
  14.     struct dunode * next;
  15.     struct dunode * prior;
  16. }DUNODE;

  17. typedef DUNODE * DuNodePtr;

  18. int InitDuList(DuNodePtr * headptr);
  19. int CreatDuNode(DuNodePtr * nodeptr, char * name, char * addr, char sex, float score, int id);
  20. int LenDuList(DuNodePtr headptr);
  21. int GetDuNode(DuNodePtr headptr, int num, DuNodePtr * nodeptr);
  22. int AddDuNode(DuNodePtr headptr, DuNodePtr nodeptr);
  23. int PrintAllDuNode(DuNodePtr headptr);
  24. int InserDuNode(DuNodePtr headptr, int num, DuNodePtr nodeptr);
  25. int DeleteDuNode(DuNodePtr headptr, int num);
  26. int UnionDuList(DuNodePtr headptr1, DuNodePtr headptr2);
  27. int SortDuNode(DuNodePtr headptr);
  28. int SortDuNode2(DuNodePtr headptr);

  29. int main(int argc, char ** argv)
  30. {
  31.     printf("*********双向循环链表的操作********\n");
  32.     
  33.     DuNodePtr headptr = NULL;
  34.     InitDuList(&headptr); /*需传指针的地址,才能改变指针变量的值*/
  35.     
  36.     DuNodePtr nodeptr = NULL;
  37.     CreatDuNode(&nodeptr, "王贤才", "湖北", 'm', 100.00, 1);
  38.     AddDuNode(headptr, nodeptr);
  39.     CreatDuNode(&nodeptr, "张秋苹", "湖北", 'w', 100.00, 2);
  40.     AddDuNode(headptr, nodeptr);
  41.     CreatDuNode(&nodeptr, "程序员", "上海", 'm', 100.00, 3);
  42.     AddDuNode(headptr, nodeptr);
  43.     CreatDuNode(&nodeptr, "我老婆", "上海", 'w', 100.00, 4);
  44.     AddDuNode(headptr, nodeptr);
  45.     CreatDuNode(&nodeptr, "王贤才", "上海", 'm', 99.00, 6);
  46.     AddDuNode(headptr, nodeptr);
  47.     PrintAllDuNode(headptr);
  48.     
  49.     DuNodePtr getnodeptr = NULL;
  50.     int num = 3;
  51.     GetDuNode(headptr, num, &getnodeptr);
  52.     printf("*************取得第[%d]个元素如下:************\n", num);
  53.     printf(" id =[%d]\n name =[%s]\n addr =[%s]\n sex =[%c]\n score=[%.2f]\n", getnodeptr->id, getnodeptr->name, getnodeptr->addr, getnodeptr->sex, getnodeptr->score);
  54.     
  55.     CreatDuNode(&nodeptr, "我老子", "上海", 'm', 98.00, 5);
  56.     num = 3;
  57.     InserDuNode(headptr, num, nodeptr);
  58.     
  59.     /*
  60.     num = 4;
  61.     DeleteDuNode(headptr, num);
  62.     */
  63.     
  64.     PrintAllDuNode(headptr);
  65.     
  66.     DuNodePtr headptr2 = NULL;
  67.     InitDuList(&headptr2); /*需传指针的地址,才能改变指针变量的值*/
  68.     
  69.     DuNodePtr nodeptr2 = NULL;
  70.     CreatDuNode(&nodeptr2, "王贤才", "湖北", 'm', 98.00, 13);
  71.     AddDuNode(headptr2, nodeptr2);
  72.     CreatDuNode(&nodeptr2, "张秋苹", "湖北", 'w', 99.00, 12);
  73.     AddDuNode(headptr2, nodeptr2);
  74.     CreatDuNode(&nodeptr2, "程序员", "上海", 'm', 100.00, 11);
  75.     AddDuNode(headptr2, nodeptr2);
  76.     
  77.     printf("********************\n");
  78.     UnionDuList(headptr, headptr2);
  79.     PrintAllDuNode(headptr);
  80.     
  81.     SortDuNode(headptr);
  82.     PrintAllDuNode(headptr);
  83.     
  84.     SortDuNode2(headptr);
  85.     PrintAllDuNode(headptr);
  86.     
  87.     return 0;
  88. }

  89. /*功能:初始化双向循环链表:(带头结点的)初始时链表为空*/
  90. int InitDuList(DuNodePtr * headptr)
  91. {
  92.     printf("初始化双向循环链表......\n");
  93.     *headptr = (DuNodePtr)malloc(sizeof(DUNODE)); /*头结点:数据域为空"*/
  94.     (*headptr)->next = *headptr;
  95.     (*headptr)->prior = *headptr;
  96.     return 0;
  97. }

  98. /*创建一个节点*/
  99. int CreatDuNode(DuNodePtr * nodeptr, char * name, char * addr, char sex, float score, int id)
  100. {
  101.     *nodeptr = (DuNodePtr)malloc(sizeof(DUNODE));
  102.     memset((*nodeptr)->name, 0, sizeof(char)*NAMELEN);
  103.     strcpy((*nodeptr)->name, name);
  104.     memset((*nodeptr)->addr, 0, sizeof(char)*ADDRLEN);
  105.     strcpy((*nodeptr)->addr, addr);
  106.     (*nodeptr)->sex = sex;
  107.     (*nodeptr)->score = score;
  108.     (*nodeptr)->id = id;
  109.     (*nodeptr)->next = NULL;
  110.     (*nodeptr)->prior = NULL;
  111.     return 0;
  112. }

  113. /*功能:求双向循环链表的长度,参数node:头结点指针*/
  114. int LenDuList(DuNodePtr headptr)
  115. {
  116.     int len = 0;
  117.     DuNodePtr p = NULL;
  118.     p = headptr->next;
  119.     while(p != headptr)
  120.     {
  121.         len++;
  122.         p = p->next;
  123.     }
  124.     return len;
  125. }

  126. /*功能:求双向循环链表中的第num个元素的节点指针*/
  127. int GetDuNode(DuNodePtr headptr, int num, DuNodePtr * nodeptr)
  128. {
  129.     DuNodePtr p = NULL;
  130.     int count = 1;
  131.     
  132.     p = headptr->next;
  133.     while((p != headptr)&&(count < num))
  134.     {
  135.         p = p->next;
  136.         count++;
  137.     }
  138.     if((p == headptr)||(count > num)) /*第num个元素不存在*/
  139.     {
  140.         printf("第[%d]个元素不存在!\n", num);
  141.         return -1;
  142.     }
  143.     else if((p != headptr)&&(count == num)) /*第num个元素存在*/
  144.     {
  145.         *nodeptr = p;
  146.         return 0;
  147.     }
  148. }

  149. /*功能:在双向循环链表尾插入一个元素*/
  150. int AddDuNode(DuNodePtr headptr, DuNodePtr nodeptr)
  151. {
  152.     printf("在链尾增加一个元素!\n");
  153.     DuNodePtr p = NULL;
  154.     
  155.     p = headptr;
  156.     while(p->next != headptr) /*p指向最后一个节点元素了*/
  157.     {
  158.         p = p->next;
  159.     }
  160.     p->next = nodeptr; /*连接后继指针*/
  161.     nodeptr->prior = p; /*连接前驱指针*/
  162.     nodeptr->next = headptr; /*首尾相连*/
  163.     headptr->prior = nodeptr;
  164.     return 0;
  165. }

  166. /*功能:打印出双向循环链表中所有节点元素的信息*/
  167. int PrintAllDuNode(DuNodePtr headptr)
  168. {
  169.     DuNodePtr p = NULL;
  170.     p = headptr->next;
  171.     printf("链表长度=[%d]\n", LenDuList(headptr));
  172.     printf("显示链表的所有内容如下:\n");
  173.     printf("*************************************************************************\n");
  174.     printf("id        name        addr        sex        score\n");
  175.     while(p != headptr)
  176.     {
  177.         printf("[%d]        [%s]    [%s]        [%c]        [%.2f]\n", p->id, p->name, p->addr, p->sex, p->score);
  178.         p = p->next;
  179.     }
  180.     return 0;
  181. }

  182. /*功能:在双向循环链表的第num个元素之前插入一个元素(先找到第num个元素)*/
  183. int InserDuNode(DuNodePtr headptr, int num, DuNodePtr nodeptr)
  184. {
  185.     int count = 1;
  186.     DuNodePtr p = NULL;
  187.     p = headptr->next;
  188.     while(p != headptr) /*p会指向第num个元素*/
  189.     {    
  190.         if(count == num) break;
  191.         p = p->next;
  192.         count++;
  193.     }
  194.     
  195.     if((p == headptr)||(count > num)) /*第num个元素不存在*/
  196.     {
  197.         printf("第[%d]个元素不存在!\n", num);
  198.         return -1;
  199.     }
  200.     else if((p != headptr)&&(count == num)) /*p指向第num个元素*/
  201.     {/*断链是有一定顺序的*/
  202.         nodeptr->prior = p->prior;            /*第1步:让新节点的前驱指向第num-1个节点*/
  203.         p->prior->next = nodeptr;            /*第2步:让第num-1个节点的后驱指向新节点*/
  204.         p->prior = nodeptr;                    /*第3步:让第num个节点的前驱指向新节点*/
  205.         nodeptr->next = p;                    /*第4步:让新节点的后驱指向第num个节点*/
  206.         return 0;
  207.     }
  208.     
  209.     return 0;
  210. }

  211. /*功能:删除双向循环链表中的第num个元素*/
  212. int DeleteDuNode(DuNodePtr headptr, int num)
  213. {
  214.     int count = 1;
  215.     DuNodePtr p = NULL;
  216.     
  217.     p=headptr->next;
  218.     while(p != headptr) /*p会指向第num个元素*/
  219.     {    
  220.         if(count == num) break;
  221.         p = p->next;
  222.         count++;
  223.     }
  224.     
  225.     if((p == headptr)||(count > num)) /*第num个元素不存在*/
  226.     {
  227.         printf("第[%d]个元素不存在!\n", num);
  228.         return -1;
  229.     }
  230.     else if((p != headptr)&&(count == num)) /*p指向第num个元素*/
  231.     {
  232.         p->prior->next = p->next;            /*第1步:第num-1个元素的后继指向第num-2个元素*/
  233.         p->next->prior = p->prior;            /*第2步:第num+1个元素的前驱指向第num-1个元素*/
  234.         free(p); /*删除的节点要释放*/
  235.         return 0;
  236.     }
  237.     
  238.     return 0;
  239. }

  240. /*功能:按照数据域中某个数据成员的值(id)对双向循环链表的元素进行递减排序
  241. 使用条件:链表中至少要有2个节点元素*/
  242. int SortDuNode(DuNodePtr headptr)
  243. {
  244.     int i, j, len;
  245.     DuNodePtr p0 = NULL;
  246.     DuNodePtr p1 = NULL;
  247.     DuNodePtr p2 = NULL;
  248.     DuNodePtr p3 = NULL;
  249.     
  250.     printf("对双链表排序如下:\n");
  251.     len = LenDuList(headptr);
  252.     printf("len=[%d]\n", len);
  253.     /*只能用冒泡法排序:相邻两数比较,对链表来说,交换相邻两数要相对容易的多*/
  254.     for(i=0; i
  255.     {
  256.         p0 = headptr; /*p0指向头节点*/
  257.         for(j=0; j
  258.         {
  259.             /*使每个元素都有自己的*/
  260.             p1 = p0->next;
  261.             p2 = p0->next->next;
  262.             p3 = p0->next->next->next;
  263.             /*p1,p2是要交换的2个节点的指针,p0是这2个节点之前的节点指针,p3值这2个节点之后的节点*/
  264.             /*4个节点的指针都已经保存,就绝对不会断链了,也可以只定义3个*/
  265.             if(p1->id < p2->id)
  266.             {
  267.                 p1->next = p3;
  268.                 /*p2的next域非空:即若p2后面还有元素,则其后面的元素的前驱也要赋值
  269.                 如果其后面的元素不存在,当然其前驱也不存在了,也就不需要赋值*/
  270.                 //if(p3 != headptr)
  271.                 //{    
  272.                     p3->prior = p1;
  273.                 //}
  274.                 p0->next = p2;
  275.                 p2->prior = p0;
  276.                 p2->next = p1;
  277.                 p1->prior = p2;
  278.             }
  279.             p0 = p0->next; /*第一个最小的已经排好,所以p0可以指向下一个节点了*/
  280.         }
  281.     }
  282.     
  283.     return 0;
  284. }

  285. /*功能:按照数据域中某个数据成员的值(id)对双向循环链表的元素进行递增排序
  286. 使用条件:链表中至少要有2个节点元素*/
  287. int SortDuNode2(DuNodePtr headptr)
  288. {
  289.     int i, j, len;
  290.     DuNodePtr p0 = NULL, p1 = NULL, p2 = NULL, p3 = NULL;
  291.     
  292.     printf("对双链表排序如下:\n

阅读(510) | 评论(0) | 转发(0) |
0

上一篇:双向非循环链表

下一篇:查找

给主人留下些什么吧!~~