数据结构3 单向循环链表+双向链表

一、双向链表

功能函数

#include "./03_doublelinklist.h"

//创建空的双向链表
doulist* creat_doublelinklist()
{
	doulist* head = (doulist*)malloc(sizeof(doulist));
	if( NULL == head)
	{
		printf("创建空的双向链表失败\n");
		return NULL;

	}
	memset(head,0,sizeof(doulist));
	head->next = NULL;
	head->pre = NULL;
	head->txt.len=0;
	return head;
}
//头插法
void insert_doublelinklistBYfront(doulist* head,dataType data)
{
	doulist* temp = (doulist*)malloc(sizeof(doulist));
	if(NULL == temp)
	{
		printf("新节点创建失败,插入失败\n");
		return;
	}
	temp->txt.data = data;
	temp->next = NULL;
	temp->pre = NULL;

	//特殊值,head后面没有数据
	if(NULL == head->next)
	{
		temp ->next = head ->next;
		head->next= temp;
		temp->pre=head;
	}else
	{
		temp->next = head->next;
		head->next = temp;
		temp->next->pre = temp;//head后面不是空的情况,head后面没有节点时
		temp->pre = head;
	}
	head->txt.len++;
	return;

}
//遍历
void bianli_doublelist(doulist* head)
{
	doulist* p = head;
	while(p->next != NULL)
	{
		p=p->next;
		printf("%d  ",p->txt.data);
	}
	printf("\n");
	return;
}
//尾插
void insert_doublelinklistBYrear(doulist* head,dataType data)
{
	doulist* temp = (doulist*)malloc(sizeof(doulist));
	if(NULL == temp)
	{
		printf("新节点创建失败,插入失败\n");
	}
	temp->txt.data=data;
	temp->next=NULL;
	temp->pre=NULL;


	doulist* p = head;
	while(p->next != NULL)
	{
		p=p->next;

	}
	temp->next = p->next;
	p->next=temp;
	temp->pre = p;
	head->txt.len++;
	return;

}
//中间插入
void insert_doublelinklistBYpos(doulist* head,dataType data,int pos)
{
	if(pos<1 || pos>head->txt.len)
	{
		printf("位置非法,插入失败\n");
		return;

	}
	doulist* temp = (doulist*)malloc(sizeof(doulist));
	if(NULL == temp)
	{
		printf("新节点创建失败,插入失败\n");
	}
	temp->txt.data=data;
	temp->next=NULL;
	temp->pre=NULL;
	doulist* p = head;
	for(int i = 0;i<pos-1;i++)
	{
		p=p->next;

	}
	if(NULL == p->next)
	{
		temp->next=p->next;
		p->next=temp;
		temp->pre=p;

	}else
	{
		p->next->pre=temp;
		temp->pre=p;

		temp->next=p->next;
		p->next=temp;
	}
	head->txt.len++;
	return;

}
//头删
dataType delete_BYfront(doulist* head)
{
	if(NULL == head->next)
	{
		printf("链表为空删除失败\n");
		return (dataType)-1;

	}
	doulist* temp =head->next;
	if(NULL ==temp->next)//链表中只有一个有效节点
	{
		head->next = NULL;


	}else{
		head->next=temp->next;
		temp->next->pre = head;

	}

	dataType data1 = temp->txt.data;
	free(temp);

	head->txt.len--;
	return data1;

}
//尾删
dataType delete_BYrear(doulist* head)
{
	doulist* p = head;
	while(p->next != NULL)
	{
		p=p->next;

	}
	p->pre->next = NULL;
	dataType data = p->txt.data;
	free(p);
	return data;
	head->txt.len--;

}
//中间删
dataType delete_doulinklistBYmiddle(doulist* head,int pos)
{
	if(pos<1 || pos>head->txt.len)
	{
		printf("位置非法,插入失败\n");
		return (dataType)-1;

	}

	doulist* p =head;
	for(int i=0;i<pos;i++)
	{
		p=p->next;

	}
	if(p->next == NULL)
	{
		p->pre->next=NULL;
	}
	else
	{
		p->pre->next=p->next;
		p->next->pre=p->pre;

	}
	dataType data = p->txt.data;
	free(p);
	return data;



}

