单向链表的翻转和快慢指针
void reversalFront_list(list* head)
{
if(NULL == head->next || NULL == head)
{
printf("链表为空或不存在\n");
return;
}
list* p = head->next;
head->next = NULL;
while(p != NULL)
{
list* q=p->next;
p->next = head->next;
head->next = p;
p=q;
}
return;
}
datatype middleNode_list(list* head)
{
if(NULL == head->next || NULL == head)
{
printf("链表为空或不存在\n");
return (datatype)-1;
}
list* slow, *fast;
slow = fast = head->next;
while(fast != NULL&&fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
return slow->db.data;
}
约瑟夫问题(不全):
void joseph_list(Joseph** pjhead,int N,int M)
{
Joseph* p=NULL,*q=NULL;
*pjhead = (Joseph*)malloc(sizeof(Joseph));
if(NULL == *pjhead)
{
printf("单向循环链表创建失败\n");
return;
}
(*pjhead)->data = 1;
(*pjhead)->next = NULL;
p = *pjhead;
for(int i=2;i<=N;i++)
{
Joseph* q = (Joseph*)malloc(sizeof(Joseph));
q->data = i;
q->next = NULL;
p->next = q;
p=q;
}
p->next = *pjhead;
p=*pjhead;
while(p->next !=p)
{
for(int i=1;i<=M;i++)
{
q = p;
p=p->next;
}
printf("%d ",p->data);
q->next = p->next;
p = p->next;
}
printf("\n");
}