Implement Stack and Queue using Arrays
Stack using Array
Queue using Array
The Queue implementation above functions like a circular queue using the modulo operator (%) to wrap around when reaching the end of the array. This is evident in the push and pop operations:
- In the
pushmethod, we usethis.end = (this.end + 1) % this.sizeto wrap the end pointer back to the beginning when it reaches the end of the array. - Similarly, in the
popmethod, we usethis.start = (this.start + 1) % this.sizeto advance the start pointer in a circular manner.