主函数

 #include "./03_doublelinklist.h"                                 
 int main(int argc, const char *argv[])                           
 {                                                                
     doulist* head = creat_doublelinklist();                      
 //头插法                                                         
     printf("头插入11,12,13,14,15为:\n");                        
     insert_doublelinklistBYfront(head,11);                       
     insert_doublelinklistBYfront(head,12);                       
     insert_doublelinklistBYfront(head,13);                       
     insert_doublelinklistBYfront(head,14);                       
     insert_doublelinklistBYfront(head,15);                       
                                                                  
 //遍历                                                           
     bianli_doublelist(head);                                     
 //尾插                                                           
     insert_doublelinklistBYrear(head,100);                       
     bianli_doublelist(head);                                     
 //中间插入                                                       
     insert_doublelinklistBYpos(head,1000,2);                     
     bianli_doublelist(head);                                     
 //头删                                                           
     dataType data = delete_BYfront(head);                        
     bianli_doublelist(head);                                     
 //尾删                                                           
     dataType data1= delete_BYrear(head);                         
     bianli_doublelist(head);                                     
 //中间删                                                         
     dataType data2 = delete_doulinklistBYmiddle(head,3);         
     printf("第三位删除的数据为:%d\n",data2);                    
     bianli_doublelist(head);                                     
     return 0;                                                    
 }                                                                

头文件

#ifndef __DOUBLELINKLIST_H__                                          
#define __DOUBLELINKLIST_H__                                          
#include "stdio.h"                                                    
#include "stdlib.h"                                                   
#include "string.h"                                                   
                                                                      
                                                                      
typedef int dataType;                                                 
typedef struct double_link                                            
{                                                                     
    union                                                             
    {                                                                 
        dataType data;                                                
        int len;                                                      
    }txt;                                                             
    struct double_link* next;                                         
    struct double_link* pre;                                          
}doulist;                                                             
doulist* creat_doublelinklist();                                      
//头插法                                                              
void insert_doublelinklistBYfront(doulist* head,dataType data);       
//遍历                                                                
void bianli_doublelist(doulist* head);                                
//尾插                                                                
void insert_doublelinklistBYrear(doulist* head,dataType data);        
//中间插入                                                            
void insert_doublelinklistBYpos(doulist* head,dataType data,int pos); 
//头删                                                                
dataType delete_BYfront(doulist* head);                               
//尾删                                                                
dataType delete_BYrear(doulist* head);                                
//中间删                                                              
dataType delete_doulinklistBYmiddle(doulist* head,int pos);           
                                                                      
                                                                      

运行结果

二、单向循环链表

主函数

 #include "./13_looplist.h"                                 
 int main(int argc, const char *argv[])                     
 {                                                          
     //创建循环单链表                                       
     Loop* head = creant_looplist();                        
     //头插                                                 
     printf("头插10,20,30,40,50为:\n");                   
     insert_Byfront(head,10);                               
     insert_Byfront(head,20);                               
     insert_Byfront(head,30);                               
     insert_Byfront(head,40);                               
     insert_Byfront(head,50);                               
     //遍历                                                 
      bianli_looplist(head);                                
     //尾插                                                 
     printf("尾插11,12,13为:\n");                          
     insert_Bytail(head,11);                                
     insert_Bytail(head,12);                                
     insert_Bytail(head,13);                                
     bianli_looplist(head);                                 
     //头删                                                 
     printf("头删50后为:\n");                              
     dataType data = delete_looplistByfront(head);          
     bianli_looplist(head);                                 
     //尾删                                                 
     dataType data1 =  delete_looplistByrear(head);         
     printf("尾部删除数据为:%d\n",data1);                  
     bianli_looplist(head);                                 
     return 0;                                              
 }                                                          
                                                            

功能函数

#include "./13_looplist.h"
//创建空的循环单链表
Loop* creant_looplist()
{
	//创建一个头结点
	Loop* head = (Loop*)malloc(sizeof(Loop));
	if(NULL == head)
	{
		printf("创建节点失败\n");
		return NULL;
	}
	head->txt.len=0;
	head->next=head;
	return head;
}
//头插
void insert_Byfront(Loop* head,dataType data)
{
	Loop* temp = (Loop*)malloc(sizeof(Loop));
	if(NULL == temp)
	{
		printf("创建节点失败\n");
		return;
	}
	temp->txt.data = data;
	temp->next = NULL;


	temp->next=head->next;
	head->next=temp;
	head->txt.len++;
	return;


}
//遍历
void bianli_looplist(Loop* head)
{
	Loop* p =head;
	while(p->next != head)
	{
		p=p->next;
		printf("%d  ",p->txt.data);
	}
	putchar(10);
	return;
}
//尾插
void insert_Bytail(Loop* head,dataType data)
{
	Loop* temp = (Loop*)malloc(sizeof(Loop));
	if(NULL == temp)
	{
		printf("创建节点失败\n");
		return;
	}
	temp->txt.data = data;
	temp->next = NULL;
	
	Loop* p =head;
	while(p->next != head)
	{
		p=p->next;
	}
	temp->next=head;
	p->next=temp;
	head->txt.len++;
	return;

}
//头删
dataType delete_looplistByfront(Loop* head)
{
	Loop* temp = head->next;
	head->next=temp->next;
	head->txt.len--;
	dataType data = temp->txt.data;
	free(temp);
	temp = NULL;
	return data;

}
//尾删
dataType delete_looplistByrear(Loop* head)
{
	if(head == head->next)
	{
		printf("链表为空,删除失败\n");
		return(dataType)-1;
	}

	Loop* p = head;
	while(p->next->next != head)
	{
		p=p->next;
	}
	dataType data = p->next->txt.data;
	free(p->next);
	p->next=head;
	head->txt.len--;
	return data;


}

