单链表操作

include<stdio.h>
#include<stdlib.h>
#include<string.h>

 typedef char LinkType; 

 typedef struct LinkNode 
{ 
   LinkType data; 

   struct LinkNode* next; 

}  LinkNode; 

/** 
 *  * @brief 初始化链表 
 *  * 
 *   * @param head 
 *    */ 
void LinkListInit(LinkNode** head)
{   
   if(head==NULL)
    {
      printf("There is no head pointer!");
      exit(0);
    }
 
    *head=NULL;

}

/** 
 *  * @brief 尾插一个元素到链表中 
 *  * 
 *   * @param head 
 *    * @param value 
 *     */ 
LinkNode* LinkListPushBack(LinkNode** head, LinkType value)
{ 
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
   if(*head==NULL)
    {
      *head=(LinkNode*)malloc(sizeof(LinkNode));
      (*head)->data=value;
      (*head)->next=NULL;
    }
   else
    {
     LinkNode *p = *head;
      while(p->next!=NULL)
      {
        p=p->next;    
      }
        LinkNode *tmp=(LinkNode*)malloc(sizeof(LinkNode));
        tmp->data=value;
        tmp->next=NULL;
        p->next=tmp;
      
     }


} 

/** 
 *  * @brief 尾删一个元素 
 *  * 
 *   * @param head 
 *    */ 
void LinkListPopBack(LinkNode** head) 
{
   
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
   if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }
    if((*head)->next==NULL)
    {
      free(*head);
      *head=NULL;
    }
    else
    {
       LinkNode * p=*head;
      while(p->next->next!=NULL)
     {
         p=p->next; 
     }
      free(p->next);
      p->next=NULL;
    }


}



/** 
 *  * @brief 头插一个元素 
 *  * 
 *   * @param head 
 *    * @param value 
 *     */ 
void LinkListPushFront(LinkNode** head, LinkType value) 
{
  
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
   if(*head==NULL)
    {
      *head=(LinkNode*)malloc(sizeof(LinkNode));
      (*head)->data=value;
      (*head)->next=NULL;
    }
    else
    {
      LinkNode* p=(LinkNode*)malloc(sizeof(LinkNode));
                p->data=value;
                p->next=*head;
                *head=p;
    }
   
}


/** 
 *  * @brief 头删一个元素 
 *  * 
 *   * @param head 
 *    */ 
void LinkListPopFront(LinkNode** head)
{

    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }
 
    if((*head)->next==NULL)
    {
      free(*head);
      *head=NULL;
    }
    else
    {
      LinkNode *p=*head;
      *head=(*head)->next;
      free(p);
      p=NULL;
     }



}

/** 
 *  * @brief 查找元素在链表中的位置 
 *  * 
 *   * @param head 
 *    * @param to_find 要查找的值 
 *    * 
 *     * @return 这个值对应的节点的地址 
 *     */ 
LinkNode* LinkListFind(LinkNode* head, LinkType to_find) 
{
    if(head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }
    while(head!=NULL)
    {
       if(head->data== to_find)
       {
         return head;
       }  
        head=head->next;
    }

}


/** 
 *  * @brief 在pos之前插入元素 
 *  * 
 *   * @param head 
 *    * @param pos 
 *     * @param value 
 *      */ 
void LinkListInsert(LinkNode** head, LinkNode* pos, LinkType value)
{
          
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }

     LinkNode *cur= NULL;
     LinkNode *pre= NULL;
     
     for(cur=*head;cur!=NULL;cur=cur->next)
    {
        if(cur==pos)
        { 
           if(pre!=NULL)
           {
            LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
            p->data=value;
            pre->next=p;
            p->next=cur;
            break;
           }
           else
           {
             LinkListPushFront(head,value);
             break;
           }
        }
          pre=cur;
    }
 
     if(cur==NULL)
    {
      printf("The pos is not in the list!");
      exit(0);
    }
 

} 

/** 
 *  * @brief 在pos之后插入元素 
 *  * 
 *   * @param head 
 *    * @param pos 
 *     * @param value 
 *      */ 
void LinkListInsertAfter(LinkNode** head, LinkNode* pos, LinkType value)
{

    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }
    LinkNode * cur=NULL;
    for(cur=*head;cur!=NULL;cur=cur->next)
   {
       if(cur==pos)
       {
          LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
          p->data=value;
          p->next=cur->next;
          cur->next=p;
          break;
        }
    }
     if(cur==NULL)
    {
      printf("The pos is not in the headlist!");
      exit(0);
    }

} 


/** 
 *  * @brief 删除指定位置的元素 
 *  */ 
void LinkListErase(LinkNode** head, LinkNode* pos)
{
    
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }

    LinkNode*cur=NULL;
    LinkNode*pre=NULL;
    for(cur=*head;cur!=NULL;cur=cur->next)
    {
        if(cur==pos)
        {
            if(pre!=NULL)
             {
               pre->next=cur->next;
               free(cur);
               
               break;
             }
             else
             {
               break;  
             }

        }
         pre=cur;
    }

    if(cur==NULL)
     {
       printf("The pos is not in the headlist!");
       exit(0);
     }
    
} 

// I don't quite understand what it mean. 
// void LinkListErase2(LinkNode** head, LinkNode** pos); 

