码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构/循环链表

时间:2014-12-03 18:30:57      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:ar   sp   for   on   数据   div   bs   ad   ef   

 
尾指针   *real 
 
(head->next=dead)
 
1初始化
void ds_init( node **pNode)
 
{
 int item;
 node *temp,*target;
 printf("输入终点的值,输入0完成出初始化\n");
 
 while(1)
 {
  scanf("%d",&item);
   fflush(stdin);
   if (item==0)
   return ;
  if ((*pNode)==NULL)
  {//循环链表只有一个节点
   *pNode=(node*)malloc(sizeof(struct CLinklist));
   if (!(*pNode))
   {
    exit (0);
    (*pNode)->date=item;
    (*pNode)->next=*pNode;
 
 
   }
   else {
    //找到next指向第一个节点的终点
    for (target=(*pNode); target->next!=(*pNode);target=target->next)
    {
     //生成一个新的终点
     temp=(*node)malloc(sizeof(struct CLinklist));
     if (!temp)
      exit(0);
     temp->date=item;
     temp->next=*pNode;
     target->next=temp;
 
        } 
   }
  }
 }
}
 
 
 
2链表的插入:
//链表存储结构的定义
typedef struct CLinkList
{
 int date;
 struct CLinkList *next;
 
}node;
 
//插入终点
//参数: 链表的第一个节点;插入的位置
void ds_insert(node **pNode,int i )
{
 node *temp;
 node *target,*p;
 int j=i;
 printf("输入要插入的节点的值:");
 scanf("%d",&item);
 if (i==1)
 {
  //新插入的节点的第一个节点
  temp=(node*)malloc(sizeof(struct CLinkList));
  if (!temp)
   exit(0);
  temp->date=item;
  //寻找到最后一个节点
  for (target=(*pNode);target->next!=(*pNode);target=target->next);
   temp->next=(*pNode);
      target->next=temp;
      *pNode=temp;
  }
  else
  {
   target=*pNode;
   for (; j < (i-1); j++)
   {
    target=target->next;
 
    /* code */
    //循环2次
   }
   temp=(node*)malloc(sizeof(struct CLinkList));
   if (!item)
    exit (0);
   temp->date=item;
   p=target->next;
   temp->next=p;
 
 
 
  }
 }
3 删除节点:
//删除节点
//参数说明:参数1:该节点的值;参数2:第i个节点
void ds_delete(node **pNode ,int i)
{
 node *target,*temp;
 int j=1;
 if (i==1)
 {
  //删除的是di一个节点
 
  //找到最后一个节点
  for (target =*pNode ;target->next!=*pNode;target=target->next)
  {
   temp=*pNode;
   *pNode=(*pNode)->next;
   target->next=*pNode;
   free(temp);
 
 
   /* code */
  }
else
{
 target=*pNode;
 for (; j < i-1; j++)
 {
  target=target->next;
 
  /* code */
 }
 
 temp=target->next;
 target->next=temp->next;
 free(temp);
 
 
}
 
 }
 
 
 
}
4 查找节点:
//查找节点
 
int ds_search(node *pNode ,int elem)
{
 node *target;
 int i=1;
 for (target=*pNode; target->date!=elem&&target->next!=*pNode; ++i)
 {
  target=target->next;
 
  /* code */
 }
if (target->next==pNode)//表中不存在的元素
 return -1;
else return 1;
 
 
}
 
 

数据结构/循环链表

标签:ar   sp   for   on   数据   div   bs   ad   ef   

原文地址:http://www.cnblogs.com/da-guang/p/4140636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!