1,单链表的增删改查以及通过单链表实现jsoehp(约瑟夫)问题
头文件.h
#ifndef _LK_LKLIST__
#define _LK_LKLIST__
typedef int datatype;
typedef struct s
{
union{
int len;
datatype data;
}msg;
struct s* next;
}loophead;
void ceatn_list(loophead **phead);
void inser_headlist(loophead **head,datatype a);
void show_looplist(loophead *phead);
void rm_looplist(loophead **phead);
void inser_taillist(loophead **phead,datatype a);
void rm_taillist(loophead **phead);
void inser_looplist(loophead *head,int index,datatype a);
void rm_wiezhilooplist(loophead *head,int index);
void joseph_list(loophead *head,int k,int m);
#endif
实现增删改查的函数以及实现jsoehp函数
#include <stdio.h>
#include <stdlib.h>
#include "./loop_list.h"
/*
* function: 创建单循环链表
* @param [ in]
* @param [out]
* @return
*/
void ceatn_list(loophead **phead)
{
*phead=(loophead*)malloc(sizeof(loophead));
if(*phead==NULL)
{
printf("创建头结点失败\n");
return;
}
(*phead)->next=*phead;
(*phead)->msg.len=0;
return;
}
/*
* function: 头插法
* @param [ in]
* @param [out]
* @return
*/
void inser_headlist(loophead **phead,datatype a)
{
//创建新的结点
loophead *temp=(loophead*)malloc(sizeof(loophead));
if(temp==NULL)
{
printf("创建头结点失败\n");
return;
}
temp->next=NULL;
temp->msg.data=a;
//插入
temp->next=(*phead)->next;
(*phead)->next=temp;
(*phead)->msg.len++;
return;
}
/*
* function: 头删法
* @param [ in]
* @param [out]
* @return
*/
void rm_looplist(loophead **phead)
{
if((*phead)->next==NULL)
{
printf("循环单链表为空\n");
return;
}
loophead *temp=(*phead)->next;
(*phead)->next=(*phead)->next->next;
free(temp);
(*phead)->msg.len--;
return;
}
/*
* function: 尾插入
* @param [ in]
* @param [out]
* @return
*/
void inser_taillist(loophead **phead,datatype a)
{
//创建新的结点
loophead *temp=(loophead*)malloc(sizeof(loophead));
if(temp==NULL)
{
printf("创建头结点失败\n");
return;
}
temp->next=NULL;
temp->msg.data=a;
loophead *p=(*phead);
while(p->next!=(*phead))
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
(*phead)->msg.len++;
return;
}
/*
* function: 尾删除
* @param [ in]
* @param [out]
* @return
*/
void rm_taillist(loophead **phead)
{
if((*phead)->next==NULL)
{
printf("循环单链表为空\n");
return;
}
loophead *p=(*phead);
while(p->next->next!=(*phead))
{
p=p->next;
}
loophead *temp=p->next;
p->next=(*phead);
(*phead)->msg.len--;
return;
}
/*
* function: 循环单链表的遍历
* @param [ in]
* @param [out]
* @return
*/
void show_looplist(loophead *head)
{
loophead *p=head;
while(p->next!=head)
{
p=p->next;
printf("%-3d",p->msg.data);
}putchar(10);
}
/*
* function: 按位置插入
* @param [ in]
* @param [out]
* @return
*/
void inser_looplist(loophead *head,int index,datatype a)
{
//创建新的结点
loophead *temp=(loophead*)malloc(sizeof(loophead));
if(temp==NULL)
{
printf("创建头结点失败\n");
return;
}
temp->next=NULL;
temp->msg.data=a;
loophead *p=head;
for(int i=0;i<index;i++)
{
p=p->next;
}
temp->next=p->next;
p->next=temp;
head->msg.len++;
return;
}
/*
* function: 按位置删除
* @param [ in]
* @param [out]
* @return
*/
void rm_wiezhilooplist(loophead *head,int index)
{
if(head->next==NULL)
{
printf("循环单链表为空\n");
return;
}
if(index<0||index>head->msg.len)
{
printf("删除位置非法\n");
return;
}
loophead *p=head;
for(int i=0;i<index-1;i++)
{
p=p->next;
}
//备份
loophead *temp=p->next;
//删除
p->next=temp->next;
free(temp);
head->msg.len--;
return;
}
void joseph_list(loophead *head,int k,int m)
{
//备份,删除头结点(存储数据无效)
loophead *p=head;
while(p->next!=head)
{
p=p->next;
}
p->next=head->next;
p=head;
head=head->next;
free(p);
//找到第一个为k---3的位置,编号为1
p=head;//第一个开始
for(int i=0;i<k-1;i++)
{
p=p->next;
}
//当只有一个结点时满足
while(p->next!=p)
{
//删除结点
for(int j=0;j<m-2;j++)
{
p=p->next;
}
//备份
loophead *temp=p->next;
p->next=p->next->next;
//打印出删除的数据
printf("%-3d",temp->msg.data);
free(temp);
p=p->next;
}
printf("%-3d",p->msg.data);
free(p);
return;
}
main函数
#include <stdio.h>
#include "./loop_list.h"
int main(int argc, const char *argv[])
{
loophead * phead=NULL;
ceatn_list(&phead);
inser_headlist(&phead,5);
inser_headlist(&phead,1);
inser_headlist(&phead,30);
show_looplist(phead);
rm_looplist(&phead);
show_looplist(phead);
inser_taillist(&phead,8);
inser_taillist(&phead,9);
inser_taillist(&phead,60);
show_looplist(phead);
rm_taillist(&phead);
show_looplist(phead);
inser_looplist(phead,1,3);
inser_looplist(phead,3,6);
inser_looplist(phead,6,10);
show_looplist(phead);
inser_looplist(phead,1,2);
inser_looplist(phead,3,4);
inser_looplist(phead,6,7);
show_looplist(phead);
/*
rm_wiezhilooplist(phead,0);
rm_wiezhilooplist(phead,3);
rm_wiezhilooplist(phead,4);
show_looplist(phead);
*/
joseph_list(phead,3,4);
return 0;
}
运行结果
2,直接插入排序,链表翻转,快慢指针
头文件.h
#ifndef _LK_LKLIST__
#define _LK_LKLIST__
typedef int datatype;
typedef struct s
{
union{
int len;
datatype mun;
}msg;
struct s* next1;
}datahead;
void inset_lklist(datahead *head,datatype c);
void flip_lklist(datahead *head);
datatype middnode_lklist(datahead *head);
#endif
#include <stdio.h>
#include "./link_list.h"
#include <stdlib.h>
/*
* function: 创建链表
* @param [ in]
* @param [out]
* @return
*/
datahead* cetan_lklist()
{
datahead *head= (datahead*)malloc(sizeof(datahead));
if(head==NULL)
{
printf("头结点创建失败\n");
return (datahead*)-1;
}
head->msg.len=0;
head->next1=NULL;
return head;
}
/*
* function: 直接插入排序
* @param [ in]
* @param [out]
* @return
*/
void inset_lklist(datahead *head,datatype c)
{
//创建新的结点
datahead *temp=(datahead*)malloc(sizeof(datahead));
if(temp==NULL)
{
printf("头结点创建失败\n");
return;
}
temp->msg.mun=c;
temp->next1=NULL;
datahead *p=head;
while(p->next1!=NULL&&p->next1->msg.mun< c)
{
p=p->next1;
}
temp->next1=p->next1;
p->next1=temp;
head->msg.len++;
return;
}
/*
* function: 链表翻转flip
* @param [ in]
* @param [out]
* @return
*/
void flip_lklist(datahead *head)
{
//判空
if(head->next1==NULL||head==NULL)
{
printf("链表为空,头结点为空\n");
return;
}
//备份头结点
datahead *p=head->next1,*q;
//独立出头指针
head->next1=NULL;
while(p!=NULL)
{
//备份下一个结点
q=p->next1;
//插入数据
p->next1=head->next1;
head->next1=p;
//将p移动至下一个结点
p=q;
}
return;
}
/*
* function: 快慢指针
* @param [ in]
* @param [out]
* @return
*/
datatype middnode_lklist(datahead *head)
{
//判空
if(head->next1==NULL||head==NULL)
{
printf("链表为空,头结点为空\n");
return (datatype)-1;
}
//slow指针想后移动一步,fast指针向后移动两步
datahead *slow,*fast;
slow=head->next1;
fast=head->next1->next1;
while(fast!=NULL&&fast->next1!=NULL)
{
slow=slow->next1;
fast=fast->next1->next1;
}
return slow->msg.mun;
}
/*
* function: //遍历链表
* @param [ in]
* @param [out]
* @return
*/
void shuchu_lklist(datahead *head)
{
while(head->next1!=NULL)
{
head=head->next1;
printf("%-3d",head->msg.mun);
}putchar(10);
return;
}
运行结果