数据结构作业第三天

joseph(约瑟夫)问题 约瑟夫(约瑟夫)问题)
1.设编号分别为:1,2, ...,n的n个人围坐一圈。
2.约定序号为k (1 3.他的下一位又从1开始计数,数到m的那个人又出列,依次类推,直到所有人出列为止。
例如,8个人围坐一圈,约定从第3个人开始编号为1,数到第4个人出列。出列后原来第5个人计算为n=8,k=3,m=4,最终出队的结果是62743518
提示:单向循环链表,删除头结点 

功能函数

#include "./02_func.h"                       
                                             
                                             
                                             
Rod *  crealink()                            
{                                            
    Rod * head = (Rod*)malloc(sizeof(Rod));  
    if(head == NULL)                         
    {                                        
        printf("创建失败\n");                
        return NULL;                         
    }                                        
    head->next = head;                       
    head->txt.len = 0;                       
    return head;                             
}                                            
                                             
//插入                                       
int inslink(Rod *head, Typedate demo)        
{                                            
    Rod * temp = (Rod*)malloc(sizeof(Rod));  
    temp->next = NULL;                       
    temp->txt.data = demo;                   
    Rod* p = head;=============              
    while(p->next != head)                   
    {                                        
        p = p->next;                         
    }                                        
    p->next = temp;                          
    temp->next = head;                       
    head->txt.len++;                         
    return 0;                                
                                             
}                                            
                                             
//遍历                                       
                                             
int showlink(Rod *head)                      
{                                            
    Rod * p = head;                          
    while(p->next != head)                   
    {                                        
        p = p->next;                         
        printf("%d\n", p->txt.data);         
    }                                        
    return 0;                                
}                                            
//查找                                       
Rod * looklink(Rod * head, int index)        
{                                            
    Rod * p = head;                          
    for(int i = 1; i < index; i++)           
    {                                        
        p = p->next;                         
    }                                        
    return p;                                
                                             
}                                            
int rm_link(Rod *head)                       
{                                            
    Rod * p = head->next;                    
    Rod * old = head;                        
    Rod * q = NULL;//前一个点;               
    Typedate temo;                           
    while(old->next != head)                 
     {                                       
        old=old->next;                       
    }                                        
    old->next = head->next;                  
    head->next = NULL; // 去除头结点         
    Rod * link = p;  //起始点                
    for(int i = 1; i < 3; i++)               
    {                                        
        link = link->next;                   
    }                                        
    Rod * over;                              
    for(int i = 1; i < 7; i++)               
    {                                        
        q = looklink(link, 4);               
        temo = q->next->txt.data;            
        inslink(head, temo);                 
        over = q->next;                      
        q->next = q->next->next;             
        free(over);                          
    }                                        
    return 0;                                
}

主函数

#include "./02_func.h"

int main(int argc, const char *argv[])
{
    Rod* head = crealink();
    printf("插入\n");
    inslink(head,1);
    inslink(head,2);
    inslink(head,3);
    inslink(head,4);
    inslink(head,5);
    inslink(head,6);
    inslink(head,7);
    inslink(head,8);
    showlink(head);
    rm_link(head);
    return 0;
}

头文件

#ifndef __FUNC_H__
#define __FUNC_H__
#include 
#include 
#include 
typedef int Typedate;
typedef struct node
{
    union
    {
        int len;
        Typedate data;
    } txt;
    struct node * next;
} Rod;
Rod *  crealink();
int showlink(Rod *head);
//插入
int inslink(Rod *head, Typedate demo);
//遍历
int showlink(Rod *head);
//查找
Rod * looklink(Rod * head, int index);
//排列
int rm_link(Rod *head);



#endif

 双向链表

功能函数

 #include "./02-func.h"
 
 dlbnode* cleandoublelink()
 {
     dlbnode * head = (dlbnode*)malloc(sizeof(dlbnode));
     memset(head, 0, sizeof(dlbnode));
     head->txt.len = 0;
     head->next = NULL;
     head->perv = NULL;
     return head;
 }
 //头插
 int  inserdlblink(dlbnode *head , Typedata demo)
 {
     dlbnode * temp = (dlbnode*)malloc(sizeof(dlbnode));
     temp->next = NULL;
     temp->perv = NULL;
     temp->txt.data = demo;
 
     if(head->next== NULL)
     {
         temp->next = head->next;
         head->next = temp;
         temp->perv = head;
     }
     else
     {
                                                                          
         temp->next = head->next;
     head->next = temp;
 
     temp->next->perv = temp;
     temp->perv = head;
     }
     head->txt.len++;
     return 0;
 }
 
 //遍历 
 int showlink(dlbnode * head)
 {
     dlbnode *p = head;
     if(head->next == NULL || head == NULL)
     {
         printf("链表无数据或者链表为空");
         return -1;
     }
     while(p->next != NULL)
     {
         p = p->next;
         printf("%d\n", p->txt.data);
     }
     return 0;
 }
 
 //尾插
 int headdlblink(dlbnode *head, Typedata demo)
 {
     dlbnode * temp = (dlbnode*)malloc(sizeof(dlbnode));
     temp->next = NULL;
     temp->perv = NULL;
     temp->txt.data = demo;
     dlbnode *p = head;
     while(p->next != NULL)
     {
         p = p->next;//找到尾节点
     }
     temp->next = p->next;
     p->next = temp;
     temp->perv = p;
     head->txt.len++;
 }
 
 //中间插入
 
 int  meddleinster_dlblink(dlbnode * head, Typedata demo, int index)
 {
     dlbnode * temp = (dlbnode*)malloc(sizeof(dlbnode));
     temp->next = NULL;
     temp->perv = NULL;
     temp->txt.data = demo;
     dlbnode * p = head;
     for( int i = 0 ; i < index-1; i++)
     {
         p = p->next;
     }
     if(p->next == NULL)
     {
       temp->next = p->next;
       p->next = temp;
       temp->perv = p;
     }
     else
     {
     temp->next = p->next;
     p->next = temp;
 
     temp->next->perv = temp;
     temp->perv = p;
 
     }
     head->txt.len++;
     return 0;
 
 }
 
 //头删除
 
 int rm_headlink(dlbnode* head)
 {
 
     if(head->next == NULL)
     {
         printf("链表为空\n");
         return -1;
     }
     dlbnode * p = head->next;
     if( p->next == NULL)
     {
         free(p);
         head->next = NULL;
     }
    else
    {
     head->next = head->next->next;
     head->next->next->perv = head;
     free(p);
    }
     head->txt.len--;
     return 0;
 }
 
 //尾删除
 
 int rm_rearlink(dlbnode * head)
 {
     if(head->next == NULL)
     {
         printf("链表为空\n");
         return -1;
     }
     dlbnode * p = head;
     while(p->next->next != NULL)
     {
         p = p->next;
     }
     free(p->next);
     p->next = NULL;
     head->txt.len--;
     return 0;
 }
 
 //中间删除
 
 int rm_meddlelink(dlbnode * head, int index)
 {
     int i = 0;
     dlbnode * p = head;
     if(index > head->txt.len || index <= 0)
     {
         printf("下表错误删除失败\n");
         return -1;
     }
     if(head->next == NULL)
     {
         printf("列表为空\n");
         return -1
     }
     for(i; i < index; i++)
     {
         p = p->next;
     }
     if(p->next = NULL)
     {
         p->perv->next = NULL;
     }
       else
       {
           p->perv->next = p->next;
           p->next->perv = p->perv;
       }
       free(p);
       head->txt.len--;
 
     return 0;
 }
 
 

头文件

#ifndef __FUNC_C__
#define __FUNC_C__

#include 
#include                                                  
#include 
typedef int Typedata;
typedef struct node
{
    union
    {
        int len;
        Typedata data;
    } txt;
    struct node * next;
    struct node * perv;
} dlbnode;
dlbnode* cleandoublelink();
//头插
int  inserdlblink(dlbnode *head , Typedata demo);
//遍历
int showlink(dlbnode * head);
//尾插
int headdlblink(dlbnode *head, Typedata demo);

//中间插入
int  meddleinster_dlblink(dlbnode * head, Typedata demo, int index);
//头删
int rm_headlink(dlbnode* head);
//尾删除
int rm_rearlink(dlbnode * head);
//从中间删除
int rm_meddlelink(dlbnode * head, int index);

#endif

主函数

 #include "./02-func.h"
 
 int main(int argc, const char *argv[])
 {
     dlbnode * head = cleandoublelink();
     printf("头插\n");
     inserdlblink(head, 100);
     inserdlblink(head, 120);
     inserdlblink(head, 111);
     inserdlblink(head, 130);
     showlink(head);
     printf("尾插\n");
     headdlblink(head, 100);
     headdlblink(head, 120);
     headdlblink(head, 111);
     headdlblink(head, 130);
     showlink(head);
    printf("中间插入\n");
    meddleinster_dlblink(head, 888, 2);
    showlink(head);
    printf("头删\n");
    rm_headlink(head);
    showlink(head);
    printf("尾删\n");
    rm_rearlink(head);
    showlink(head);
    printf("从中间删除\n");
    rm_meddlelink(head,3);
    showlink(head);
     return 0;
 }                                          

循环单线链表

功能函数

#include "./01-func.h"

Loop * overturn_linklist()
{
    Loop * head = (Loop*)malloc(sizeof(Loop));
    if(NULL == head)
    {
        printf("链表为空\n");
        return NULL;
    }
    head->txt.len = 0;
    head->next=head;
   return head;
}

//尾插

int  insert_linkRear(Loop* head, Typedata demo)
{
    Loop* temp = (Loop *)malloc(sizeof(Loop));
    if(NULL== temp)
    {
        printf("创建节点失败\n");
        return -1;
    }
    temp->next = NULL;
    temp->txt.data = demo;
    Loop * p = head;                              
    while(p->next != head)
    {
        p = p->next;
    }
    temp->next = p->next;
    p->next = temp;
    head->txt.len++;
    return 0;

}

//遍历
int show_link(Loop * head)
{
    Loop * p = head;
    if(head == NULL || head->next==head)
    {
        printf("链表为空或者无数据\n");
        return -1;
    }
    while(p->next != head)
    {

        p = p->next;
        printf("%d\n", p->txt.data);
    }
  return 0;
}
//头删除
int rm_headlink(Loop *head)
{
    if(head == NULL || head->next==head)
    {
        printf("链表为空或者无数据\n");
        return -1;
    }
    Loop * p = head->next;
    head->next = head->next->next;
    free(p);
    head->txt.len--;
    return 0;
}
//尾删


int rm_deatlink(Loop *head)
{
    if(head == NULL || head->next==head)
    {
        printf("链表为空或者无数据\n");
        return -1;
    }
    Loop * p = head;
    while(p->next->next != head)
    {
        p = p->next;
    }
    free(p->next);
    p->next = head;
    head->txt.len--;
    return 0;
}

主函数

 #include "./01-func.h"
 
 int main(int argc, const char *argv[])
 {
     Loop * head = overturn_linklist(); 
     //尾插
     insert_linkRear(head, 10);
     insert_linkRear(head, 12);
     insert_linkRear(head, 15);
     insert_linkRear(head, 19);
     insert_linkRear(head, 28);
     show_link(head);
     printf("头删\n");
     rm_headlink(head);
     show_link(head);
     printf("尾删\n");
     rm_deatlink(head);
     show_link(head);
     return 0;
 }                                         

头文件

#ifndef __FUNC_H
#define __FUNC_H
#include 
#include 
typedef int Typedata;
typedef struct node
{
    union
    {
        int len;
        Typedata data;
    }  txt;
    struct node* next;
} Loop;
Loop * overturn_linklist();
//尾插
int  insert_linkRear(Loop* head, Typedata demo);
//遍历
int show_link(Loop * head);
//头删
int rm_headlink(Loop *head);
//尾删
int rm_deatlink(Loop *head);


#endif                                                  

双向循环链表

功能函数

#include "./01_func.h"


Ble * crean_doublelink()
{
     Ble * head = (Ble *)malloc(sizeof(Ble));
     memset(head, 0, sizeof(Ble));
     if(head == NULL)
     {
         printf("创建失败\n");
         return NULL;
     }
     head->next = head;
     head->perv = head;
     head->txt.len = 0;
     
     return head;
}                                              

头文件

 #ifndef __FUNC__H_           
 #define __FUNC__H_           
 #include            
 #include           
 #include           
 typedef int Typedata;        
 typedef struct node          
 {                            
     union                    
     {                        
         int len;             
         Typedata data;       
     }  txt;                  
     struct node * next;      
     struct node * perv;      
 } Ble;                       
 Ble * crean_doublelink();    
                              
 #endif                       

主函数

#include "./01_func.h"                      
                                            
int main(int argc, const char *argv[])      
{                                           
    Ble * head  =  crean_doublelink();      
    return 0;                               
}                                           

你可能感兴趣的:(数据结构)