Implement a custom memory-efficient stack or queue that minimizes allocations and operates under strict time complexity bounds.