#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
int data;
struct node *next;
}Node ,*LinkList;
LinkList CreatList(LinkList L,int *num)//创建动态链表
{
LinkList p , q;
L = (LinkList)malloc(sizeof(Node));
L -> next = NULL;
q = L;
while(1)
{
p = (LinkList)malloc(sizeof(Node));
p -> next = NULL;
scanf("%d",&p -> data);
if(p -> data == 0)
break;
(*num) ++;
q -> next = p;
q = p;
}
q = L -> next;
while(q)
{
printf("%d ",q -> data);
if(q -> next == NULL)
p = q;
q = q -> next;
}
p -> next = L -> next;
return L;
}
LinkList yuesefu(LinkList L, int len)//利用delnum来确定点到次数是不是3的倍数(当然修改成其他的也行),输出该节点值并删除该节点。由于q指针依旧要后移,所以delnum要++以跟上下移的节奏;
{
LinkList p , q;
int delnum = 0, tot = 0;
q = L;
while(tot < len)
{
delnum ++;
if(delnum % 3 == 0 && delnum != 0)
{
tot ++;
p = q -> next;
printf("%d ",p -> data);
q ->next = p -> next;
free(p);
delnum ++;
}
q = q-> next;
}
return L;
}
int main()
{
LinkList L;
int n = 0;
L = NULL;
L = CreatList(L,&n);
printf("\nthe length of your list is: %d \n",n);
yuesefu(L,n);
system("pause");
return 0;
}
//链表解约瑟夫环,本次修改查出原因为删除当前节点时,delnum下移没有跟上导致每次输出后移++;
#include<stdlib.h>
#include<string.h>
typedef struct node{
int data;
struct node *next;
}Node ,*LinkList;
LinkList CreatList(LinkList L,int *num)//创建动态链表
{
LinkList p , q;
L = (LinkList)malloc(sizeof(Node));
L -> next = NULL;
q = L;
while(1)
{
p = (LinkList)malloc(sizeof(Node));
p -> next = NULL;
scanf("%d",&p -> data);
if(p -> data == 0)
break;
(*num) ++;
q -> next = p;
q = p;
}
q = L -> next;
while(q)
{
printf("%d ",q -> data);
if(q -> next == NULL)
p = q;
q = q -> next;
}
p -> next = L -> next;
return L;
}
LinkList yuesefu(LinkList L, int len)//利用delnum来确定点到次数是不是3的倍数(当然修改成其他的也行),输出该节点值并删除该节点。由于q指针依旧要后移,所以delnum要++以跟上下移的节奏;
{
LinkList p , q;
int delnum = 0, tot = 0;
q = L;
while(tot < len)
{
delnum ++;
if(delnum % 3 == 0 && delnum != 0)
{
tot ++;
p = q -> next;
printf("%d ",p -> data);
q ->next = p -> next;
free(p);
delnum ++;
}
q = q-> next;
}
return L;
}
int main()
{
LinkList L;
int n = 0;
L = NULL;
L = CreatList(L,&n);
printf("\nthe length of your list is: %d \n",n);
yuesefu(L,n);
system("pause");
return 0;
}
//链表解约瑟夫环,本次修改查出原因为删除当前节点时,delnum下移没有跟上导致每次输出后移++;