javastack
javastack
Stacks in Data Structures is a linear type of data structure that follows the LIFO
(Last-In-First-Out) principle and allows insertion and deletion operations from
one end of the stack data structure, that is top. Implementation of the stack
can be done by contiguous memory which is an array, and non-contiguous
memory which is a linked list. Stack plays a vital role in many applications.
Push: it is the method that are Used to insert the elements into the stacks
Pop: It is the method that are used to Remove an element from the stacks
Peek: it is the method returns an object from the top of the stack.
Search: It returns the position of the element from the top of the stack.
Empty: To check whether a stack is empty or not, we use the empty() method
Java Queue
A queue is an object that represents a data structure designed to have the
element inserted at the end of the queue, and the element removed from the
beginning of the queue.
In queues, elements are stored and accessed in First In, First Out manner. That is,
elements are added from the behind and removed from the front.
add() - Inserts the specified element into the queue. If the task is
successful, add() returns true, if not it throws an exception.
offer() - Inserts the specified element into the queue. If the task is
successful, offer() returns true, if not it returns false.
peek() - Returns the head of the queue. Returns null if the queue is
empty.
remove() - Returns and removes the head of the queue. Throws an
exception if the queue is empty.
poll() - Returns and removes the head of the queue. Returns null if the
queue is empty.
A singly linked list is a unidirectional linked list. So, you can only traverse it in
one direction, from head node to tail node.
A doubly linked list is a bi-directional linked list. So, you can traverse it in both
directions. Unlike singly linked lists, its nodes contain one extra pointer called
the previous pointer. This pointer points to the previous node.
Circular Linked List
A circular Linked list is a unidirectional linked list. So, you can traverse it in only
one direction. But this type of linked list has its last node pointing to the head
node. So while traversing, you need to be careful and stop traversing when you
revisit the head node.
Add elements: We can use the add() method to add an element (node)
at the end of the LinkedList.
Access elements: he get() method of the LinkedList class is used to
access an element from the LinkedList.
Change elements: The set() method of LinkedList class is used to change
elements of the LinkedList.
Remove elements: The remove() method of the LinkedList class is used
to remove an element from the LinkedList.