#include <stdio.h>
#include <windows.h>
#include <string.h>
struct link * creat1(int n);
void out(struct link * head);
void free_(struct link * head);
struct link * jsf(struct link * head,int m);
struct link
{
int data;
struct link * next;
};
void main()
{
struct link * head;
struct link * p;
int n,m;
printf("input n , m:"); //读入个数与约定删除数
scanf("%d %d",&n,&m);
head = creat1(n);
out(head);
p = jsf(head,m);
printf("\nlast: %d \n",p->data);
free(p);
}
struct link * jsf(struct link * head,int m)
{
int i;
struct link * p = malloc(sizeof(struct link )); //给无头结点链表添加辅助头结点,但循环链表的尾巴仍指向第一个元素点,而不是头结点
struct link * q;
p->next = head;
head = p;
while(p->next != p) //只剩下一个节点时,即为结果
{
for(i = 0; i < m; i++) //数到M删除一次
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
p = q;
}
free(head);//删除头结点
return p;
}
struct link * creat1(int n) //创建循环链表
{
struct link * head;
struct link * p;
struct link * q;
int i = 1;
head = (struct link *)malloc(sizeof(struct link));
p = q = (struct link *)malloc(sizeof(struct link));
p->data = i;
head->next=p;
while (i < n)
{
i++;
p = (struct link *)malloc(sizeof(struct link));
p->data = i;
q->next = p;
q = p;
}
p = head->next; //删除头结点。使之为无头结点链表,多此一举,懒得改
q->next = p;
free(head);
return p;
}
void out(struct link * head) //输出无头结点链表
{
struct link * p = head->next;
printf("%d ",head->data);
while(p != head)
{
printf("%d ",p->data);
p = p->next;
}
putchar(10);
}
void free_(struct link * head) //删除无头结点链表,未使用
{
struct link * p = head->next;
struct link * q = head;
while(head != p)
{
q = p->next;
free(p);
p = q;
}
free(head);
}
#include <windows.h>
#include <string.h>
struct link * creat1(int n);
void out(struct link * head);
void free_(struct link * head);
struct link * jsf(struct link * head,int m);
struct link
{
int data;
struct link * next;
};
void main()
{
struct link * head;
struct link * p;
int n,m;
printf("input n , m:"); //读入个数与约定删除数
scanf("%d %d",&n,&m);
head = creat1(n);
out(head);
p = jsf(head,m);
printf("\nlast: %d \n",p->data);
free(p);
}
struct link * jsf(struct link * head,int m)
{
int i;
struct link * p = malloc(sizeof(struct link )); //给无头结点链表添加辅助头结点,但循环链表的尾巴仍指向第一个元素点,而不是头结点
struct link * q;
p->next = head;
head = p;
while(p->next != p) //只剩下一个节点时,即为结果
{
for(i = 0; i < m; i++) //数到M删除一次
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
p = q;
}
free(head);//删除头结点
return p;
}
struct link * creat1(int n) //创建循环链表
{
struct link * head;
struct link * p;
struct link * q;
int i = 1;
head = (struct link *)malloc(sizeof(struct link));
p = q = (struct link *)malloc(sizeof(struct link));
p->data = i;
head->next=p;
while (i < n)
{
i++;
p = (struct link *)malloc(sizeof(struct link));
p->data = i;
q->next = p;
q = p;
}
p = head->next; //删除头结点。使之为无头结点链表,多此一举,懒得改
q->next = p;
free(head);
return p;
}
void out(struct link * head) //输出无头结点链表
{
struct link * p = head->next;
printf("%d ",head->data);
while(p != head)
{
printf("%d ",p->data);
p = p->next;
}
putchar(10);
}
void free_(struct link * head) //删除无头结点链表,未使用
{
struct link * p = head->next;
struct link * q = head;
while(head != p)
{
q = p->next;
free(p);
p = q;
}
free(head);
}