数据结构之表(3)双链表

双链表的实现和单链表差不多,就是多了一个指向结点前驱的指针域,其实现和单链表差不多。

   1: # ifndef DOUBLELINKLIST_H
   2: # define DOUBLELINKLIST_H
   3:  
   4: /************************************************************************/
   5: /* 存储结构                                                              */
   6: /************************************************************************/
   7: typedef struct ds_double_node {
   8:     int data ;
   9:     struct ds_double_node * next ;
  10:     struct ds_double_node * before ;
  11: }ds_doubleList_head;
  12:  
  13: /************************************************************************/
  14: /* 基本操作                                                             */
  15: /************************************************************************/
  16:  
  17: /*初始化,返回指向头结点的指针*/
  18: ds_doubleList_head * ds_double_init() ;
  19:  
  20: //
  21: //下面是Insert操作
  22: //
  23:  
  24: /*插入,在表的开始*/
  25: int ds_double_insertFront(ds_doubleList_head * pHead,int elem) ;
  26:  
  27: /*插入,在表的末尾*/
  28: int ds_double_insertEnd(ds_doubleList_head * pHead,int elem) ;
  29:  
  30: /*插入,在特定位置前/后*/
  31: int ds_double_insertSpecific(ds_doubleList_head * pHead,int i,int elem) ;
  32:  
  33: //
  34: //删除操作
  35: //
  36:  
  37: /*删除表头*/
  38: void ds_double_deleteFront(ds_doubleList_head * pHead) ;
  39:  
  40: /*删除表尾*/
  41: void ds_double_deleteEnd(ds_doubleList_head * pHead) ;
  42:  
  43: /*删除特定位置的结点*/
  44: void ds_double_deleteSpecific(ds_doubleList_head * pHead,int i) ;
  45:  
  46: //
  47: //查找
  48: //
  49:  
  50: /*查找某个数据在表中的位置,不在表中返回0.*/
  51: int ds_double_search(ds_doubleList_head * pHead,int elem) ;
  52:  
  53: /*查找某数据的前驱*/
  54: int ds_double_searchPre(ds_doubleList_head * pHead,int elem) ;
  55:  
  56: /*求后继*/
  57: int ds_double_searchNext(ds_doubleList_head * pHead,int elem) ;
  58:  
  59: //
  60: //其他操作
  61: //
  62:  
  63: /*释放存储空间*/
  64: void ds_double_destroy(ds_doubleList_head * pHead) ;
  65:  
  66: /*遍历*/
  67: void ds_double_traverse(ds_doubleList_head * pHead) ;
  68:  
  69: # endif

  这里只是声明,具体的实现可参照单链表的实现。

  在对单链表进行操作时,用到比较多的操作是:获取某个结点和交换两个结点。

  交换两个结点

     1: void ds_swap(struct ds_link_node * node1,struct ds_link_node * node2) {
     2:     struct ds_link_node temp ;
     3:     temp = *node1 ;
     4:     *node1 = *node2 ;
     5:     *node2 =  temp ;
     6: }

  获取某个结点

     1: struct ds_link_node * ds_getNode(ds_link_head * pHead,int i) {
     2:     struct ds_link_node * temp ;
     3:     int j;
     4:     if(i > pHead->data || i < 0)
     5:         return NULL ;
     6:     temp = pHead->next ;
     7:     for(j = 0 ; j < i ; j ++ ,temp = temp->next)  ;
     8:     return temp ;
     9: }

转载于:https://www.cnblogs.com/lovelq522/archive/2011/04/24/2026476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值