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

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 14:58:20


点击(此处)折叠或打开

  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.     UnionDuList(headptr, headptr2);
  78.     PrintAllDuNode(headptr);
  79.     
  80.     SortDuNode(headptr);
  81.     PrintAllDuNode(headptr);
  82.     
  83.     SortDuNode2(headptr);
  84.     PrintAllDuNode(headptr);
  85.     
  86.     return 0;
  87. }

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

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

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

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

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

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

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

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

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

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

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

上一篇:单循环链表

下一篇:双向循环链表

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