在链接列表前n位置中移动某个节点
Moving a certain node in a linked list forward n positions
我正在尝试在链接列表中移动节点向前n位置
例如
列表:1、2、3、4、5、6
移动'2'向前3位置
新列表:1、3、4、5、2、6
这就是我尝试的
void MoveNodeForward(Node* head, int x, int n)
{
Node* t = head;
while(t != NULL)
{
if(t->data == x)
{
for(int i=0; i<n && t->next != NULL; i++)
{
int temp = t->next->data;
t->next->data = t->data;
t->data = temp;
t = t->next;
}
}
t = t->next;
}
}
,但它只是交换节点的值。
我需要一种方法来改变节点本身的位置,而不仅仅是值。
void move_n_position(struct node **p1,int position,int n)//n is the no of time moving front
{
int i;
struct node *bef=NULL;
struct node *prev=NULL;
struct node *temp=*p1;
for(i=1;i<position;i++)
{
prev=temp;
temp=temp->link;
}
if(prev!=NULL)
{
bef=temp;
prev->link=bef->link;
}
else
{
bef=temp;
(*p1)=(*p1)->link;
}
i=0;
while(i<n)
{
temp=temp->link;
i++;
}
bef->link=temp->link;
temp->link= bef;
}
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- Boost Graph Library,修复节点大小
- 如何从具有移动语义的类对象中生成共享指针
- C++A*算法并不总是在路径中具有目标节点
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- 如何找到2个单链表的公共节点
- 在链接列表前n位置中移动某个节点
- 带有钩子的C 二进制树,让我知道何时/何时插入或移动节点
- 排序链表-移动节点还是交换数据成员
- 链表 - 移动到下一个节点
- 具有可移动节点、可访问属性和可靠ID的C++图
- 重载 + 运算符以模拟移动到下一个节点
- 使用可移动节点创建有向图(使用 QT / Boost)
- 使用pugixml和rapidxml移动(不复制)节点
- Cocos2dx:如何停止CCFollow移动视差节点并使背景变黑
- 如何在链表的节点间移动
- 在C++中使用节点撤消板上移动