链表的基础
//#include<stdio.h>
//#include<stdlib.h>
//
//struct node
//{
// int data;
// struct node *next;
//};
//
//struct node *creat1(int n);//逆序建立链表o
//void display(struct node *head);
//
//int main()
//{
// int n;
// struct node *head;
// scanf("%d",&n);
// head=creat1(n);
// display(head);
// return 0;
//}
//struct node *creat1(int n)
//{
// struct node *head,*p;
// int i;
// head=(struct node*)malloc(sizeof(struct node));
// head->next=NULL;
// for(i=1;i<=n;i++)
// {
// p=(struct node*)malloc(sizeof(struct node));
// scanf("%d",&p->data);
// p->next=head->next;
// head->next=p;
// }
// return head;
//}
//
//void display(struct node *head)
//{
// struct node *p;
// p=head->next;
// while(p!=NULL)
// {
// if(p->next==NULL)
// printf("%d\n",p->data);
// else
// printf("%d ",p->data);
// p=p->next;
// }
//}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat2(int n);
void display(struct node *head);
int main()
{
int n;
struct node *head;
scanf("%d",&n);
head=creat2(n);
display(head);
return 0;
}
struct node *creat2(int n) //顺序建立链表
{
struct node *head,*p,*tail;
int i;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=1; i<=n; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
void display(struct node *head)//链表输出
{
struct node *p;
p=head->next;
while(p!=NULL)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
}
struct node *Search(struct node *head,int key)//结点的查找
{
struct node *p;
p=head->next;
while(p!=NULL)
{
if(p->data==key)
return p;
else
p=p->next;
}
return NULL;
}
//链表结点的插入
//(1)开辟新结点
//struct node *q;
//q=(struct node*)malloc(sizeof(struct node));
//q->data=10;
//q->next=NULL;
//
(2)修改指针域
//q->next=p->next;
//p->next=q;
void Insert(struct node *p,int key)//链表结点的而插入
{
struct node *q;
q=(struct node*)malloc(sizeof(struct node));
if(!q)
exit(1);
q->data=key;
q->next=NULL;
q->next=p->next;
p->next=q;
}
int del(struct node *head,int key)//链表结点的删除
{
struct node *p,*q;
int flag=0;
p=head;
while(p->next!=NULL)
{
if(p->next->data==key)
{
flag=1;
break;
}
else
p=p->next;
}
if(flag==1)
{
q=p->next;
p->next=q->next;
free(q);
return 1;
}
else
return 0;
}
void Reverse(struct node *head)//逆置单链表
{
struct node *p,*q;
p=head->next;
head->next=NULL;
q=p->next;
while(p!=NULL)
{
p->next=head->next;
head->next=p;
p=q;
if(q!=NULL)
q=q->next;
}
}
//链表的归并
struct node *Merge(struct node *head1,struct node *head2)
{
struct node *p1,*p2,*tail;
p1=head1->next;
p2=head2->next;
head1->next=NULL;
tail=head1;
free(head2);
while(p1&&p2)
{
if(p1->data<p2->data)
{
tail->next=p1;
tail=p1;
p1=p1->next;
tail->next=NULL;
}
else
{
tail->next=p2;
tail=p2;
p2=p2->next;
tail->next=NULL;
}
}
if(p1)
tail->next=p1;
else
tail->next=p2;
return head1;
}
//单链表的拆分
struct node *Split(struct node *head1)
{
struct node *head2,*p,*q;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
p=head1->next;
head1->next=NULL;
q=p->next;
while(p!=NULL)
{
if(p->data>=0)
{
p->next=head1->next;
head1->next=p;
}
else
{
p->next=head2->next;
head2->next=p;
}
p=q;
if(q!=NULL)
q=q->next;
}
return head2;
}
//约瑟夫环问题
#include <stdio.h>
#include <malloc.h>
typedef struct mon
{
int num;
struct mon *next;
} MONKEY;
// 循环链表的创建函数
MONKEY *creat(int n)
{
int i;
MONKEY *p,*tail,*head;
// 第一个结点的生成
head=(MONKEY *)
malloc(sizeof(MONKEY));
head->num=1;
head->next=NULL;
tail=p;
// 顺序建链表,完成其余n-2个结点的插入
for(i=2; i<=n; i++)
{
p=(MONKEY *)
malloc(sizeof(MONKEY));
p->num=i;
p->next=NULL;
tail->next=p;
tail=p;
}
// 最后一个结点的指针域指向第一个结点
tail->next=head; //形成循环链表
return head;
}
int sel(MONKEY *head, int n, int m)
// n只猴子报数m出圈的控制函数
{
int num=0;
// 表示猴子报数的计数变量
int count=0;
// 用以统计出圈猴子数目的计数变量
MONKEY *p,*q;
// 分别指向当前结点及其前驱结点的指针
q=head; //设置跟随指针
while(q->next!=head)
q=q->next; // 跟随指针p指向尾结点
printf("被删除的猴子序号依次是:");
while(count<n-1) //还有可以报数结点 q->next!=q
{
p=q->next; //设置当前指针
num++; //报数
if(num%m==0) // 找到一个被删结点
{
q->next=p->next; //完成删除
printf("%3d",p->num); //完成输出
free(p); //释放删除结点存储空间
count++; //统计出圈个数
}
else
{
q=p; //未找到被删结点,q移向下一个结点
}
}
return q->num; //最后一个结点的数据域
//即为留在圈内猴王的序号
}
int main()
{
int n,m;
MONKEY *head;
scanf("%d%d",&n,&m);
head=creat(n);
printf("\n猴王是%d\n",sel(head,n,m));
return 0;
}