旋转链表C
我目前正在解决列表和函数的求和问题,我遇到了这个问题,即逆时针旋转一个链表k。 这是同样的代码旋转链表C,c,linked-list,C,Linked List,我目前正在解决列表和函数的求和问题,我遇到了这个问题,即逆时针旋转一个链表k。 这是同样的代码 void rotate_k(struct list *node,int k) { int count=0; struct list *knode,*ptr=node; while(ptr!=NULL && count < k) { ptr=ptr->next; count++; } knode=ptr;
void rotate_k(struct list *node,int k)
{
int count=0;
struct list *knode,*ptr=node;
while(ptr!=NULL && count < k)
{
ptr=ptr->next;
count++;
}
knode=ptr;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next =node;
node=knode->next;
knode->next=NULL;
}
void rotate_k(结构列表*节点,int k)
{
整数计数=0;
结构列表*knode,*ptr=node;
while(ptr!=NULL&&countnext;
计数++;
}
knode=ptr;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=节点;
节点=旋钮->下一步;
knode->next=NULL;
}
假设输入是1->2->3->4->5->6,k=4
输出应该是5->6->1->2->3->4,但是代码给出了输出1->2->3->4->5。
需要帮助:)您没有修改原始列表(
节点
参数)
struct list*rotate_k(struct list*node,int k)
{
整数计数=0;
结构列表*knode,*ptr=node;
while(ptr!=NULL&&countnext;
计数++;
}
knode=ptr;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=节点;
节点=旋钮->下一步;
knode->next=NULL;
return knode;//next=NULL
很奇怪;您应该在knode
之前的节点上执行此操作(这就是从结果中删除6的原因)SJuan的方法是正确的,但是如果你不想使用返回值,那么你需要为节点使用双指针。记住,C会复制你传递到函数中的变量。如果原始根节点是指针(我假设它是指针)然后,您需要创建指向指针的指针,否则您只是更改根节点指针的副本,而不是实际的根节点指针
void rotate_k(struct list **node, int k)
{
int count = 0;
struct list *knode, *ptr = *node;
while(ptr != NULL && count < k)
{
ptr = ptr->next;
count++;
}
knode = ptr;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = *node;
*node = knode->next;
knode->next = NULL;
}
void rotate\u k(结构列表**节点,int k)
{
整数计数=0;
结构列表*knode,*ptr=*节点;
while(ptr!=NULL&&countnext;
计数++;
}
knode=ptr;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=*节点;
*节点=旋钮->下一步;
knode->next=NULL;
}
void rotate\u list\u right(listnode**head,int k)
{
如果(!head | |!*head)
{
printf(“\nrotate\u list\u right:empty list=so return\n”);
返回;
}
if(k<1)
{
printf(“\n旋转列表右:无效输入:k必须大于等于1\n”);
返回;
}
listnode*post=*头;
listnode*curr=*头;
/*按k节点移动帖子*/
而(k--)
{
post=post->next;
如果(!post)/*k大于列表的长度*/
{
printf(“\n旋转列表右:无效输入:k必须小于列表大小\n”);
返回;
}
}
/*将当前移动到第k个最后节点*/
while(post->next)
{
当前=当前->下一步;
post=post->next;
}
/*currs的下一个是新标题*/
listnode*tmp=*头;
*head=当前->下一步;
当前->下一步=0;
//投递
post->next=tmp;
}
调试器说了些什么?想想需要对列表做些什么。找到第k个元素;将它设为新的头部,并将旧的尾部指向旧的头部。我看不到执行最后两部分的代码。在第二个while循环转到列表末尾时,也返回新的头部,并且ptr->next=节点将尾部指向旧的头部。node=knode->nex根据我从您的代码(以及结果)中读取的内容,t在列表中新增了5个标题,knode
指向您想要的元素5
。无论如何,请测试它。
void rotate_k(struct list **node, int k)
{
int count = 0;
struct list *knode, *ptr = *node;
while(ptr != NULL && count < k)
{
ptr = ptr->next;
count++;
}
knode = ptr;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = *node;
*node = knode->next;
knode->next = NULL;
}
void rotate_list_right(listnode** head, int k)
{
if( !head || !*head )
{
printf( "\nrotate_list_right: empty list = so return \n" );
return;
}
if( k < 1 )
{
printf( "\nrotate_list_right:invalid input: k must be >= 1 \n" );
return;
}
listnode* post = *head;
listnode* curr = *head;
/* move post by k nodes */
while(k--)
{
post = post->next;
if( !post ) /* k is bigger than length of the list */
{
printf( "\nrotate_list_right:invalid input: k must be smaller than list size \n" );
return;
}
}
/* move curr to kth-last node */
while(post->next)
{
curr = curr->next;
post = post->next;
}
/* currs' next is new header */
listnode* tmp = *head;
*head = curr->next;
curr->next = 0;
//join post
post->next = tmp;
}