数据结构之表(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: }
posted on 2011-04-24 21:43  codmer  阅读(293)  评论(0)    收藏  举报