链表的实现

数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么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. jquery报表插件收藏 MARK

    Highcharts http://www.hcharts.cn/ 统计报表插件 jquery ui官网 http://jqueryui.com/selectmenu/#custom_render

  2. JS 中的require 和 import 区别整理

    ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...

  3. Eclipse 入手配置

    工欲善其事,必先利其器 在安装eclipse后进行简单的配置能够为以后的开发提高不少效率,这里慢慢先记录自己的经验. 一.编码配置 utf-8 1.worksplace: 首先设置就是workspac ...

  4. C Primer Plus note6

    error: invalid preprocessing directive #difine| 无效的宏定义处理 宏定义define 写成了 difine.

  5. side Effect

    副作用 side Effect 副作用是在计算结果的过程中,系统状态的一种变化,或者与外部世界进行的可观察的交互. 副作用可能包含,但不限于: 1.更改文件系统 2.往数据库里插入数据 3.发送一个h ...

  6. thinkphp5设置404页面不跳转

    thinkphp5设置404页面的步骤: 1. 首先关闭调试模式,即配置application/config文件,使'app_debug' => false 2. 添加自定义404页面跳转地址, ...

  7. linux awk 使用的一个例子

    1. 场景 从日志中获取漏发奖励的司机id 2. 日志 如下(需要获取一个时间段的 driverIdStr) ::-thread-] order.service.TOrderInfoServiceIm ...

  8. UX2内核浏览加速技术纲要[带你解决WebView卡顿]

    UX2内核是本人负责主要开发的浏览服务项目,其主要目的是为开发者提供一个简单好用.轻便的网络浏览服务.UX2内核的安卓端是基于WebView进行深度优化的,同时欢迎大家使用这个内核用于app页面或浏览 ...

  9. 关于Function原型对象和Object原型对象的一些疑惑

    网上有一道美团外卖的面试题是这样的: Function.prototype.a = 'a'; Object.prototype.b = 'b'; function Person(){}; var p ...

  10. 字符数字转换 atoi 与 strtol

    原文:http://www.cnblogs.com/JefferyZhou/archive/2010/07/01/1769555.html 在很多时候我们都很清楚 atoX 系列函数: atoi , ...