Stack and Queue in java

 


Stack:-

    Stack is a Java class that extends Vector in the Collection framework. In stack data structure have FILO(First In Last Out), LIFO(Last In First Out) concept.

    Its mean, if we add some data  to the stack, inserted first data  we can execute it in last. We can execute last inserted data at first.

    Java provides an implementation of a stack through the java.util.Stack class, which extends the Vector class.






    If there are any element is not in the stack, we called that stacks as empty stack. 

    In a stack, you typically have operations like:

            Push:-  Adds an element to the top of the stack.



            Pop:-Removes and returns the top element from the stack.



            Peek:-Returns the top element without removing it from the stack.



    The "top" element in a stack refers to the element that is at the top of the stack, meaning it is the last element that was added.

Queue:-

    Java Queue represents an ordered list of elements.. In queue data structure have FIFO(First In First Out), LILO(Last In Last Out) concept. Its look like real life queue.

    Its mean, if we add some data  to the queue,  inserted first data we can execute it in first. We can execute last inserted data at last.

    Queue is an interface in java. We cant create objects using interface directly. We can implement queue using linkedlist.

Real world example





    In queue has there are several operations like ,

            Enqueue (Add): Adding an element to the end of the queue.


 
    
           Dequeue (Remove): Removing and returning the element from the front of the queue.



           Peek: Retrieving the element at the front of the queue without removing it.



Comments

Popular posts from this blog

අද අපි කතා කරමු Data collections ගැන

Understanding and Mastering Coding Errors

OOP key points