搜索
查看: 1360|回复: 0

PHP常用算法和数据结构示例(必看篇)

[复制链接]

4770

主题

4771

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
14338
发表于 2018-2-11 11:56:26 | 显示全部楼层 |阅读模式

            实例如下:
$i;$j--){
      if($arr[$j]=0){
      $arr[$j+1] = $arr[$j];
      $j--;
    }
    $arr[$j+1] = $x;
  }
  return $arr;
}
echo '插入排序:';
echo implode(' ',InsertSort($arr))."
";
//---------------------------------------
//       常用查找算法
//---------------------------------------
//二分查找
function binary_search($arr,$low,$high,$key){
  while($low$arr[$mid]){
      $low = $mid+1;
    }
  }
  return -1;
}
$key = 6;
echo "二分查找{$key}的位置:";
echo binary_search(QSort($arr),0,8,$key);
//顺序查找
function SqSearch($arr,$key){
  $length = count($arr);
  for($i=0;$i$length){
    return "删除位置出错!";
  }
  for($i=$pos-1;$idata = 0; //用来记录链表长度
  $linkList->next = null;
}
//头插法创建链表
function createHead(&$linkList,$length){
  for($i=0;$idata = $i;
    $newNode->next = $linkList->next;//因为PHP中对象本身就是引用所以不用再可用“&”
    $linkList->next = $newNode;
    $linkList->data++;
  }
}
//尾插法创建链表
function createTail(&$linkList,$length){
  $r = $linkList;
  for($i=0;$idata = $i;
    $newNode->next = $r->next;
    $r->next = $newNode;
    $r = $newNode;
    $linkList->data++;
  }
}
//在指定位置插入指定元素
function insert($linkList,$pos,$elem){
  if($pos$linkList->data+1){
    echo "插入位置错误!";
  }
  $p = $linkList;
  for($i=1;$inext;
  }
  $newNode = new Node();
  $newNode->data = $elem;
  $newNode->next = $p->next;
  $p->next = $newNode;
}
//删除指定位置的元素
function delete($linkList,$pos){
  if($pos$linkList->data+1){
    echo "位置不存在!";
  }
  $p = $linkList;
  for($i=1;$inext;
  }
  $q = $p->next;
  $p->next = $q->next;
  unset($q);
  $linkList->data--;
}
//输出链表数据
function show($linkList){
  $p = $linkList->next;
  while($p!=null){
    echo $p->data." ";
    $p = $p->next;
  }
  echo '
';
}
$linkList = new Node();
init($linkList);//初始化
createTail($linkList,10);//尾插法创建链表
show($linkList);//打印出链表
insert($linkList,3,'a');//插入
show($linkList);
delete($linkList,3);//删除
show($linkList);
/**
* Class Stack
* 用PHP模拟顺序栈的基本操作
*/
class Stack{
  //用默认值直接初始化栈了,也可用构造方法初始化栈
  private $top = -1;
  private $maxSize = 5;
  private $stack = array();
  //入栈
  public function push($elem){
    if($this->top >= $this->maxSize-1){
      echo "栈已满!
";
      return;
    }
    $this->top++;
    $this->stack[$this->top] = $elem;
  }
  //出栈
  public function pop(){
    if($this->top == -1){
      echo "栈是空的!";
      return ;
    }
    $elem = $this->stack[$this->top];
    unset($this->stack[$this->top]);
    $this->top--;
    return $elem;
  }
  //打印栈
  public function show(){
    for($i=$this->top;$i>=0;$i--){
      echo $this->stack[$i]." ";
    }
    echo "
";
  }
}
$stack = new Stack();
$stack->push(3);
$stack->push(5);
$stack->push(8);
$stack->push(7);
$stack->push(9);
$stack->push(2);
$stack->show();
$stack->pop();
$stack->pop();
$stack->pop();
$stack->show();
/**
* Class Deque
* 使用PHP实现双向队列
*/
class Deque{
  private $queue = array();
  public function addFirst($item){//头入队
    array_unshift($this->queue,$item);
  }
  public function addLast($item){//尾入队
    array_push($this->queue,$item);
  }
  public function removeFirst(){//头出队
    array_shift($this->queue);
  }
  public function removeLast(){//尾出队
    array_pop($this->queue);
  }
  public function show(){//打印
    foreach($this->queue as $item){
      echo $item." ";
    }
    echo "
";
  }
}
$deque = new Deque();
$deque->addFirst(2);
$deque->addLast(3);
$deque->addLast(4);
$deque->addFirst(5);
$deque->show();
//PHP解决约瑟夫环问题
//方法一
function joseph_ring($n,$m){
  $arr = range(1,$n);
  $i = 0;
  while(count($arr)>1){
    $i=$i+1;
    $head = array_shift($arr);
    if($i%$m != 0){ //如果不是则重新压入数组
      array_push($arr,$head);
    }
  }
  return $arr[0];
}
//方法二
function joseph_ring2($n,$m){
  $r = 0;
  for($i=2;$i
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

长春门户网站

长春门户网是网民了解长春的网络窗口,同是提供长春地区百姓生活分类供求信息的门户网站,同时提供长春网站建设、长春网站设计,我们将逐步的完善网站分类信息资源;

长春门户网二维码

联系我们

  • 工作时间:早上8:00 - 晚上5:30
  • 投稿联系:13624467185(微信同号)
  • 反馈邮箱:5053050@QQ.com
  • 公司地址:吉林省长春市亚泰大街与自由大路交汇五环国际大厦1408室

QQ|小黑屋|手机版|Archiver|cc! ( 吉ICP备2021009740号-8 )

Powered by Discuz! X3.4 © 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表