交换单个链接列表中的两个节点 [英] Swap two nodes in a singly linked list

查看:44
本文介绍了交换单个链接列表中的两个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在pascal的单链接列表中切换两个节点?

How to switch two nodes in a singly linked list in pascal?

procedure List.switch(var n1,n2 : pNode);
var aux : pNode;
begin
  aux := n1;
  p1:= n2;
  n2 := aux;
end;

(这里pNode是列表中节点上的指针.)

(Here pNode is a pointer on a node in the list.)

节点的定义如下:

pNode = ^Node;
Node = record
    data : data;
    next : pNode;
end;

此代码无效.它要么不编译,要么说不能使用常量表达式的地址",要么什么都不做.我想这与指针的工作方式有关...

This code doesn't work. It either doesn't compile, saying "Can't take the address of constant expressions", or just doesn't do anything. I guess it has to do with how pointers work...

我在此处找到了相关信息,但我没有找到读C.

I found relevant information here, but I don't read C.

感谢您的任何建议!

推荐答案

我认为这样会起作用:

function SwapNodes(first: pNode): pNode;
begin
  Result := first.next;
  first.next := Result.next;
  Result.next := first;
end;

这篇关于交换单个链接列表中的两个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