PHP简单实现循环链表功能示例

2025-05-29 0 32

本文实例讲述了PHP简单实现循环链表功能。分享给大家供大家参考,具体如下:

概述:

循环链表是另一种形式的链式存贮结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环。

如下图所示:

PHP简单实现循环链表功能示例

实现代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62
<?php

class node{

public $data;

public $link;

public function __construct($data=null,$link=null){

$this->data=$data;

$this->link=$link;

}

}

class cycleLinkList{

public $head;

public function __construct($data,$link=null){

$this->head=new node($data,$link);

$this->head->link=$this->head;

}

public function insertLink($data){

$p=new node($data);

$q=$this->head->link;

$r=$this->head;

if($q==$r)

{

$q->link=$p;

$p->link=$q;

return;

}

while($q!=$this->head){

$r=$q;$q=$q->link;

}

$r->link=$p;

$p->link=$this->head;

}

}

$linklist=new cycleLinkList(1);

for($i=2;$i<11;$i++){

$linklist->insertLink($i);

}

$q=$linklist->head->link;

echo $linklist->head->data;

while($q!=$linklist->head){

echo $q->data;

$q=$q->link;

}

echo "<br>--------------------------<br>";

$p=$linklist->head;

$r=$p;

$n=10;

$i=2;

while($n)

{

while(0!=$i){

$r=$p;$p=$p->link;

$i--;

}

echo $p->data;

$r->link=$p->link;

$tmp=$p;

$p=$p->link;

unset($tmp);

$n--;

$i=2;

}

?>

运行结果:

?

1

2

3
12345678910

--------------------------

36927185104

希望本文所述对大家PHP程序设计有所帮助。

原文链接:http://blog.csdn.net/jingbing082619/article/details/46955235

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP简单实现循环链表功能示例 https://www.kuaiidc.com/93331.html

相关文章

发表评论
暂无评论