数据结构之表(4)单循环链表

链表、双链表、单循环链表、双循环链表 的实现代码都差不多,区别只是在对指针域的修改。下面,是对单循环链表的实现

   1: # include <stdio.h>
   2: # include <stdlib.h>
   3:  
   4: /*存储结构的定义*/
   5: typedef struct node_tag {
   6:     int data ;
   7:     struct node_tag * next ;
   8: }node;
   9:  
  10: /************************************************************************/
  11: /* 操作                                                                 */
  12: /************************************************************************/
  13:  
  14: /*初始化循环链表*/
  15: void ds_init(node ** pNode) {
  16:     int item ;
  17:     node * temp ;
  18:     node * target ;
  19:  
  20:     printf("输入结点的值,输入0完成初始化\n");
  21:     while(1) {
  22:         scanf("%d",&item) ;
  23:         fflush(stdin) ;
  24:         if(item == 0)
  25:             return ;
  26:  
  27:         if((*pNode) == NULL) { /*循环链表中只有一个结点*/
  28:                 *pNode = (node*)malloc(sizeof(struct node_tag)) ;
  29:                 if(!(*pNode))
  30:                     exit(0) ;
  31:                 (*pNode)->data = item ;
  32:                 (*pNode)->next = *pNode ;
  33:             }
  34:         else {
  35:             /*找到next指向第一个结点的结点*/
  36:             for(target = (*pNode) ; target->next != (*pNode) ; target = target->next) ;
  37:  
  38:             /*生成一个新的结点*/
  39:             temp = (node *) malloc(sizeof(struct node_tag)) ;
  40:             if(!temp)
  41:                 exit(0) ;
  42:             temp->data = item ;
  43:             temp->next = *pNode ;
  44:             target->next = temp ;
  45:         }
  46:     }    
  47: }
  48:  
  49: /*插入结点*/
  50: /*参数:链表的第一个结点,插入的位置*/
  51: void ds_insert(node ** pNode ,int i) {
  52:     node * temp ;
  53:     node * target ;
  54:     node * p ;
  55:     int item ;
  56:     int j = 1 ;
  57:  
  58:     printf("输入要插入结点的值:");
  59:     scanf("%d",&item) ;
  60:  
  61:     if(i == 1) { //新插入的结点作为第一个结点
  62:         temp = (node *)malloc(sizeof(struct node_tag)) ;
  63:         if(!temp)
  64:             exit(0) ;
  65:         temp ->data = item ;
  66:  
  67:         /*寻找到最后一个结点*/
  68:         for(target = (*pNode) ; target->next != (*pNode) ; target = target->next) ;
  69:         temp->next = (*pNode) ;
  70:         target->next = temp ;
  71:         *pNode = temp ;
  72:     }
  73:     else {
  74:         target = *pNode ;
  75:         for( ; j < (i-1) ; target=target->next,++ j) ;
  76:         temp = (node *)malloc(sizeof(struct node_tag)) ;
  77:         if(!temp)
  78:             exit(0) ;
  79:         temp ->data = item ;
  80:         p = target->next ;
  81:         target->next = temp ;
  82:         temp->next = p ;
  83:     }
  84: }
  85:  
  86: /*删除结点*/
  87: void ds_delete(node ** pNode,int i) {
  88:     node * target ;
  89:     node * temp ;
  90:     int j = 1 ;
  91:  
  92:     if(i == 1) { //删除的是第一个结点
  93:         /*找到最后一个结点*/
  94:         for(target = *pNode ; target->next != *pNode ;target = target->next) ;
  95:         temp = *pNode ;
  96:         *pNode = (*pNode)->next ;
  97:         target->next = *pNode ;
  98:         free(temp) ;
  99:     }
 100:     else {
 101:         target = *pNode ;
 102:         for( ; j < i-1 ; target= target->next,++j) ;
 103:         temp = target->next ;
 104:         target->next = temp->next ;
 105:         free(temp) ;
 106:     }
 107: }
 108:  
 109: /*返回结点所在位置*/
 110: int ds_search(node * pNode,int elem) {
 111:     node * target ;
 112:     int i = 1 ;
 113:  
 114:     for(target = pNode ; target->data != elem && target->next != pNode ; ++i , target = target->next) ;
 115:     if(target->next == pNode) /*表中不存在该元素*/
 116:         return 0 ;
 117:     else
 118:         return i ;
 119: }
 120: /*遍历*/
 121: void ds_traverse(node * pNode) {
 122:     node * temp ;
 123:     temp = pNode ;
 124:     printf("***********链表中的元素******************\n");
 125:     do {
 126:         printf("%4d ",temp->data) ;
 127:     }while((temp = temp->next) != pNode) ;
 128:     printf("\n") ;
 129: }

测试程序

   1: int main()
   2: {
   3:     node * pHead = NULL ;
   4:     ds_init(&pHead) ;
   5:     ds_traverse(pHead) ;
   6:     ds_insert(&pHead,3) ;
   7:     printf("在位置3插入值后:\n") ;
   8:     ds_traverse(pHead) ;
   9:  
  10:     ds_delete(&pHead,2) ;
  11:     printf("删除第2个结点后:\n") ;
  12:     ds_traverse(pHead) ;
  13:     printf("元素5所在位置:%d\n",ds_search(pHead,5)) ;
  14:     fflush(stdin) ;
  15:     getchar() ;
  16:     return 0 ;
  17: }

运行结果:

2

要想看循环链表的存储结构,可以在初始化完成后,加入断点查看,结果下图:

3

posted on 2011-04-26 14:30  codmer  阅读(1632)  评论(0编辑  收藏  举报