• 队列和栈


    队列

    一、什么是队列

    图解

    解释:我们的队列就像排队加油一样,谁在前面谁就先加油,这就满足队列的概念先进先出的。

    概念:值允许在一端进行插入操作,而在另一端进行删除操作的线性表。一般的实现方法是通过链式表进行实现,所以也可叫链队列。

    场景:我们的输入缓冲区接受键盘的输入就是按队列的形式输入和输出,大家可以看一下你们自己的输入法。

    二、队列的实现代码

    链式队列

    <?php
    	class Node{
    		public $data;
    		public $next;
    		public function __construct($data = null)
    		{
    			$this->data = $data;
    			$this->next = null;
    		}
    	}
    
    	class Queue{
    		private $next;
    		private $font;//头指针
    		private $rear;//尾指针
    
    		public function __construct()
    		{
    			$this->font = $this;//指向头节点
    			$this->rear = $this;
    		}
    
    		//判断队列是否为空
    		public function isEmpty()
    		{
    			if($this->font == $this->rear){//如果两指针指向同一个地方,就认为是空队列
    				return true;
    			}else{
    				return false;
    			}
    		}
    		//插入
    		public function insert($val)
    		{
    			$node = new Node();
    			$node->data = $val;
    			$this->rear->next = $node;
    			$this->rear = $node;
    			echo "入队成功";
    			return;
    		}
    		//出队
    		public function delete()
    		{
    			if($this->isEmpty()){
    				echo "队列为空";
    				return;
    			}else{
    				$p = $this->font->next;
    				$this->font->next = $p->next;
    				if($this->rear == $p){
    					$this->rear = $this->font;
    				}
    				echo "出队成功";
    				return ;
    			}
    		}
    		//显示队列的所有元素
    		public function show()
    		{
    			if($this->isEmpty()){
    				echo "队列为空";
    				return;
    			}else{
    				$p = $this->font->next;
    				while ($p) {
    					$arr[] = $p->data;
    					$p = $p->next;
    				}
    				return $arr;
    			}
    		}
    	}
    	$line = new Queue();
    	$line->insert(1);
    	$line->insert(2);
    	$line->insert(3);
    	$line->insert(4);
    	$line->insert(5);
    	var_dump($line->show());
    

     循环队列

    <?php
    	class Queue{
    		private $font;//头指针
    		private $rear;//尾指针
    		private $data;
    		const Size = 10;
    		private static $count;
    		public function __construct()
    		{
    			$this->data = [];
    			$this->font = null;
    			$this->rear = null;
    			self::$count = 0;
    		}
    
    		//插入队列
    		public function insert($val)
    		{
    			if(self::$count == self::Size)
    			{
    				echo "队列已经满";
    				return ;
    			}
    			$this->rear = ($this->rear + 1)%self::Size;
    			$this->data[$this->rear] = $val;
    			self::$count++;
    		}
    		//删除队列的元素
    		public function delete()
    		{
    			if(self::$count == 0){
    				echo "队列为空";
    				return ;
    			}else{
    				$this->font = ($this->font + 1)%self::Size;
    				unset($this->data[$this->font]);
    				self::$count--;
    				echo "删除成功";
    				return ;
    			}
    		}
    		//显示队列的所有的元素
    		public function show()
    		{
    			return $this->data;
    		}
    	}
    	$line = new Queue();
    	$line->insert(1);
    	$line->insert(2);
    	$line->insert(3);
    	$line->insert(4);
    	var_dump($line->show());

    一、什么是栈

    图解

    解释:栈就像上面图所看到的,压子弹最先进去的就是到子弹夹的最底部(栈的底部),这也就满足栈的工作原理:先进后出。

    概念:栈是限制在一端进行插入操作和删除操作的线性表(俗称堆栈),允许进行操作的一端称为“栈顶”,另一固定端称为“栈底”,当栈中没有元素时称为“空栈”。向一个栈内插入元素称为是进栈,push;从一个栈删除元素称为是出栈,pop。特点 :先进后出。栈我们一般用顺序表来实现。

    场景:我们再浏览网页的时候,点击左上角的后退按钮,就直接返回你上一个浏览的网站,这个原理就是栈的原理。

    二、栈的代码实现

    <?php
    	class Stack{
    		//数据初始化
    		public function __construct()
    		{
    			$this->top = -1;//初始化当前指针指向-1,应为数组下标为0就不是空栈了
    			$this->size = 10;//申明栈的大小
    			$this->stack = [];//初始化栈为空栈
    		}
    		//进栈的操作
    		public function push($val)
    		{
    			if($this->top == $this->size - 1) {
    				echo "栈满";
    				return;
    			}
    			$this->top++;//利用数组的指针移动
    			$this->stack[$this->top] = $val;
    		}
    		//出栈的操作
    		public function pop()
    		{
    			if($this->top == -1) {
    				echo "栈已经为空";
    				return;
    			}
    			$val = $this->stack[$this->top];
    			unset($this->stack[$this->top]);
    			$this->top--;
    			return $val;
    		}
    		//查看栈里面所有元素
    		public function show()
    		{
    			for($i = $this->top; $i >= 0; $i--) {
    				echo $this->stack[$i];
    				echo "<br />";
    			}
    		}
    	}
    $stack = new Stack();
    $stack->push(1);
    $stack->push(2);
    $stack->push(3);
    $stack->push(4);
    $stack->push(5);
    $stack->push(6);
    $stack->push(7);
    $stack->show();

    效果:

  • 相关阅读:
    ECharts之类型bar(堆积条形图)
    ECharts之类型bar
    ECharts之类型pie
    ECharts入门一
    ECharts第一个实例
    html树形菜单控件
    【JavaScript】Javascript中document.execCommand()的用法
    【JavaScript】dhtmlXTree 中文API
    【javascript】addEventListener事件方法
    java判断集合是否重复的一种便捷方法
  • 原文地址:https://www.cnblogs.com/meichao/p/9228906.html
Copyright © 2020-2023  润新知