---
title: "how to create queue using two stack in PHP."  
description: "how to create queue using two stack in PHP."  
author: "Erick Wilsom"  
published: 2022-10-27  
updated: 2022-10-27  
canonical: https://www.mindstick.com/forum/157177/how-to-create-queue-using-two-stack-in-php  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# how to create queue using two stack in PHP.

how to create one [queue](https://www.mindstick.com/forum/160388/describe-the-basic-features-and-use-cases-of-the-queue-and-stack-collections-in-c-sharp) using [two stack](https://answers.mindstick.com/qa/98636/how-to-create-a-queue-using-two-stack-in-java) in PHP.

## Replies

### Reply by Ravi Misra

```php
<?php
class ReadingList
{
    protected $stack;
    protected $limit;

    public function __construct($limit = 10) {
        // initialize the stack
        $this->stack = array();
        // stack can only contain this many items
        $this->limit = $limit;
    }

    public function push($item) {
        // trap for stack overflow
        if (count($this->stack) < $this->limit) {
            // prepend item to the start of the array
            array_unshift($this->stack, $item);
        } else {
            throw new RunTimeException('Stack is full!');
        }
    }

    public function pop() {
        if ($this->isEmpty()) {
            // trap for stack underflow
	      throw new RunTimeException('Stack is empty!');
	  } else {
            // pop item from the start of the array
            return array_shift($this->stack);
        }
    }

    public function top() {
        return current($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }
}
```

### Reply by Hemant Patel

**I have no knowledge of PHP language.**

```html
<!doctype html>
<html>
<body>
   <h3> Implementing enqueue and dequeue using two stacks </h3>
   <div id='result1'></div>
   <div id='result2'></div>
   <div id='result3'></div>
   <script>
      class Queue {
         constructor() {
            this.enqueueStack = [];
            this.dequeueStack = [];
         }
         enqueue(item) {
            this.enqueueStack.push(item);
         }
         dequeue() {
            if (this.dequeueStack.length === 0) {

               // move everything from enqueueStack to dequeueStack
               while (this.enqueueStack.length > 0) {
                  const item = this.enqueueStack.pop();
                  this.dequeueStack.push(item);
               }
            }

            // dequeue from dequeueStack
            return this.dequeueStack.pop();
         }
      }

      // testing
      const queue = new Queue();

      // enqueue three strings
      queue.enqueue('tutorials');
      queue.enqueue('point');
      queue.enqueue('JavaScript');
      document.getElementById('result1').innerHTML = queue.dequeue(); // 1
      document.getElementById('result2').innerHTML = queue.dequeue(); // 2
      document.getElementById('result3').innerHTML = queue.dequeue(); // 3
   </script>
</body>
</html>
```


---

Original Source: https://www.mindstick.com/forum/157177/how-to-create-queue-using-two-stack-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
