数据结构算法笔记 lesson6 循环链表1

单循环链表

将单链表中终端结点的指针由空指针改为指向头结点,就使整个单链表形成一个环,这种头尾相接的单链表称为单循环链表

循环链表和单链表的主要差异

 循环的判空链表的条件上,判断head->next是否为null , head->next 是否等于 head

初始化

typedef struct CLinkList
{
        int data;
        struct CLinkList  *next;
}
void ds_init( node **pNode)
{
        int item;
        node *temp;
        node *target;
        printf ("输入节点的值");
        while(1)
         {
            scanf("%d",&item);
            fflush(stdin);
             if(item==0)
                return; 
             if((*pNode)==NULL)
              {
                 *pNide = (node*)malloc (sizeof(struct CLinkList));
                   if(!(*pNode))
                    exit(0);
                 (*pNode)->data = item;
                 (*pNode)->next = *pNode;
              }
         else
        {
                for(target=(*pNode);target->next !=(*pNode); target=target->next);
                temp= (node*)malloc (sizeof(struct CLinkList));
                if(!temp)
                 exit(0);

                temp->data = item;
                temp->next= *pNode;
                target->next = temp;
             }
        }
   }

插入

void ds_insert(node**pNode , int i)
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j =1;
    printf("请输入:");
    scanf("%d",&item);
    if(i==1)
    {
      temp = (node*)malloc(sizeof(struct CLinkList));
      if (!temp)
       exit(0);
      temp -> data = 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;
        temp = (node *)malloc (sizeof(struct CLinkList));
        if(!temp)
          exit(0);
        temp ->data = item;
         
        p = target ->next;
        target->next = temp;
        temp ->next =p ;
     }

删除

 void ds_delete(node **pNode , int i)
{
    node * target;
    node* temp;
    int j = 1;
    if(j==1)
   {
     for (target = (*pNode);target->next ! = (*pNode);target= target->next);
     temp = *pNode;
     *pNode = (*pNode)->next;
     target->next = *pNode;
     free(temp);
    }
  else
   {
    target = *pNode;
    for(; jnext;
    temp =target ->next;
    target->next = temp ->next;
    free(temp);
   }
}

返回结点所在位置

int ds_search(node *pNode, int elem)
{
    node *target;
    int  i =1;
    for (target = pNode;target->data! = elem&& target->next != pNode;++i)
     {
        target = target->next;
     }
      if(target->next == pNode)
          return 0 ; 
       else 
          return 1;
}

约瑟夫问题

 #include
 #include 
typedef  node
{
   int data;
   struct node* next;
}node;

node *create (int n)
{
    node *p = NULL, *head;
    head = (node*)malloc (sizeof(node));
    p =head;
    node *s;
    int i = 1;
    if(0!=n)
     {
         while(i<=n)
      {
         s =(node*)malloc (sizeof(node));
         s->data = i++;
         p->next = s ;
         p = s;
      }
         s->next = head->next;
     }
     free (head);
    return s->next;
}
  
int main()
{
     int n =41;
     int m =3;
     int i ;
     node *p =create(n);
     node *temp;
     m%=n;
   while(p != p->next)
    {
       for (i=1;inext ;
       }
      printf("%d->" , p->next->data);

      temp = p->next;
      p->next = temp->next;
      free(temp);
      p = p->next;
    }
  printf("%d\n",p->data);
  return 0;
}


 

你可能感兴趣的:(数据结构,算法学习笔记)