[Go to site: main page, start]

0% found this document useful (0 votes)
10 views10 pages

Java Stack Final2

The document provides an overview of stacks in Java, describing them as linear data structures that follow Last In First Out (LIFO) ordering. It explains the basic operations of stacks, including push, pop, peek, and search methods, along with examples of their usage. Additionally, it covers various methods for manipulating stack elements, such as removing, setting, and inserting elements at specific positions.

Uploaded by

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

Java Stack Final2

The document provides an overview of stacks in Java, describing them as linear data structures that follow Last In First Out (LIFO) ordering. It explains the basic operations of stacks, including push, pop, peek, and search methods, along with examples of their usage. Additionally, it covers various methods for manipulating stack elements, such as removing, setting, and inserting elements at specific positions.

Uploaded by

Gian Sanpedro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Stack in Java

Overview
Java uses stacks and queues as its fundamental data structure. Both
stacks and queues are linear data structures because elements are stored
and accessed sequentially.

What is a Stack?

 a stack is an abstract, linear data type with a predefined capacity (or boundary).
It follows a particular order for adding or removing elements. Linear data
structures organize their components in a straight line, so if we add or remove an
element, they will grow or shrink respectively.

 Stacks are used in a variety of ways when we code. We use stacks to implement
functions, parsers, expression evaluation, and some algorithms. Stacks are great
for processing nested structures, so they are important for
understanding recursion.
 A simple real-world application of a stack is reversing a string letter by letter.
Another good example of a data stack is the undo and redo function on a
computer or text editor. Undo removes your most recent change, and redo builds
upon already existing changes.

Let us conceptualize stacks using a stack of plates placed in a box. The first plate
placed in the stack (the plate at the bottom of the stack) will be the last one to be
removed, and the plate added last would be the first to be removed.
This is called the Last In First Out (LIFO) or First In Last Out (FILO) ordering.

How do Stacks work?

 The implementation of stacks is relatively easy. The functionality depends on


the pop and push method, as you can see from the illustration above.
The pop method removes or deletes elements from the stack, while
the push method adds items to the stack.

When an element is inserted into a stack, it takes the top position and the variable
storing this position points to the number below it. The top variable should be updated
anytime an element is inserted or removed from it.

Note: What’s important to remember is that insertion and deletion happen on the same
end of a Stack.

A typical stack must contain the following methods:

 pop(): this method removes an element from the top of the stack and returns it.
 push(): this method adds an element to the top of the stack.

Stack push() Method in Java


 push(E element) method - is used to push an element into the Stack. The
element gets pushed onto the top of the Stack.
Syntax:
[Link](E element)
Example:
//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link](" Initial Stack : " + mystack);
Stack pop() Method in Java
 pop() method - is used to pop an element from the stack. The element is popped
from the top of the stack and is removed from the same.
Syntax:
[Link]()
Example:
//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link](" Initial Stack : " + mystack);
Displaying the list after the pop operation
//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link](" Initial Stack : " + mystack);

//removing the elements using the pop() method


[Link](" Popped element: " +
[Link]());
[Link](" Popped element: " +
[Link]());

// Displaying final list after the pop() method


[Link](" Final Stack after : " +
mystack);
Output:
Stack peek() Method in Java
 peek() method - is used to retrieve or fetch the first element of the Stack or the
element present at the top of the Stack. The element retrieved does not get
deleted or removed from the Stack.

Syntax:
[Link]()

Example:
//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link]("Initial Stack : " + mystack);

// Fetching the element at the head of the Stack


[Link]("The element at the top of the
stack is: " + [Link]());
Output:
Stack search() Method in Java
 search(Object element) method - is used to search for an element in the stack
and get its distance from the top. This method starts the count of the position
from 1 and not from 0. The element that is on the top of the stack is considered
to be at position 1. If more than one element is present, the index of the element
closest to the top is returned. The method returns its position if the element is
successfully found and returns -1 if the element is absent.
Syntax:
[Link](element)
Stack<Integer> mynumber = new Stack<Integer>();

// Stacking int values


[Link](8);
[Link](4);
[Link](3);
[Link](3);
[Link](5);

// Displaying the Stack


[Link]("The stack is: " + mynumber);

// Checking for the element 9


[Link]("Does the stack contains '9'? "
+ [Link](9));
// Checking for the element 10
[Link]("Does the stack contains '10'? "
+ [Link](10));

// Checking for the element 11


[Link]("Does the stack contains '11'? "
+ [Link](11));
// Checking for the element 8
[Link]("Does the stack contains '8'? "
+ [Link](8));
Output:
Stack removeElementAt() method in Java
 removeElementAt(int index)- is used to remove an element from a Stack from a
specific position or index. In this process the size of the Stack is automatically
reduced by one and all other elements after the removed element are shifted
downwards by one position.

Syntax:
[Link](int index)

Example:
//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link]("Initial Stack : " + mystack);

// Initial size
[Link]("The initial size is: "
+ [Link]());

// Remove the element at 3rd position


[Link](2);

// Print the final Stack


[Link]("Final Stack: " + mystack);

// Final size
[Link]("The final size is: "
+ [Link]());
Output:
Stack setElementAt() method in Java with
 setElementAt()- is used to set the component at the specified index of this vector
to be the specified object. The previous component at that position is discarded.
The index must be a value greater than or equal to 0 and less than the current
size of the vector.

Syntax:
setElementAt(E element, int index)

//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link]("Initial Stack : " + mystack);

// Using setElementAt() method to replace element


[Link]("JAVASCRIPT", 3);

// Displaying the modified linkedstack


[Link]("The new Stack is:"
+ mystack);
Output:
Stack firstElement() method in Java
 firstElement() - is used to retrieve or fetch the first element of the Stack. It returns
the element present at the 0th index of the Stack

Syntax:
[Link]()

Stack lastElement() method in Java


 lastElement() - is used to retrieve or fetch the last element of the Stack. It returns
the element present at the last index of the Stack.

Syntax:
[Link]()
Example:

//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link]("Initial Stack : " + mystack);

//displaying the first element


[Link]("The first element is: "
+ [Link]());

//displaying the last element


[Link]("The last element is: "
+ [Link]());

Output:
Stack insertElementAt() method in Java
 insertElementAt(element, index) - is used to insert a particular element at the
specified index of the Stack. Both the element and the position is passed as the
parameters. If an element is inserted at a specified index, then all the elements
are pushed upward by one and hence the capacity is increased, creating a space
for the new element.

Syntax:

[Link]()

Example:
//Creating a Stack
Stack<String> mystack = new Stack<String>();

// Adding elements into the stack


// using push() method
[Link](" C++");
[Link](" PHP");
[Link](" JAVA" );
[Link](" C#");
[Link](" PHYTON");

// Displaying the Stack


[Link]("Initial Stack : " + mystack);

// Inserting element at 3rd position


[Link]("RUBY RAILS", 2);

// Inserting element at last position


[Link]("R STUDIO", 5);

// Displaying the final Stack


[Link]("The final Stack is "
+ mystack);

Output:

You might also like