[go: up one dir, main page]

0% found this document useful (0 votes)
2 views9 pages

javastack

stack in java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

javastack

stack in java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Stack Class

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.

Working of Stack in Data Structures

Now, assume that you have a stack of books.


You can only see the top, i.e., the top-most book, namely 40, which is kept top
of the stack.
If you want to insert a new book first, namely 50, you must update the top and
then insert a new text.
And if you want to access any other book other than the topmost book that is
40, you first remove the topmost book from the stack, and then the top will
point to the next topmost book.
After working on the representation of stacks in data structures, you will see
some basic operations performed on the stacks in data structures.

Java Stack Basic Operation:

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

Program to show an Example of Stack program in Java


Output:

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.

Java.Util.Queue contains multiple elements before the process. The order of


elements of the queue in Java is FIFO (first-in-first-out). It provides additional
operations such as insertion, inspection, and deletion.

Working of Queue Data Structure

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.

Some of the commonly used methods of the Queue interface are:

 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.

Program To Show the Implementation of Queue


Output:

Java Linked List


A linked list is like a train where each bogie is connected with links. Different
types of linked lists exist to make lives easier, like an image viewer, music
player, or when you navigate through web pages.

Types Of Linked List

Singly Linked List

A singly linked list is a unidirectional linked list. So, you can only traverse it in
one direction, from head node to tail node.

Doubly Linked List

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.

Working of a Java LinkedList


Elements in linked lists are not stored in sequence. Instead, they are scattered
and connected through links (Prev and Next).

Methods of Java LinkedList


LinkedList provides various methods that allow us to perform different
operations in linked lists. We will look at four commonly used LinkedList
Operators in this tutorial:

 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.

Program to Show the Implementation of the single


linked list
Output

You might also like