Write a program to implement stack using stack class in data structure
Introduction Stack
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
There are many real-life examples of a stack. Consider an example of plates stacked over one another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
stack
stack operations Algorithm by using Array
1. PUSH:-When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs.
2. POP:- When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an underflow state.
3.isEmpty:- It determines whether the stack is empty or not.
4. peek:- It returns the element at the given position.
stack implementation by using Array
import java.util.Stack;;
class stackclass {
public static void main(String[] args) {
Stack< String > animals = new Stack < >();
animals.push("shubhi");
animals.push("narayan");
animals.push("mane");
System.out.println(animals);
animals.pop();
System.out.println(animals);
}
}
output
[shubhi, narayan, mane]
[shubhi, narayan]
stack implementations ways
- how implement stack By using array
- how implement stack By using Linked list
- how implement stack By using stack class
Next Topic
No comments:
Post a Comment