使用PHP实现单链表

单链表顾名思义就是一个链式数据结构,它有一个表头,并且除了最后一个节点外,所有节点都有其后继节点。如下图。

首先,我们写出链表节点的类。单链表中的每一个节点,都保存其数据域和后驱指针

[php]  view plain  copy
  1. //链表节点   
  2. class node {   
  3.     public $id//节点id   
  4.     public $name//节点名称   
  5.     public $next//下一节点   
  6.      
  7.     public function __construct($id$name) {   
  8.         $this->id = $id;   
  9.         $this->name = $name;   
  10.         $this->next = null;   
  11.     }   
  12. }  
链表中还有两个特别重要的方法,插入和删除。插入需要找到插入的位置,把前一个元素的next指针指向被插入的节点,并将被插入节点的next指针指向后一个节点,如下图左侧所示。而删除则是把前一个节点的next指针指向后一个节点,并返回被删除元素的数据内容,如下图右侧所示。
 

[php]  view plain  copy
  1. //单链表   
  2. class singelLinkList {   
  3.     private $header//链表头节点   
  4.      
  5.     //构造方法   
  6.     public function __construct($id = null, $name = null) {   
  7.         $this->header = new node ( $id$name, null );   
  8.     }   
  9.   
  10.     //获取链表长度   
  11.     public function getLinkLength() {   
  12.         $i = 0;   
  13.         $current = $this->header;   
  14.         while ( $current->next != null ) {   
  15.             $i ++;   
  16.             $current = $current->next;   
  17.         }   
  18.         return $i;   
  19.     }   
  20.   
  21.     //添加节点数据   
  22.     public function addLink($node) {   
  23.         $current = $this->header;   
  24.         while ( $current->next != null ) {   
  25.             if ($current->next->id > $node->id) {   
  26.                 break;   
  27.             }   
  28.             $current = $current->next;   
  29.         }   
  30.         $node->next = $current->next;   
  31.         $current->next = $node;   
  32.     }   
  33.   
  34.     //删除链表节点   
  35.     public function delLink($id) {   
  36.         $current = $this->header;   
  37.         $flag = false;   
  38.         while ( $current->next != null ) {   
  39.             if ($current->next->id == $id) {   
  40.                 $flag = true;   
  41.                 break;   
  42.             }   
  43.             $current = $current->next;   
  44.         }   
  45.         if ($flag) {   
  46.             $current->next = $current->next->next;   
  47.         } else {   
  48.             echo "未找到id=" . $id . "的节点!<br>";   
  49.         }   
  50.     }  
  51.   
  52.     //判断连表是否为空  
  53.     public function isEmpty(){  
  54.             return $this->header == null;  
  55.     }  
  56.   
  57.     //清空链表  
  58.     public function clear(){  
  59.             $this->header = null;  
  60.     }   
  61.   
  62.     //获取链表   
  63.     public function getLinkList() {   
  64.         $current = $this->header;   
  65.         if ($current->next == null) {   
  66.             echo ("链表为空!");   
  67.             return;   
  68.         }   
  69.         while ( $current->next != null ) {   
  70.             echo 'id:' . $current->next->id . '   name:' . $current->next->name . "<br>";   
  71.             if ($current->next->next == null) {   
  72.                 break;   
  73.             }   
  74.             $current = $current->next;   
  75.         }   
  76.     }   
  77.   
  78.     //获取节点名字   
  79.     public function getLinkNameById($id) {   
  80.         $current = $this->header;   
  81.         if ($current->next == null) {   
  82.             echo "链表为空!";   
  83.             return;   
  84.         }   
  85.         while ( $current->next != null ) {   
  86.             if ($current->id == $id) {   
  87.                 break;   
  88.             }   
  89.             $current = $current->next;   
  90.         }   
  91.         return $current->name;   
  92.     }   
  93.   
  94.     //更新节点名称   
  95.     public function updateLink($id$name) {   
  96.         $current = $this->header;   
  97.         if ($current->next == null) {   
  98.             echo "链表为空!";   
  99.             return;   
  100.         }   
  101.         while ( $current->next != null ) {   
  102.             if ($current->id == $id) {   
  103.                 break;   
  104.             }   
  105.             $current = $current->next;   
  106.         }   
  107.         return $current->name = $name;   
  108.     }   
  109. }  
  110. $lists = new singelLinkList ();   
  111. $lists->addLink ( new node ( 5, 'eeeeee' ) );   
  112. $lists->addLink ( new node ( 1, 'aaaaaa' ) );   
  113. $lists->addLink ( new node ( 6, 'ffffff' ) );   
  114. $lists->addLink ( new node ( 4, 'dddddd' ) );   
  115. $lists->addLink ( new node ( 3, 'cccccc' ) );   
  116. $lists->addLink ( new node ( 2, 'bbbbbb' ) );   
  117. $lists->getLinkList ();   
  118. echo "<br>-----------删除节点--------------<br>";   
  119. $lists->delLink ( 5 );   
  120. $lists->getLinkList ();  
  121. echo "<br>-----------更新节点名称--------------<br>";   
  122. $lists->updateLink ( 3, "222222" );   
  123. $lists->getLinkList ();  
  124. echo "<br>-----------获取节点名称--------------<br>";   
  125. echo $lists->getLinkNameById ( 5 );  
  126. echo "<br>-----------获取链表长度--------------<br>";   
  127. echo $lists->getLinkLength ();   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值