/** 
 *  * @brief 删除指定值的元素 
 *  * 
 *   * @param head 
 *    * @param to_delete 
 *     */ 
void LinkListRemove(LinkNode** head, LinkType to_delete) 
{
  
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }

   LinkNode *cur=NULL;
   LinkNode *pre=NULL;
   
  for(cur=*head;cur!=NULL;cur=cur->next)
  { 
     if(cur->data==to_delete)
     {
        if(pre!=NULL)
        {
         pre->next=cur->next;
         free(cur);
         break;
        }
        else
        {
          free(*head);
          *head=NULL;
          break;
        }
     }
     pre=cur;
     cur=cur->next;
   }




}



/** 
 *  * @brief 指定值的所有元素都删掉. 
 *   * 
 *    * @param head 
 *     * @param value 
 *      */ 
void LinkListRemoveAll(LinkNode** head, LinkType value)
{
    
    if(head==NULL)
     {
       printf("There is no head pointer!");
       exit(0);
     }
    
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }

   LinkNode *cur=NULL;
   LinkNode *pre=NULL;
   LinkNode *to_delete=NULL;  
  for(cur=*head;cur!=NULL;cur=cur->next)
  { 
     if(cur->data==value)
     {
        if(pre!=NULL)
        {
         pre->next=cur->next;
         to_delete=cur;
         cur=pre;
         free(to_delete);
         to_delete=NULL;
         
        }
        else
        {
          free(*head);
          *head=NULL;
          break;
        }
     }
     pre=cur;

   }




}

/** 
 *  * @brief 判定链表为空 
 *  * 
 *   * @return 链表为空, 返回1, 否则返回0 
 *    */ 
int LinkListEmpty(LinkNode* head)
{
   if(head==NULL)
   {
     return 1;
   }
   else
   {
     return 0;
   }
} 

/** 
 *  * @brief 求链表的元素个数 
 *  * 
 *   * @param head 
 *    * 
 *     * @return 
 *      */ 
size_t LinkListSize(LinkNode* head)
{
   size_t count=0;
   while(head!=NULL)
   {
     count++;
     head=head->next;
   }
  return count;

} 

/** 
 *  * @brief 逆序打印单链表. 
 *   * 
 *    * @param head 
 *     */ 
void LinkListReversePrint(LinkNode* head)
{
  if(head==NULL)
   {
    printf("The linklist is NULL!");
    exit(0);
   }
    if(head->next!=NULL)
   {
     LinkListReversePrint(head->next);
   } 
    printf("%c->",head->data);
  

}

/** 
 *  * @brief 不允许遍历链表, 在 pos之前插入 
 *  * 
 *   * @param head 
 *    * @param pos 
 *     * @param value 
 *      */ 
void LinkListInsertBefore(LinkNode** head, LinkNode* pos, LinkType value)
{

    if(head==NULL)
     {
       return ;
     }
    if(*head==NULL)
    {
      printf("The head is NULL!");
      exit(0);
    }
     
   LinkNode* p=(LinkNode*)malloc(sizeof(LinkNode));
    p->data=pos->data;
    pos->data=value;
    p->next=pos->next;
    pos->next=p;


}

 LinkNode* JosephCycle(LinkNode* head, size_t food)
{
   if(head==NULL)
     {
        printf("The head linklist is NULL!");
        return;
     }
    LinkNode*cur=head;
    LinkNode*to_delete=NULL;
    size_t i;
    while(cur->next!=cur)
    {
       for(i=1;i<food;i++)
      {
        cur=cur->next;
      }
        cur->data=cur->next->data;
        to_delete=cur->next;
        cur->next=to_delete->next;
        free(to_delete);
        to_delete=NULL;
    }
      
    return cur;
}

 int main()
{

    LinkNode*p;
    LinkNode*tmp;
    LinkListInit(&p); 
    LinkListPushBack(&p,'b');
    LinkListPushFront(&p,'a');
    LinkListPushBack(&p,'c');
    LinkListPushFront(&p,'x');
    LinkListPopBack(&p);
    LinkListPopFront(&p);
    LinkListInsert(&p, p->next,'c');
    tmp=LinkListFind(p, 'a'); 
    printf(" %c ",tmp->data);

    LinkListErase(&p,p->next );
    LinkListInsertAfter(&p, p->next, 'z');

    LinkListRemove(&p, 'z');

    LinkListPushFront(&p,'a');
    LinkListPushFront(&p,'a');
    LinkListPushFront(&p,'b');
    LinkListPushFront(&p,'b');
    LinkListPushFront(&p,'b');
    LinkListPushFront(&p,'b');
    int i=LinkListEmpty(p);
    if(i!=1)
    {
     printf("The list is not empty!\n");
    }
    LinkListRemoveAll(&p, 'a');
    i=LinkListSize(p);
    printf("There is %d node in the linkelist.\n",i);
    LinkListReversePrint(p);
    LinkListInsertBefore(&p, p->next,'a');
  
    LinkListPushFront(&p,'a');
    LinkListPushFront(&p,'a');
    printf("\n");
    LinkListReversePrint(p);
    LinkNode *end=p;
    while(end->next!=NULL)
    {
       end=end->next;
    }
     end->next=p;
     
    printf("\n");
  LinkNode*cur= JosephCycle(p, 2);
printf("The serviver is %c \n.",cur->data);

} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值