马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
算是1个熟悉的过程,所以c语言的基础有就更好,没有也不怕。 PHP完成的单链表,以下代码:
- <?php //结点类 class Node{ private $next = NULL; //下一个结点指针 private $data = NULL; //数据 public function Node($data, $next = NULL){ $this->data = $data; $next && $this->next = $next; } public function getData(){ return $this->data; } public function setData($data){ $this->data = $data; } public function getNext(){ return $this->next; } public function setNext($next){ $this->next = $next; } } //单链表类 class LinkList{ private $data_list = NULL; //结点集 public function LinkList($data = false){ $this->data_list = array(); $title = new Node(NULL); $this->data_list[] = $title; if($data){ if(is_array($data)){ $this->addMoreData($data); }else{ $this->addData($data); } } } //前往
第N个结点的值 public function getNodeByNumber($number){ return $this->data_list[$this->findKeyByNumber($number)]->getData(); } //添加一组结点 public function addMoreData($datas){ foreach($datas as $value){ $this->addData($value); } } //添加结点一致
进口
,供里面
挪用
//$number 添加在第几个结点的前面
public function addData($data, $number = false){ $node = new Node($data); if($number === FALSE $number == count($this->data_list)){ $this->insertLastNode($node); }elseif($number > count($this->data_list)){ return false; }else{ $this->insertNode($node, $number); } } //拔出
一个结点到最初
private function insertLastNode($node){ $node->setNext(NULL); $lastKey = $this->findLastNode(); $insert_key = $this->insertNodeIntoArray($node); $this->data_list[$lastKey]->setNext($insert_key); } //拔出
一个结点 private function insertNode($node, $number){ $insert_number = $this->findKeyByNumber($number); $node->setNext($this->data_list[$insert_number]->getNext()); $insert_key = $this->insertNodeIntoArray($node); $this->data_list[$insert_number]->setNext($insert_key); } //查找第N个结点对应的数组key private function findKeyByNumber($number){ $i = $key = 0; while($i < $number){ $key = $this->data_list[$key]->getNext(); $i ++; } return $key; } //将结点到场
数组 private function insertNodeIntoArray($node){ $this->data_list[] = $node; return $this->getLastKey(); } //删除结点 public function deleteNode($number){ if($number == 0 $number > count($this->data_list)){ return false; } $pre_key = $this->findKeyByNumber($number - 1); $key = $this->data_list[$pre_key]->getNext(); $this->data_list[$pre_key]->setNext($this->data_list[$key]->getNext()); unset($this->data_list[$key]); } //查找某结点的前一个结点 private function getPreNodeKey($key){ foreach($this->data_list as $k=>$v){ if($v->getNext() == $key){ return $k; } } return false; } //打印链表 public function getData_list(){ return $this->data_list; } //前往
数组的最初
一个键 private function getLastKey(){ end($this->data_list); return key($this->data_list); } //判别
某个键值是不是
存在 private function ifExistKey($key){ if(array_key_exists($key, $this->data_list)){ return true; } return false; } //查找尾结点 public function findLastNode(){ foreach($this->data_list as $key=>$value){ if($value->getNext() === NULL){ return $key; } } } } ?>
复制代码 没有人会喜欢和见异思迁的人交朋友,因为这种人太不安分,太不可靠,因此,你必须要强迫自己完成自己的目标,哪怕可能会很难受,也得坚持,毅力就是这么锻炼出来的。 |