Stack and Queue Data Structure (Java code and Leetcode problems)

Stack and queue are dynamic data structures, both support delete operation.

In a stack, delete operation will remove the element which is inserted into lately. It’s called last in, first out, LIFO policy.

Stack just like a pile of files on your desk, you need to take down the file on the top to access the files underneath.

In a queue, delete operation removes the element inserted first. It’s called first-in, first-out, FIFO policy.

Like we stand in a line buy something, it a real-life queue.

Stack

The insert operation in the stack is called Push, put data on the top of the stack. And delete operation is called Pop, get the data on the top, and delete it.

This is a simple Java implementation of the stack:

Stack problems:

Queue

The insert operation of the queue is called Enqueue, put data to the end of the queue. The delete operation of the queue is called Dequeue, return the element in the end and delete it.

This is a simple Java implementation of the queue.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.