链表的实现

数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X

上代码吧

<?php

/**
*@author:gongbangwei(18829212319@163.com)
*@version:1.0
*@date:2016-05-22
*单链表的基本操作
*1.初始化单链表 __construct()
*2.清空单链表 clearSLL()
*3.返回单链表长度 getLength()
*4.判断单链表是否为空 getIsEmpty()
*5.头插入法建表 getHeadCreateSLL()
*6.尾插入法建表 getTailCreateSLL()
*7.返回第$i个元素 getElemForPos()
*8.查找单链表中是否存在某个值的元素 getElemIsExist()
*9.单链表的插入操作 getInsertElem()
*10.遍历单链表中的所有元素 getAllElem()
*11.删除单链中第$i个元素 getDeleteElem()
*12.删除单链表所有重复的值 getElemUnique()
**/
header("content-type:text/html;charset=UTF-8");
class LNode{
public $mElem;
public $mNext;
public function __construct(){
$this->mElem=null;
$this->mNext=null;
}
}
class SingleLinkedList{
//头结点数据
public $mElem;
//下一结点指针
public $mNext;
//单链表长度
public static $mLength=0;
public function __construct(){
$this->mElem=null;
$this->mNext=null;
}
//返回单链表长度
public static function getLength(){
return self::$mLength;
}
public function getIsEmpty(){
if(self::$mLength==0 && $this->mNext==null){
return true;
}
else{
return false;
}
}
public function clearSLL(){
if(self::$mLength>0){
while($this->mNext!=null){
$q=$this->mNext->mNext;
$this->mNext=null;
unset($this->mNext);
$this->mNext=$q;
}
self::$mLength=0;
}
}
public function getHeadCreateSLL($sarr){
$this->clearSLL(); if(is_array($sarr) and count($sarr)>0){
foreach ($sarr as $key => $value) {
$p= new LNode;
$p->mElem=$value;
$p->mNext=$this->mNext;
$this->mNext=$p;
self::$mLength++;
}
}
else{
return false;
}
return true;
}
public function getTailCreateSLL($sarr){
$this->clearSLL(); if(is_array($sarr) and count($sarr)>0){
$q=$this;
foreach($sarr as $value){
$p=new LNode;
$p->mElem=$value;
$p->mNext=$q->mNext;
$q->mNext=$p;
$q=$p;
self::$mLength++;
}
}
else{
return false;
}
}
public function getElemForPos($i){
if(is_numeric($i) && $i<self::$mLength && $i>0){
$p=$this->mNext;
for ($j=1; $j < $i ; $j++) {
$q=$p->mNext;
$p=$q;
}
return $p->mElem;
}
else{
return null;
}
}
public function getElemIsExist($value){
if($value){
$p=$this;
while($p->mNext!=null and $p->mElem!=value){
$q=$p->mNext;
$p=$q;
}
if($p->mElem==value){
return true;
}
else{
return false;
}
}
}
public function getElemPosition($value){
if($value){
$p=$this;
$pos=0;
while($p->mNext!=null and $p->mElem!=$value){
$q=$p->mNext;
$p=$q;
$pos++;
}
if($p->mElem==$value){
return $pos;
}
else{
return -1;
}
}
}
/*单链表的插入操作
*
*@param int $i 插入元素的位序,即在什么位置插入新的元素,从1开始
*@param mixed $e 插入的新的元素值
*@return boolean 插入成功返回true,失败返回false
*/
public function getInsertElem($i,$e){
if($i<self::$mLength){
$j=1;
$p=$this;
}
else{
return false;
}
while($p->mNext!=null and $j<$i){
$q=$p->mNext;
$p=$q;
$j++;
}
$q=new LNode;
$q->mElem=$e;
$q->mNext=$p->mNext;
$p->mNext=$q;
self::$mLength++;
return true;
}
/**
*删除单链中第$i个元素
*@param int $i 元素位序
*@return boolean 删除成功返回true,失败返回false
*/
public function getDeleteElem($i){
if($i>self::$mLength || $i<1){
return false;
}
else{
$p=$this;
$j=1;
while($j<$i){
$p=$p->mNext;
$j++;
}
$q=$p->mNext;
$p->mNext=$q->mNext;
unset($q);
self::$mLength--;
return true;
}
}
public function getAllElem(){
$all=array();
if(!$this->getIsEmpty()){
$p=$this->mNext;
while($p->mNext){
$all[]=$p->mElem;
$p=$p->mNext;
}
if($p->mElem)
$all[]=$p->mElem;
return $all;
}
}
public function getElemUnique(){
if(!$this->getIsEmpty()){
$p=$this;
while($p->mNext!=null){
$q=$p->mNext;
$ptr=$p;
while($q->mNext!=null){
if(strcmp($p->mElem,$q->mElem)===0){
$ptr->mNext=$q->mNext;
$q->mNext=null;
unset($q->mNext);
$q=$ptr->mNext;
self::$mLength--;
}
else{
$ptr=$q;
$q=$q->mNext;
}
}
//处理最后一个元素
if(strcmp($p->mElem,$q->mElem)===0){
$ptr->mNext=null;
self::$mLength--;
}
$p=$p->mNext;
}//end of while
}
}
} ///////////////test//////////
$node=new SingleLinkedList;
$arr=array('gbw','michael','php','js');
//$node->getHeadCreateSLL($arr);
//print_r($node->getAllElem());
$node->getTailCreateSLL($arr);
echo $node->getElemForPos(2);
$pos=$node->getElemPosition('gbw');
echo $pos;
$node->getDeleteElem($pos);
$node->getInsertElem(1,'gbw2');
print_r($node->getAllElem());

