Chinaunix首页 | 论坛 | 博客
  • 博客访问: 281350
  • 博文数量: 95
  • 博客积分: 618
  • 博客等级: 中士
  • 技术积分: 455
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-28 13:39
文章分类

全部博文(95)

文章存档

2015年(65)

2013年(1)

2012年(10)

2011年(19)

分类: C/C++

2011-11-30 20:46:23

 

  1. typedef struct node
  2. {
  3.     int element;
  4.     struct node *front;
  5.     struct node *next;
  6. }node;

  7. /*
  8. 创建空表头
  9. */
  10. node *creathead()
  11. {
  12.     node *head=(node *)malloc(sizeof(node));
  13.     if (head==NULL)
  14.     {
  15.         return NULL;
  16.     }
  17.     head->front=head;
  18.     head->next=head;
  19.     return head;
  20. }

  21. /*
  22. 插入节点
  23. */
  24. int insert(node *point,int n)
  25. {
  26.     if (point==NULL)
  27.     {
  28.         return 0;
  29.     }
  30.     node *p=(node *)malloc(sizeof(node));
  31.     p->element=n;
  32.     node *tmp=point->next;
  33.     p->next=tmp;
  34.     p->front=point;
  35.     point->next=p;
  36.     tmp->front=p;
  37.     return 0;
  38. }

  39. /*
  40. 顺序遍历节点
  41. */
  42. int printlistn(node *head)
  43. {
  44.     node *p=head;
  45.     while (p->next!=head)
  46.     {
  47.         p=p->next;
  48.         printf("%d ",p->element);
  49.     }
  50.     return 0;
  51. }

  52. /*
  53. 逆序遍历节点
  54. */
  55. int printlistf(node *head)
  56. {
  57.     node *p=head;
  58.     while (p->front!=head)
  59.     {
  60.         p=p->front;
  61.         printf("%d ",p->element);
  62.     }
  63.     return 0;
  64. }

  65. /*
  66. 查找
  67. */
  68. node* find(node *p,int n)
  69. {
  70.     if (p->element==n) //就是这个,不找了
  71.     {
  72.         return p;
  73.     }
  74.     node *tmp=p->next; //从下一个开始
  75.     while (tmp!=p)
  76.     {
  77.         if (tmp->element==n)
  78.         {
  79.             return tmp;
  80.         }
  81.         tmp=tmp->next;
  82.     }
  83.         return NULL;
  84. }

  85. /*
  86. 删除
  87. */
  88. int deletnode(node *p,int n)
  89. {
  90.     node *p1=find(p,n);
  91.     if (p1==NULL)
  92.     {
  93.         printf("要删除的结点不存在\n");
  94.         return 0;
  95.     }
  96.     node *pfront=p1->front;        //前一个结点
  97.     node *pnext=p1->next;        //下一个结点
  98.     pfront->next=p1->next;
  99.     pnext->front=p1->front;
  100.     free(p1);
  101.     return 0;
  102. }
阅读(1681) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~