PHP单链表的基本操作
链表的实现
数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么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单链表的基本操作的更多相关文章
- 用Java实现单链表的基本操作
笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置. package mars; //单链表添加,删除节点 public class ListNode ...
- 单链表及基本操作(C语言)
#include <stdio.h> #include <stdlib.h> /** * 含头节点单链表定义及基本操作 */ //基本操作函数用到的状态码 #define TR ...
- 用java简单的实现单链表的基本操作
package com.tyxh.link; //节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 ...
- 单链表的基本操作--c++
#include <iostream> //实现单链表的建立,测长和打印 #include <string> using namespace std; struct node ...
- 【C++/数据结构】单链表的基本操作
#pragma once #ifndef _CLIST_H_ #define _CLIST_H_ #include <iostream> #include <assert.h> ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- C++实现单链表
之前一直没怎么在意C++中的链表,但是突然一下子让自己写,就老是出错.没办法,决定好好恶补一下该方面的知识,也为今后的数据结构大下个良好的基础,于是我总结出以下几点,有些地方可能不正确,还望大家不吝赐 ...
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- PHP数据结构之三 线性表中的单链表的PHP实现
线性表的链式存储:用一组任意的存储单元存储线性表中的数据元素.用这种方法存储的线性表简称线性链表. 链式存储线性表的特点:存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分 ...
随机推荐
- CSS学习(二)
display : block inline-block inline block此元素将显示为块级元素,此元素前后会带有换行符. inline默认.此元素会被显示为内联元素,元素前后 ...
- CentOS 6 安装 MySQL 8.0.+
1.先查询是否安装MySQL 大多数centos 6 自带 MySQL5.1 命令: rpm -qa|grep mysql 执行: [root@lifan ~]# rpm -qa|grep mysql ...
- flask之flask-sqlalchemy(一)
一 安装flask-sqlalchemy pip install flask-sqlalchemy 二 导入相关模块和对象 from flask_sqlalchemy import SQLAlchem ...
- Java并发编程:volatile关键字解析(学习总结-海子)
博文地址:Java并发编程:volatile关键字解析
- IntelliJ IDEA16 热部署,解决每次修改java文件就得重启tomcat的问题
这样就可以了....
- 接口调用 读取图片报错 Access to the path '' is denied.解决方案
调用接口 读取服务器上 图片 报错: Server was unable to process request. ---> Access to the path '图片路径' is denied ...
- 简单的数据库连接池实例(java语言)
1.概述 频繁的创建和销毁数据库连接消耗非常多的系统资源,创建一个池子, 管理一定数量的连接,用的时候去池中取,用完了放回池中,这时比较通用的做法. 2.关键字 LinkedList synchro ...
- 本地存储localStroage的用法及示例
localStorage是HTML5在在客户端存储数据的新方法,存储的数据没有时间限制. localStorage的主要API: localStorage.setItem(key,value); ...
- dotnet watch+vs code提升asp.net core开发效率
在园子中,已经又前辈介绍过dotnet watch的用法,但是是基于asp.net core 1.0的较老版本来讲解的,在asp.net core 2.0的今天,部分用法已经不太一样,所以就再写一篇文 ...
- Open images from USB camera on linux using V4L2 with OpenCV
I have always been using OpenCV's VideoCapture API to capture images from webcam or USB cameras. Ope ...