PHP单链表的基本操作的更多相关文章

  1. 用Java实现单链表的基本操作

    笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置. package mars; //单链表添加,删除节点 public class ListNode ...

  2. 单链表及基本操作(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点单链表定义及基本操作 */ //基本操作函数用到的状态码 #define TR ...

  3. 用java简单的实现单链表的基本操作

    package com.tyxh.link; //节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 ...

  4. 单链表的基本操作--c++

    #include <iostream> //实现单链表的建立,测长和打印 #include <string> using namespace std; struct node ...

  5. 【C++/数据结构】单链表的基本操作

    #pragma once #ifndef _CLIST_H_ #define _CLIST_H_ #include <iostream> #include <assert.h> ...

  6. Java实现单链表的各种操作

    Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素   4.实现链表的反转   5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...

  7. C++实现单链表

    之前一直没怎么在意C++中的链表,但是突然一下子让自己写,就老是出错.没办法,决定好好恶补一下该方面的知识,也为今后的数据结构大下个良好的基础,于是我总结出以下几点,有些地方可能不正确,还望大家不吝赐 ...

  8. 数据结构——Java实现单链表

    一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...

  9. PHP数据结构之三 线性表中的单链表的PHP实现

    线性表的链式存储:用一组任意的存储单元存储线性表中的数据元素.用这种方法存储的线性表简称线性链表. 链式存储线性表的特点:存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分 ...

随机推荐

  1. [转]ECMAScript 6 入门 -编程风格

    本文转自:http://es6.ruanyifeng.com/#docs/style 编程风格 块级作用域 字符串 解构赋值 对象 数组 函数 Map结构 Class 模块 ESLint的使用 本章探 ...

  2. T-SQL 备份和还原数据库

    --完整备份  Backup   Database   db_database  To disk='D:\Backup\db_database_Full.bak'   --差异备份  Backup   ...

  3. 在 Azure 上创建和链接 MySQL 数据库

    本快速入门介绍了如何使用 Azure 门户创建并连接 MySQL 数据库.在本教程中完成的所有操作均符合 1 元试用条件. 开始之前如果您还没有 Azure 账户,可以申请 1 元试用账户 步骤1:创 ...

  4. Error:Execution failed for task ':xutils:mergeDebugAndroidTestResources'. > No slave process to proc

    Error:Execution failed for task ':xutils:mergeDebugAndroidTestResources'. > No slave process to p ...

  5. java_对象序列化、反序列化

    1.概念 序列化:将对象转化为字节序列的过程 反序列化:将字节序列转化为对象的过程 用途: A:将对象转化为字节序列保存在硬盘上,如文件中,如文本中的例子就是将person对象序列化成字节序列,存在p ...

  6. PHP+Xdebug实现远程调试

    以前以为php调试时服务器端和IDE必须在同一台机子上,无意发现xdebug其实是支持远程调试的. 尝试之后发现可以配置成功,还是可以调试代码的感觉爽啊!   php所在Ubuntu服务器       ...

  7. 怎么让div显示一行,其余的隐藏。

    <style> div{ white-space: nowrap; text-overflow:ellipsis; text-overflow: ellipsis; overflow:hi ...

  8. C# javascript 全选按钮

    function selectAll(checkbox) {                  $('input[type=checkbox]').attr('checked', $(checkbox ...

  9. html 之 position用法

    引用: position的四个属性值: 1.relative2.absolute3.fixed4.static下面分别讲述这四个属性. <div id="parent"> ...

  10. 移动端Hybird的网络层优化策略

    一.前端代码策略:域名切换(多域名部署),解决DNS缓存及域名劫持 二.客户端策略 客户端在空闲时ping cdn节点域名列表中的域名,测量延时.丢包等数据.如果延迟 > xxx,丢包 > ...