头文件

 #ifndef __LOOPLIST_H__
 #define __LOOPLIST_H__
 #include "stdio.h"
 #include "stdlib.h"
 typedef int dataType;
 typedef struct loop_node
 {
     union
     {
         dataType data;
         int len;
     } txt;
     struct loop_node* next;
 }Loop;
 //创建空的循环单链表
 Loop* creant_looplist();
 
 //头插
 void insert_Byfront(Loop* head,dataType data);
 //遍历
 void bianli_looplist(Loop* head);
 //尾插                                          
 void insert_Bytail(Loop* head,dataType data);
 //头删
 
 dataType delete_looplistByfront(Loop* head);
 //尾删
 dataType delete_looplistByrear(Loop* head);
 
 #endif
                                                 

运行结果

三、约瑟夫问题

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

 #include "./113_yuesefu_func.h"         
 int main(int argc, const char *argv[])  
 {                                       
                                         
     loop* head = creat_looplist();      
     insert_yueloop(head,1);             
     insert_yueloop(head,2);             
     insert_yueloop(head,3);             
     insert_yueloop(head,4);             
     insert_yueloop(head,5);             
     insert_yueloop(head,6);             
     insert_yueloop(head,7);             
     insert_yueloop(head,8);             
                                         
     bianli_loopyue(head);               
     delete_yueloop(head);               
     bianli_loopyue(head);               
     return 0;                           
 }                                       
                                         
#include "stdlib.h"
typedef int dataType;
typedef struct loop_yue
{
    union
    {
        dataType data;
        int len;
    }txt;
    struct loop_yue* next;

}loop;

                                                 
loop* creat_looplist();

void insert_yueloop(loop* head,dataType data);

void bianli_loopyue(loop* head);
void  delete_yueloop(loop* head);

#endif

 #include "./113_yuesefu_func.h"              
 //创建空的循环单链表                         
 loop* creat_looplist()                       
 {                                            
     loop* head = (loop*)malloc(sizeof(loop));
     if(NULL == head)                         
     {                                        
         printf("创建头结点失败\n");          
         return NULL;                         
     }                                        
     head->txt.len=0;                         
     head->next=head;                         
                                              
     return head;                             
 }                                            
 //插入数据                                   
 void insert_yueloop(loop* head,dataType data)
 {                                            
     loop* temp = (loop*)malloc(sizeof(loop));
     if(NULL == temp)                         
     {                                        
         printf("节点为空,插入失败\n");      
         return;                              
                                              
     }                                        
     temp->txt.data = data;                   
     temp->next = NULL;                       
                                              
     loop* p = head;                          
     while(p->next != head)                   
     {                                        
         p=p->next;                           
                                              
     }                                        
     temp->next=head;                         
     p->next=temp;                            
     head->txt.len++;                         
     return;                                  
                                              
                                              
                                              
                                              
 }                                            
 //遍历                                       
 void bianli_loopyue(loop* head)              
 {                                            
     loop* p = head;                          
     while(p->next != head)                   
     {                                        
         p=p->next;                           
         printf("%d  ",p->txt.data);          
                                              
     }                                        
     putchar(10);                             
     return;                                  
                                              
                                              
 }                                            
 //删除                                       
 void delete_yueloop(loop* head)              
 {                                            
     loop* move=head;                         
     loop* temp =NULL;                        
     while(move->next!=move)                  
     {                                        
                                              
         for(int i = 3;i<=head->txt.len;i++)  
         {                                    
             temp=move;                       
             move=move->next;                 
         }                                    
         temp->next=move->next;               
         move=move->next;                     
         printf("%d  ",move->txt.data);       
     }                                        
     return;                                  
                                              
                                              
 }                                            
                                              


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值