Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302878
  • 博文数量: 172
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 895
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-09 16:57
文章分类

全部博文(172)

文章存档

2012年(86)

2011年(86)

分类:

2011-11-10 10:20:35

代码: double-list.rar  

插入节点时,都是将 *prior *next 指向了 data 首地址

*prior *next,就是,上一个、下一个 结点的首地址


list.h 文件

  1. #ifndef _LIST_H
  2. #define _LIST_H

  3. typedef char ElemType;
  4. /*typedef struct _stu
  5. {

  6. }ElemType;*/

  7. typedef struct _node
  8. {
  9.     ElemType data; //数据域
  10.     struct _node *prior,*next;
  11. }LIST;

  12. LIST *init_list();
  13. int insert_elem(LIST *l,ElemType *e,int i);
  14. int delete_elem(LIST *l,int i);
  15. int get_length(LIST *l);

  16. #endif

list.c 文件


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"

  4. //初始化
  5. LIST *init_list()
  6. {
  7.     LIST *l=(LIST *)malloc(sizeof(LIST));//开辟 head 结点
  8.     if(l == NULL)exit(1);

  9.     l->prior=l->next=NULL; //初始化 head 结点 prior next=NULL
  10.     return l;
  11. }


  12. int insert_elem(LIST *l,ElemType *e,int i)
  13. {
  14.     int j=1;

  15.     LIST *p=l; //指向 head 结点
  16.     LIST *new;
  17.     
  18.     if(i < 1)
  19.         return 1;

  20.     new=(LIST *)malloc(sizeof(LIST));
  21.     if(new==NULL)
  22.         return 1;
  23.     new->data=*e;
  24.     new->prior=NULL;
  25.     new->next=NULL;
  26.     
  27.     while(j<i) //因为从head 开始,所以这里是定位到 i-1 个结点
  28.     {
  29.         p=p->next; //p 指向 i-1 结点
  30.         j++;
  31.     }

  32.     new->next=p->next; //prior next 都是指向了首地址=data地址
  33.     new->prior=p;
  34.     if(p->next!=NULL)// p 不是最后一个结点
  35.         new->next->prior=new;
  36.     p->next=new;//插入节点时,都是将 *prior *next 指向data 首地址
  37.     return 0;
  38. }

  39. int delete_elem(LIST *l,int i)
  40. {
  41.     LIST *p=l; //指向了 head 结点
  42.     LIST *tmp;
  43.     
  44.     int j=1;
  45.     
  46.     while(j<i) //定位到 i-1 结点
  47.     {
  48.         p=p->next;
  49.         j++;
  50.     }

  51.     tmp=p->next; //指向要删除的结点
  52.     p->next=tmp->next;
  53.     if(tmp->next!=NULL) //tmp不是最后结点,
  54.         tmp->next->prior=p; // i+1 结点 指向 i-1 结点
  55.     free(tmp);
  56.     
  57.     return 0;
  58. }


main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "list.h"

  4. int main()
  5. {
  6.     ElemType ch1='a';    
  7.     ElemType ch2='b';
  8.     ElemType ch3='c';
  9.     LIST *list;    
  10.     list=init_list(); // 初始化 爽链表,malloc head 头结点

  11.     insert_elem(list,&ch1,1); //插入 结点
  12.     insert_elem(list,&ch2,1);
  13.     insert_elem(list,&ch3,3);
  14.     printf("1=%c\n",list->next->data); //打印 结点data 数据
  15.     printf("2=%c\n",list->next->next->data);
  16.     printf("3=%c\n",list->next->next->next->data);    
  17.     printf("length=%d\n",get_length(list)); //显示 结点数

  18.     delete_elem(list,1); //删除结点
  19.     printf("1=%c\n",list->next->data);
  20.     printf("2=%c\n",list->next->next->data);
  21.     delete_elem(list,2); //删除结点
  22.     printf("1=%c\n",list->next->data);
  23.     printf("length=%d\n",get_length(list)); //显示结点数
  24.     return 0;
  25. }

显示:
  1. ywx@yuweixian:~/yu/data-struct/book/double-list$ ./main
  2. 1=b
  3. 2=a
  4. 3=c
  5. length=3
  6. 1=a
  7. 2=c
  8. 1=a
  9. length=1

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